C# Class Lawo.IO.WriteBuffer

Provides a thin wrapper for a buffer that is emptied by calling the provided callback.

The fields in this class and its base are not encapsulated properly for performance reasons. See ReadBuffer for more information.

A frequent use case for this class is when a not previously known number of bytes need to be written to a stream one by one. In this case the following code tends to be much faster than calling Stream.WriteByte for each byte: void WriteToStream(Stream stream) { var writeBuffer = new WriteBuffer(stream.Write, 1024); bool done = false; while (!done && ((writeBuffer.Count < writeBuffer.Capacity) || writeBuffer.Flush())) { // .. byte theByte = 0x00; // Calculate the byte to append to the buffer writeBuffer[writeBuffer.Count++] = theByte; // Set done to true as soon as we're done... } }

Inheritance: Buffer
Show file Open project: Lawo/ember-plus-sharp Class Usage Examples

Public Methods

Method Description
Flush ( ) : bool

Empties the contents of the buffer by calling the callback specified at construction.

After calling this function, Count equals 0.

Reserve ( int size ) : void

Ensures that size <= (Buffer.Capacity - Count).

Performs the following steps: If size > (Buffer.Capacity - Count), then calls Flush. If size > Buffer.Capacity, then enlarges the buffer such that it can hold at least size bytes.

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

Writes the array segment identified by buffer, offset and count to the buffer.

The buffer is flushed as necessary.

WriteBuffer ( WriteCallback write, int bufferSize ) : System

Initializes a new instance of the WriteBuffer class.

Private Methods

Method Description
AssertNotNull ( string value ) : void
FlushAsync ( CancellationToken cancellationToken ) : Task
ReserveAsync ( int size, CancellationToken cancellationToken ) : Task
WriteAsUtf8 ( string value, int byteCount ) : void
WriteAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
WriteBuffer ( WriteAsyncCallback writeAsync, int bufferSize ) : System
WriteToBuffer ( byte buffer, int offset, int count ) : int

Method Details

Flush() public method

Empties the contents of the buffer by calling the callback specified at construction.
After calling this function, Count equals 0.
The object was created by calling /// .
public Flush ( ) : bool
return bool

Reserve() public method

Ensures that size <= (Buffer.Capacity - Count).
Performs the following steps: If size > (Buffer.Capacity - Count), then calls Flush. If size > Buffer.Capacity, then enlarges the buffer such that it can hold at least size bytes.
The object was created by calling /// .
public Reserve ( int size ) : void
size int The minimum number of bytes to reserve.
return void

Write() public method

Writes the array segment identified by buffer, offset and count to the buffer.
The buffer is flushed as necessary.
The length of is less than /// plus . equals null. and/or /// are negative. The object was created by calling /// .
public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void

WriteBuffer() public method

Initializes a new instance of the WriteBuffer class.
equals null. is 0 or negative.
public WriteBuffer ( WriteCallback write, int bufferSize ) : System
write WriteCallback The method that is called when the buffer needs to be emptied.
bufferSize int The size of the buffer in bytes.
return System