ArgumentOutOfRangeException on Stream.Read method

B

Brian

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(NestedSingleAsyncResult
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...
 
E

Evan Freeman[C++ Samuri]

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 said:
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(NestedSingleAsyncResult
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top