Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

ArgumentOutOfRangeException on Stream.Read method
I'm having intermittent trouble with a call to the Read method of
the HttpWebResponse object.  I get an ArumentOutOfRangeException
claiming that deep down inside of the Read method, the count parameter
of the BlockCopy method is showing up as a negative number.  Don't
know where the negative number is coming from since I've passed the
value of 1024 to the Read and Write methods.  I've even tested the
value of buffer.Length and size at the time of the exception and they
are both 1024.


The exception and the source code are listed below:

System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst,
Int32 dstOffset, Int32 count)
at System.Net.ConnectStream.FillFromBufferedData(Byte[] buffer,
Int32& offset, Int32& size)
at System.Net.ConnectStream. FillFromBufferedData(NestedSingleAsyncRe
sult
castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[]
buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
state)
at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
size)
at BIZv1.Helpers.HttpHelper.FileTransfer(String urlpath, String
destination)
at BIZv1.BLL.MoveFilesFromBIZBLL.SaveFileFromBIZ()




public bool FileTransfer(string urlpath, string destination)
{
Stream istream = null;
FileStream ostream = null;
WebRequest wreq = WebRequest.Create( urlpath );
HttpWebResponse response;
byte[] buffer = new byte[this.m_bufferSize]; // 1024
int size = m_bufferSize; //1024

try
{
response = (HttpWebResponse) wreq.GetResponse();
istream = response.GetResponseStream();
long contentLength = Convert.ToInt64(response.ContentLength);
this.m_results += DateTime.Now.ToString("HH:mm:ss.ffffff") + ":
Downloading " + contentLength.ToString() + " bytes./n";
// Always overwrite downloaded file...
ostream = new FileStream(destination, FileMode.Create,
FileAccess.Write);

int maxCounter = 2000;
int counter = 0;

// Read and write data
while (true)
{
size = istream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
ostream.Write(buffer, 0, size);
}
else
{
break;
}
counter++;
if(counter >= maxCounter)
{
counter = 0;
System.Threading.Thread.Sleep(20);
}
}
}
catch(System.ArgumentOutOfRangeException e1)
{
string errorMessage = "Problems with getting\n" + urlpath  + " to
save to \n" + destination + "\n";


[extraneous exception handling code deleted]


Thanks in advance for anyone's help...

Report this thread to moderator Post Follow-up to this message
Old Post
Brian
06-04-04 12:31 AM


Re: ArgumentOutOfRangeException on Stream.Read method
Hi,

Since I can't actually debug this I'll guess.

istream.Read(buffer, 0, buffer.Length);

I assume your buffer is mangled in some way. have you taken a look at it to
make sure that there is in fact tome content in it. If not I would assume
that it is inited with 0's menaing byte 0 is 0 so length = -1 or some such
stuff.

If you have a working project that can reproduce it zip it up and include it
and I'll take a look at it. But without knowing what the values of the
variables and objects are it's a little hard to say. But I'm 99.9% sure
there is a problem with the buffer and its length in some way.

-Enjoy!!

-Evan

buffer.Length is actually different from your Content
"Brian" <Brian.L.Beard@irs.gov> wrote in message
news:c87cf7b0.0406030936.4ddf4ba9@posting.google.com...
> I'm having intermittent trouble with a call to the Read method of
> the HttpWebResponse object.  I get an ArumentOutOfRangeException
> claiming that deep down inside of the Read method, the count parameter
> of the BlockCopy method is showing up as a negative number.  Don't
> know where the negative number is coming from since I've passed the
> value of 1024 to the Read and Write methods.  I've even tested the
> value of buffer.Length and size at the time of the exception and they
> are both 1024.
>
>
> The exception and the source code are listed below:
>
> System.ArgumentOutOfRangeException: Non-negative number required.
> Parameter name: count
>    at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst,
> Int32 dstOffset, Int32 count)
>    at System.Net.ConnectStream.FillFromBufferedData(Byte[] buffer,
> Int32& offset, Int32& size)
>    at
System.Net.ConnectStream. FillFromBufferedData(NestedSingleAsyncRe
sult
> castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
>    at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
> NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
>    at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[]
> buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
> state)
>    at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset,
> Int32 size, AsyncCallback callback, Object state)
>    at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
> size)
>    at BIZv1.Helpers.HttpHelper.FileTransfer(String urlpath, String
> destination)
>    at BIZv1.BLL.MoveFilesFromBIZBLL.SaveFileFromBIZ()
>
>
>
>
> public bool FileTransfer(string urlpath, string destination)
> {
>         Stream istream = null;
> FileStream ostream = null;
> WebRequest wreq = WebRequest.Create( urlpath );
> HttpWebResponse response;
> byte[] buffer = new byte[this.m_bufferSize]; // 1024
> int size = m_bufferSize; //1024
>
> try
> {
> response = (HttpWebResponse) wreq.GetResponse();
> istream = response.GetResponseStream();
> long contentLength = Convert.ToInt64(response.ContentLength);
> this.m_results += DateTime.Now.ToString("HH:mm:ss.ffffff") + ":
> Downloading " + contentLength.ToString() + " bytes./n";
> // Always overwrite downloaded file...
> ostream = new FileStream(destination, FileMode.Create,
> FileAccess.Write);
>
> int maxCounter = 2000;
>                 int counter = 0;
>
> // Read and write data
> while (true)
> {
> size = istream.Read(buffer, 0, buffer.Length);
> if (size > 0)
> {
> ostream.Write(buffer, 0, size);
> }
> else
> {
> break;
> }
> counter++;
> if(counter >= maxCounter)
> {
> counter = 0;
> System.Threading.Thread.Sleep(20);
> }
> }
> }
> catch(System.ArgumentOutOfRangeException e1)
> {
> string errorMessage = "Problems with getting\n" + urlpath  + " to
> save to \n" + destination + "\n";
>
>
>         [extraneous exception handling code deleted]
>
>
> Thanks in advance for anyone's help...



Report this thread to moderator Post Follow-up to this message
Old Post
Evan Freeman[C++ Samuri]
06-04-04 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:21 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.