473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6374
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.