"7elephants" <(E-Mail Removed)> wrote:
> I have the following piece of code to take data from one stream and put
> it into another...
[snip]
> Int32.TryParse(ConfigurationManager.AppSettings["bufferLimit"].ToString(),
> out bufferSize);
[snip]
> inputStream.Position = 0;
[snip]
> int bytesRead = inputStream.Read(buffer, offset,
> bufferSize);
[snip]
> outputStream.Write(buffer, offset, bytesRead);
> offset += bytesRead;
This offset parameter to both Stream.Read and Stream.Write indicate to
these methods at what index in the buffer to read or write. The slice of
the array, buffer[offset..offset+count-1] inclusive, needs to be valid.
You're increasing the offset, but you're never decreasing it. Eventually
this slice runs off the end of your buffer.
> So, my two questions are 1) Can anybody tell me when I am getting this
> error? and 2) Is there a better way for me to do this?
Here's a copy routine I wrote a long time ago:
---8<---
public static void CopyStream(Stream source, Stream dest)
{
byte[] buffer = new byte[65536];
int read;
do
{
read = source.Read(buffer, 0, buffer.Length);
dest.Write(buffer, 0, read);
} while (read != 0);
}
--->8---
Setting the Position to 0 might not be desirable; for one thing, the
stream might not be seekable (Stream.CanSeek will tell you). I have an
overloaded CopyStream that can specify an 'int count' argument to limit
the number of bytes copied.
I'm not sure you gain a lot by making the buffer configurable; depending
on the stream backing store, it can make sense to make the buffer
relatively large (for example, the stream might be a NetworkStream over
a TCP socket which has the Nagle algorithm turned off (NoDelay = true)),
but not large enough to land in the large object heap (80 KB or so), so
that it's still cheap to GC.
-- Barry
--
http://barrkel.blogspot.com/