In my previous entry I wrote about how .net streams don’t have a built in way to copy from one stream to another.

I also said “the function is easy enough to implement (there is even an example buried in the help file, if you can but find it)”.

Someone pointed out to me that it was unhelpful to not then go on and point out how to find it.

So, straight from the documentation for Stream.Write, here is the code:

const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
    output.Write(bytes, 0, numBytes);

While we’re here - notice the subtle bug in the second line, which really should be using the constant:

byte[] bytes = new byte[size];


blog comments powered by Disqus