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
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
December 2003
2003