Redzen.IO.MemoryBlockStream.Read C# (CSharp) Method

Read() public method

Reads a block of bytes from the stream and writes the data to a buffer.
public Read ( byte buffer, int offset, int count ) : int
buffer byte The byte array to read bytes into.
offset int The zero-based byte offset in buffer at which to begin storing data from the current stream.
count int The maximum number of bytes to read.
return int
        public override int Read(byte[] buffer, int offset, int count)
        {
            // Basic checks.
            if(null == buffer) throw new ArgumentNullException("buffer", "Buffer cannot be null.");
            if(offset < 0) throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
            if(count < 0) throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
            if((buffer.Length - offset) < count) {
                throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
            }
            if(!this._isOpen) throw new ObjectDisposedException("Stream is closed.");

            // Test for end of stream (or beyond end)
            if(_position >= _length) {
                return 0;
            }

            // Read bytes into the buffer.
            int blockIdx = _position / _blockSize;
            int blockOffset = _position % _blockSize;
            return ReadInner(buffer, offset, count, blockIdx, blockOffset);
        }