Geeks With Blogs


Microsoft Store

Support This Site

AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License

Scott Dorman Microsoft MVP, Software Architect, Developer, Author

I was working on some Stream extensions today and added a method which will read all of the data from a stream in to a byte array. Some of the classes which derive from Stream provide a similar method (MemoryStream, for example, provides a GetBuffer() method to do this). The drawback here is that the base Stream class doesn’t provide an abstract GetBuffer() method; instead it’s up to each individual derived class to implement such a method. This is perfectly reasonable as a stream may not have an underlying buffer for storage like MemoryStream does.

Taking that into consideration, you read streams the same way. The call to Stream.Read() takes an array of bytes as a buffer to fill during the read operation. It would be nice to be able to get the entire contents of the stream as a byte array.

Looking around I found an excellent blog post by Jon Skeet that provides a few implementations on how to do this. The thing I didn’t like about Jon’s implementation is that it uses Array.Copy in order to resize the array (when there is more data available than the buffer can hold) and to return the properly sized array at the end. (It also has two exit points, which I don’t ordinarily like, but that’s a minor issue.)

The concern with using Array.Copy is that it accesses the elements by index and must be somewhat generic in order to support all of the .NET languages. Since streams tend to be rather large, this introduces unnecessary overhead.

At this point, you might be asking “Why unnecessary overhead? How else do you copy arrays?” This question brings us to System.Buffer, which has been available in .NET since .NET 1.1 although you’ve probably never heard of it until now. A Buffer is designed to manipulate arrays of primitive types and treats each type as a series of bytes without any regard to behavior or limitations. How does this help us? Remember, Stream.Read returns an array of bytes and byte is a primitive type. So we can use Buffer to copy the array elements directly in memory rather than by index, which provides better performance than Array.Copy.

After modifying Jon’s implementation to use Buffer, fixing an issue I noticed (the stream’s position was never reset to 0 prior to reading) and getting rid of that pesky second exit point, my implementation is:

   1: /// <summary>
   2: /// Reads the contents of the stream into a byte array.
   3: /// data is returned as a byte array. An IOException is
   4: /// thrown if any of the underlying IO calls fail.
   5: /// </summary>
   6: /// <param name="stream">The stream to read.</param>
   7: /// <returns>A byte array containing the contents of the stream.</returns>
   8: /// <exception cref="NotSupportedException">The stream does not support reading.</exception>
   9: /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
  10: /// <exception cref="System.IO.IOException">An I/O error occurs.</exception>
  11: public static byte[] ReadAllBytes(this Stream source)
  12: {
  13:     long originalPosition = source.Position;
  14:     source.Position = 0;
  16:     try
  17:     {
  18:         byte[] readBuffer = new byte[4096];
  20:         int totalBytesRead = 0;
  21:         int bytesRead;
  23:         while ((bytesRead = source.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
  24:         {
  25:             totalBytesRead += bytesRead;
  27:             if (totalBytesRead == readBuffer.Length)
  28:             {
  29:                 int nextByte = source.ReadByte();
  30:                 if (nextByte != -1)
  31:                 {
  32:                     byte[] temp = new byte[readBuffer.Length * 2];
  33:                     Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
  34:                     Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
  35:                     readBuffer = temp;
  36:                     totalBytesRead++;
  37:                 }
  38:             }
  39:         }
  41:         byte[] buffer = readBuffer;
  42:         if (readBuffer.Length != totalBytesRead)
  43:         {
  44:             buffer = new byte[totalBytesRead];
  45:             Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
  46:         }
  47:         return buffer;
  48:     }
  49:     finally
  50:     {
  51:         source.Position = originalPosition;
  52:     }
  53: }

It will still generally use at least 2 arrays (if the original array is already the same size as the total number of bytes read, it will not resize it) and sometimes a 3rd array (created inside the while loop) when the stream is larger than the original array. However, by using the Buffer.BlockCopy and Buffer.SetByte rather than the Array methods it should offer better performance.

Technorati Tags: , , ,
DotNetKicks Image
Posted on Saturday, January 10, 2009 3:24 PM .NET (General) , .NET (C#) | Back to top

Copyright © Scott Dorman | Powered by: