GSF.IO.BlockAllocatedMemoryStream.ReadFrom C# (CSharp) Method

ReadFrom() public method

Reads specified number of bytes from source stream into this BlockAllocatedMemoryStream starting at the current position.
The stream is closed.
public ReadFrom ( Stream source, long length ) : void
source Stream The stream containing the data to copy
length long The number of bytes to copy
return void
        public void ReadFrom(Stream source, long length)
        {
            // Note: A faster way would be to write directly to the BlockAllocatedMemoryStream
            if (m_disposed)
                throw new ObjectDisposedException("BlockAllocatedMemoryStream", "The stream is closed.");

            byte[] buffer = MemoryBlockPool.Dequeue();

            do
            {
                int bytesRead = source.Read(buffer, 0, (int)Math.Min(BlockSize, length));

                if (bytesRead == 0)
                    throw new EndOfStreamException();

                length -= bytesRead;
                Write(buffer, 0, bytesRead);
            }
            while (length > 0);

            MemoryBlockPool.Enqueue(buffer);
        }