It’s inevitable. Eventually every .NET developer will end up working with a Stream object. Either to accept a file uploaded through a form, to retrieve file data from a database, or to keep some data in memory for some temporary processing. And they suck.
You’ll need to buffer streams, convert them to byte arrays, move through them, move them around, copy them .. it’s a pain, really.
Here’s how I deal with it:
Drop that class anywhere in your project. That’ll take care of copying a stream and getting the data out of a stream and into a byte array.
KNOWN LIMITATION: Both of these methods will handle a max of 2GB of data. If you’re dealing with more data, you’ll need to modify them to buffer the 2GB max at a time.