ArgumentOutOfRangeException on Stream.Read method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    ArgumentOutOfRangeException on Stream.Read method

    I'm having intermittent trouble with a call to the Read method of
    the HttpWebResponse object. I get an ArumentOutOfRan geException
    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.Argument OutOfRangeExcep tion: Non-negative number required.
    Parameter name: count
    at System.Buffer.B lockCopy(Array src, Int32 srcOffset, Array dst,
    Int32 dstOffset, Int32 count)
    at System.Net.Conn ectStream.FillF romBufferedData (Byte[] buffer,
    Int32& offset, Int32& size)
    at System.Net.Conn ectStream.FillF romBufferedData (NestedSingleAs yncResult
    castedAsyncResu lt, Byte[] buffer, Int32& offset, Int32& size)
    at System.Net.Conn ectStream.Inter nalBeginRead(In t32 bytesToRead,
    NestedSingleAsy ncResult castedAsyncResu lt, Boolean fromCallback)
    at System.Net.Conn ectStream.Begin ReadWithoutVali dation(Byte[]
    buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
    state)
    at System.Net.Conn ectStream.Begin Read(Byte[] buffer, Int32 offset,
    Int32 size, AsyncCallback callback, Object state)
    at System.Net.Conn ectStream.Read( Byte[] buffer, Int32 offset, Int32
    size)
    at BIZv1.Helpers.H ttpHelper.FileT ransfer(String urlpath, String
    destination)
    at BIZv1.BLL.MoveF ilesFromBIZBLL. SaveFileFromBIZ ()




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

    try
    {
    response = (HttpWebRespons e) wreq.GetRespons e();
    istream = response.GetRes ponseStream();
    long contentLength = Convert.ToInt64 (response.Conte ntLength);
    this.m_results += DateTime.Now.To String("HH:mm:s s.ffffff") + ":
    Downloading " + contentLength.T oString() + " bytes./n";
    // Always overwrite downloaded file...
    ostream = new FileStream(dest ination, FileMode.Create ,
    FileAccess.Writ e);

    int maxCounter = 2000;
    int counter = 0;

    // Read and write data
    while (true)
    {
    size = istream.Read(bu ffer, 0, buffer.Length);
    if (size > 0)
    {
    ostream.Write(b uffer, 0, size);
    }
    else
    {
    break;
    }
    counter++;
    if(counter >= maxCounter)
    {
    counter = 0;
    System.Threadin g.Thread.Sleep( 20);
    }
    }
    }
    catch(System.Ar gumentOutOfRang eException 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...
  • Evan Freeman[C++ Samuri]

    #2
    Re: ArgumentOutOfRa ngeException on Stream.Read method

    Hi,

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

    istream.Read(bu ffer, 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.0 406030936.4ddf4 ba9@posting.goo gle.com...[color=blue]
    > I'm having intermittent trouble with a call to the Read method of
    > the HttpWebResponse object. I get an ArumentOutOfRan geException
    > 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.Argument OutOfRangeExcep tion: Non-negative number required.
    > Parameter name: count
    > at System.Buffer.B lockCopy(Array src, Int32 srcOffset, Array dst,
    > Int32 dstOffset, Int32 count)
    > at System.Net.Conn ectStream.FillF romBufferedData (Byte[] buffer,
    > Int32& offset, Int32& size)
    > at[/color]
    System.Net.Conn ectStream.FillF romBufferedData (NestedSingleAs yncResult[color=blue]
    > castedAsyncResu lt, Byte[] buffer, Int32& offset, Int32& size)
    > at System.Net.Conn ectStream.Inter nalBeginRead(In t32 bytesToRead,
    > NestedSingleAsy ncResult castedAsyncResu lt, Boolean fromCallback)
    > at System.Net.Conn ectStream.Begin ReadWithoutVali dation(Byte[]
    > buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
    > state)
    > at System.Net.Conn ectStream.Begin Read(Byte[] buffer, Int32 offset,
    > Int32 size, AsyncCallback callback, Object state)
    > at System.Net.Conn ectStream.Read( Byte[] buffer, Int32 offset, Int32
    > size)
    > at BIZv1.Helpers.H ttpHelper.FileT ransfer(String urlpath, String
    > destination)
    > at BIZv1.BLL.MoveF ilesFromBIZBLL. SaveFileFromBIZ ()
    >
    >
    >
    >
    > public bool FileTransfer(st ring urlpath, string destination)
    > {
    > Stream istream = null;
    > FileStream ostream = null;
    > WebRequest wreq = WebRequest.Crea te( urlpath );
    > HttpWebResponse response;
    > byte[] buffer = new byte[this.m_bufferSi ze]; // 1024
    > int size = m_bufferSize; //1024
    >
    > try
    > {
    > response = (HttpWebRespons e) wreq.GetRespons e();
    > istream = response.GetRes ponseStream();
    > long contentLength = Convert.ToInt64 (response.Conte ntLength);
    > this.m_results += DateTime.Now.To String("HH:mm:s s.ffffff") + ":
    > Downloading " + contentLength.T oString() + " bytes./n";
    > // Always overwrite downloaded file...
    > ostream = new FileStream(dest ination, FileMode.Create ,
    > FileAccess.Writ e);
    >
    > int maxCounter = 2000;
    > int counter = 0;
    >
    > // Read and write data
    > while (true)
    > {
    > size = istream.Read(bu ffer, 0, buffer.Length);
    > if (size > 0)
    > {
    > ostream.Write(b uffer, 0, size);
    > }
    > else
    > {
    > break;
    > }
    > counter++;
    > if(counter >= maxCounter)
    > {
    > counter = 0;
    > System.Threadin g.Thread.Sleep( 20);
    > }
    > }
    > }
    > catch(System.Ar gumentOutOfRang eException 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...[/color]


    Comment

    Working...