C# Class Redzen.IO.MemoryBlockStream

A memory backed stream that stores byte data in blocks, this gives improved performance over System.IO.MemoryStream in some circumstances. MemoryStream is backed by a single byte array, hence if the capacity is reached a new byte array must be instantiated and the existing data copied across. In contrast, MemoryBlockStream grows in blocks and therefore avoids copying and re-instantiating large byte arrays. Also note that by using a sufficiently small block size the blocks will avoid being placed onto the large object heap, with various benefits, e.g. avoidance/mitigation of memory fragmentation.
Inheritance: Stream
Exibir arquivo Open project: colgreen/Redzen Class Usage Examples

Private Properties

Property Type Description
EnsureCapacity void
ReadInner int
WriteInner void
ZeroSpareCapacity void

Public Methods

Method Description
Flush ( ) : void

Overrides Stream.Flush so that no action is performed.

MemoryBlockStream ( ) : System

Construct with the default block size.

MemoryBlockStream ( int blockSize ) : System

Construct with the specified block size.

Read ( byte buffer, int offset, int count ) : int

Reads a block of bytes from the stream and writes the data to a buffer.

ReadByte ( ) : int

Reads a byte from the stream.

Seek ( long offset, SeekOrigin origin ) : long

Sets the position within the stream to the specified value.

SetLength ( long value ) : void

Sets the length of the stream to the specified value.

ToArray ( ) : byte[]

Writes the stream contents to a byte array, regardless of the Position property.

Trim ( ) : void

Remove excess blocks from the block list.

Write ( byte buffer, int offset, int count ) : void

Writes a block of bytes into the stream.

WriteByte ( byte value ) : void

Writes a byte to the stream at the current position.

Private Methods

Method Description
EnsureCapacity ( int value ) : void
ReadInner ( byte buff, int offset, int count, int blockIdx, int blockOffset ) : int

Read bytes from the memory stream into the provided buffer, starting at the specified block index and intra-block offset..

WriteInner ( byte buff, int offset, int count, int blockIdx, int blockOffset ) : void

Write bytes into the memory stream, starting at the specified block index and intra-block offset.

ZeroSpareCapacity ( ) : void

Ensure that any existing capacity after _length is zeroed.

Method Details

Flush() public method

Overrides Stream.Flush so that no action is performed.
public Flush ( ) : void
return void

MemoryBlockStream() public method

Construct with the default block size.
public MemoryBlockStream ( ) : System
return System

MemoryBlockStream() public method

Construct with the specified block size.
public MemoryBlockStream ( int blockSize ) : System
blockSize int The block size to use.
return System

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

ReadByte() public method

Reads a byte from the stream.
public ReadByte ( ) : int
return int

Seek() public method

Sets the position within the stream to the specified value.
public Seek ( long offset, SeekOrigin origin ) : long
offset long The new position within the stream. This is relative to the loc parameter, and can be positive or negative.
origin SeekOrigin A value of type SeekOrigin, which acts as the seek reference point.
return long

SetLength() public method

Sets the length of the stream to the specified value.
public SetLength ( long value ) : void
value long
return void

ToArray() public method

Writes the stream contents to a byte array, regardless of the Position property.
public ToArray ( ) : byte[]
return byte[]

Trim() public method

Remove excess blocks from the block list.
public Trim ( ) : void
return void

Write() public method

Writes a block of bytes into the stream.
public Write ( byte buffer, int offset, int count ) : void
buffer byte The byte data to write into the stream
offset int The zero-based byte offset in buffer from which to begin copying bytes to the current stream.
count int The maximum number of bytes to write.
return void

WriteByte() public method

Writes a byte to the stream at the current position.
public WriteByte ( byte value ) : void
value byte The byte to write.
return void