MemoryStream implementation that deals with pooling and managing memory streams which use potentially large buffers.
This class works in tandem with the RecylableMemoryStreamManager to supply MemoryStream objects to callers, while avoiding these specific problems: 1. LOH allocations - since all large buffers are pooled, they will never incur a Gen2 GC 2. Memory waste - A standard memory stream doubles its size when it runs out of room. This leads to continual memory growth as each stream approaches the maximum allowed size. 3. Memory copying - Each time a MemoryStream grows, all the bytes are copied into new buffers. This implementation only copies the bytes when GetBuffer is called. 4. Memory fragmentation - By using homogenous buffer sizes, it ensures that blocks of memory can be easily reused. The stream is implemented on top of a series of uniformly-sized blocks. As the stream's length grows, additional blocks are retrieved from the memory manager. It is these blocks that are pooled, not the stream object itself. The biggest wrinkle in this implementation is when GetBuffer() is called. This requires a single contiguous buffer. If only a single block is in use, then that block is returned. If multiple blocks are in use, we retrieve a larger buffer from the memory manager. These large buffers are also pooled, split by size--they are multiples of a chunk size (1 MB by default). Once a large buffer is assigned to the stream the blocks are NEVER again used for this stream. All operations take place on the large buffer. The large buffer can be replaced by a larger buffer from the pool as needed. All blocks and large buffers are maintained in the stream until the stream is disposed (unless AggressiveBufferReturn is enabled in the stream manager).
Inheritance: System.IO.MemoryStream
 /// <summary>
 /// Retrieve a new MemoryStream object with the given tag and with contents copied from the provided
 /// buffer. The provided buffer is not wrapped or used after construction.
 /// </summary>
 /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
 /// <param name="tag">A tag which can be used to track the source of the stream.</param>
 /// <param name="buffer">The byte buffer to copy data from.</param>
 /// <param name="offset">The offset from the start of the buffer to copy from.</param>
 /// <param name="count">The number of bytes to copy from the buffer.</param>
 /// <returns>A MemoryStream.</returns>
 //[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
 public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
 {
     var stream = new RecyclableMemoryStream(this, tag, count);
     stream.Write(buffer, offset, count);
     stream.Position = 0;
     return stream;
 }