PC Review


Reply
Thread Tools Rate Thread

Data from one stream to another

 
 
7elephants
Guest
Posts: n/a
 
      22nd Jun 2006
I have the following piece of code to take data from one stream and put
it into another...


Int32 bufferSize = 100;
Int32.TryParse(ConfigurationManager.AppSettings["bufferLimit"].ToString(),
out bufferSize);

byte[] buffer = new byte[bufferSize];

//make sure stream is at the beginning
inputStream.Position = 0;

while (true) {
//read input stream
int bytesRead = inputStream.Read(buffer, offset,
bufferSize);

if (bytesRead > 0) {
//write to output stream
outputStream.Write(buffer, offset, bytesRead);
offset += bytesRead;
} else {
break;
}
}
+--------------------------------------------------------------+

When I run the code above, I get a System.ArgumentException with the
following message "Offset and length were out of bounds for the array
or count is greater than the number of elements from index to the end
of the source collection." In the test file I am using, all the data
is read in the first read, but I can't be guaranteed that when in
PRODUCTION mode. I also can't be guaranteed that the input stream is
text-based (or else I would use readers and writers).

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?

In my research before posting I found
http://www.geocities.com/firepower_5...stem-Read.html,
but I don't necessary understand how to correct the problem.

Thanks for any help in advance.

 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      22nd Jun 2006
"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/
 
Reply With Quote
 
7elephants
Guest
Posts: n/a
 
      22nd Jun 2006
That was it. Thanks.


Barry Kelly wrote:
> "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/


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FTP Vs Data stream Nash Microsoft C# .NET 3 18th Jan 2009 03:51 PM
Bloomberg overwrites data-need to save stream of data in a new she Melanie Microsoft Excel Worksheet Functions 2 27th Dec 2007 05:42 PM
Determining if data is available on a stream Henning Friese Microsoft Dot NET 1 12th Jul 2007 10:45 AM
Stream data from IIS server RahulatCSE@gmail.com Microsoft C# .NET 2 9th Jul 2006 11:53 PM
what is data stream mp2 tmixon Microsoft Windows 2000 Multimedia 1 5th Sep 2003 06:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:16 AM.