Stream utilities in the Miscellaneous Utility Library

The System.IO.Stream class is at the heart of IO in the .NET framework. However, it is remarkably easy to abuse it, and some of the operations which are most common in daily life are harder than they need to be. That's where MiscUtil.IO.StreamUtil comes in handy. There are few methods, but there are many overloads to provide a lot of flexibility. All the methods are static, and described below.


This reads to the end of the stream, returning all the data as a byte array. Overloads allow you to provide either your own buffer (as a byte array or an IBuffer) or the size of buffer to use, defaulting to 8K if nothing is specified. The buffer is just used for each cycle of reading (the stream is read in chunks, basically). If the call returns without throwing an exception, the stream will be left at the end of its data.


This copies the contents of one stream to another, it's as simple as that. Again, you can pass in a buffer, the size of buffer to use, or just use the default of creating an 8K buffer. Again, a normally returning call will leave the input stream at the end.


Stream.Read doesn't guarantee that it will read as much data as you've specified, even if it doesn't reach the end of the stream. This is the cause of frequent problems. ReadExactly will read exactly the amount of data you've asked for, or throw an exception. Currently it just returns a new byte array with the data, but future overloads may allow a buffer to be passed in for efficiency of reuse.

Back to the main MiscUtil page.