ServiceStack.Text.RecyclableMemoryStream.Write C# (CSharp) Method

Write() public method

Writes the buffer to the stream
buffer is null offset or count is negative buffer.Length - offset is not less than count Object has been disposed
public Write ( byte buffer, int offset, int count ) : void
buffer byte Source buffer
offset int Start position
count int Number of bytes to write
return void
        public override void Write(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", offset, "Offset must be in the range of 0 - buffer.Length-1");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", count, "count must be non-negative");
            }

            if (count + offset > buffer.Length)
            {
                throw new ArgumentException("count must be greater than buffer.Length - offset");
            }

            int blockSize = this.memoryManager.BlockSize;
            long end = (long)this.position + count;
            // Check for overflow
            if (end > MaxStreamLength)
            {
                throw new IOException("Maximum capacity exceeded");
            }

            long requiredBuffers = (end + blockSize - 1) / blockSize;

            if (requiredBuffers * blockSize > MaxStreamLength)
            {
                throw new IOException("Maximum capacity exceeded");
            }

            this.EnsureCapacity((int)end);

            if (this.largeBuffer == null)
            {
                int bytesRemaining = count;
                int bytesWritten = 0;
                var blockAndOffset = this.GetBlockAndRelativeOffset(this.position);

                while (bytesRemaining > 0)
                {
                    byte[] currentBlock = this.blocks[blockAndOffset.Block];
                    int remainingInBlock = blockSize - blockAndOffset.Offset;
                    int amountToWriteInBlock = Math.Min(remainingInBlock, bytesRemaining);

                    Buffer.BlockCopy(buffer, offset + bytesWritten, currentBlock, blockAndOffset.Offset, amountToWriteInBlock);

                    bytesRemaining -= amountToWriteInBlock;
                    bytesWritten += amountToWriteInBlock;

                    ++blockAndOffset.Block;
                    blockAndOffset.Offset = 0;
                }
            }
            else
            {
                Buffer.BlockCopy(buffer, offset, this.largeBuffer, this.position, count);
            }
            this.position = (int)end;
            this.length = Math.Max(this.position, this.length);
        }

Usage Example

 /// <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;
 }