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
Superpowers for your AI  29 Mar 2026
Autotitling Windows Terminal Tabs  17 Mar 2026
Don't assume shared understanding  25 Jan 2026
Better Table Tests in Go  21 Oct 2025
Error assertions  26 Apr 2025
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
Archives
December 2003
2003