473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(Nest edSingleAsyncResult
castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
at System.Net.ConnectStream.BeginReadWithoutValidatio n(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...
Jul 21 '05 #1
1 6362
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" <Br***********@irs.gov> wrote in message
news:c8**************************@posting.google.c om...
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(Nest edSingleAsyncResult castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
at System.Net.ConnectStream.BeginReadWithoutValidatio n(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...

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Amey Samant | last post by:
hi all i was trying to read an integer from keyboard with DataInputStream can someone explain the behaviour of the following code ???? <code> DataInputStream dis=new...
7
by: Dustin B | last post by:
Since this issue encompasses so many things - I have to generally post it I guess. I'm building an application for a Wireless PDA - when it is connected to the Network I tested a TCPListener &...
14
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
1
by: Brian | last post by:
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...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
4
by: Craig Buchanan | last post by:
I am trying to save a MemoryStream (the attachment's Data property) to disk using a FileStream. It seems that the MemoryStream's Read method is the issue. The MemoryStream's size is 6680 bytes...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: In regards to reading a stream, I know we use the Read() method. One could implement reading a stream using a while-loop. while (stream.Read(buffer, 0, buffer.Length) !=...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.