13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Life before extension methods257System.IO.Stream. The Stream class is the bedrock of b<strong>in</strong>ary communications <strong>in</strong> .NET.Stream itself is an abstract class with several concrete derived classes, such as Network-Stream, FileStream, and MemoryStream. Unfortunately, there are a few pieces of functionalitythat would have been handy to <strong>in</strong>clude <strong>in</strong> Stream but that just aren’t there.The “miss<strong>in</strong>g features” I come across most often are the ability to read the whole ofa stream <strong>in</strong>to memory as a byte array, and the ability to copy 1 the contents of onestream <strong>in</strong>to another. Both of these are frequently implemented badly, mak<strong>in</strong>g assumptionsabout streams that just aren’t valid (the most common is that Stream.Read willcompletely fill the given buffer if the data doesn’t run out first).It would be nice to have the functionality <strong>in</strong> a s<strong>in</strong>gle place, rather than duplicat<strong>in</strong>git <strong>in</strong> several projects. That’s why I wrote the StreamUtil class <strong>in</strong> my miscellaneous utilitylibrary. The real code conta<strong>in</strong>s a fair amount of error check<strong>in</strong>g and other functionality,but list<strong>in</strong>g 10.1 shows a cut-down version that is more than adequate for our needs.List<strong>in</strong>g 10.1A simple utility class to provide extra functionality for streamsus<strong>in</strong>g System.IO;public static class StreamUtil{const <strong>in</strong>t BufferSize = 8192;}public static void Copy(Stream <strong>in</strong>put,Stream output){byte[] buffer = new byte[BufferSize];<strong>in</strong>t read;while ((read = <strong>in</strong>put.Read(buffer, 0, buffer.Length)) > 0){output.Write(buffer, 0, read);}}public static byte[] ReadFully(Stream <strong>in</strong>put){us<strong>in</strong>g (MemoryStream tempStream = new MemoryStream()){Copy(<strong>in</strong>put, tempStream);return tempStream.ToArray();}}The implementation details don’t matter much, although it’s worth not<strong>in</strong>g that theReadFully method calls the Copy method—that will be useful to demonstrate a po<strong>in</strong>tabout extension methods later. The class is easy to use—list<strong>in</strong>g 10.2 shows how we canwrite a web response to disk, for example.1Due to the nature of streams, this “copy<strong>in</strong>g” doesn’t necessarily duplicate the data—it just reads it from onestream and writes it to another. Although “copy” isn’t a strictly accurate term <strong>in</strong> this sense, the difference isusually irrelevant.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!