473,385 Members | 1,275 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,385 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 6361
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.