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];

Comments

blog comments powered by Disqus
Next Post
Writing Secure Applications  01 Jan 2004
Prior Post
The Asylum  19 Dec 2003
Related Posts
Types behaving badly  14 Apr 2018
Implementing Value Equality  07 Apr 2018
Implementing Entity Equality  31 Mar 2018
Equality and GetHashCode  24 Mar 2018
Types of Equality  10 Mar 2018
Why is Equality important in .NET?  03 Mar 2018
Invalid Build Configuration: Debug|BNB  14 May 2011
Intersect and Except  24 Jul 2008
Bitmap cloning in .NET  11 Dec 2003
Streams in .NET  20 Nov 2003
Archives
December 2003
2003