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

ToArray() public method

Writes the stream contents to a byte array, regardless of the Position property.
public ToArray ( ) : byte[]
return byte[]
        public byte[] ToArray()
        {
            // Allocate new byte array.
            byte[] buff = new byte[_length];
            int buffIdx = 0;

            // Calc number of full blocks.
            int fullBlockCount = _length / _blockSize;

            // Loop full blocks, copying them into buff as we go.
            for(int i=0; i<fullBlockCount; i++)
            {
                byte[] blk = _blockList[i];
                Array.Copy(blk, 0, buff, buffIdx, _blockSize);
                buffIdx += _blockSize;
            }

            // Handle final block possibly/probably partially filled.
            int tailCount = _length % _blockSize;
            if(0 != tailCount)
            {
                byte[] blk = _blockList[fullBlockCount];
                Array.Copy(blk, 0, buff, buffIdx, tailCount);
            }

            return buff;
        }

Usage Example

Exemplo n.º 1
0
 /// <summary>
 /// Read stream into byte array. Reads until the end of stream is reached and returns entire stream contents
 /// as a new byte array.
 /// </summary>
 /// <param name="stream">The stream to read data from.</param>
 /// <returns>Returns a new byte array containing the read data.</returns>
 public static byte[] ReadToByteArray(Stream stream)
 {
     using (MemoryBlockStream ms = new MemoryBlockStream())
     {
         stream.CopyTo(ms);
         return(ms.ToArray());
     }
 }
All Usage Examples Of Redzen.IO.MemoryBlockStream::ToArray