C# Class Lawo.IO.ReadBuffer

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

The fields in this class and its base are not encapsulated properly for performance reasons. For example, Buffer.this along with Index should be replaced with a method like GetBufferedByte(). However, doing so costs more than 30% of the throughput in dependent classes when run on the windows phone emulator. This most likely stems form the fact that the CF JIT is only able to inline very simple methods. Apparently, even a method with the one-liner return this.buffer[this.index++]; is not eligible for inlining.

A frequent use case for this class is when a not previously known number of bytes need to be read from a stream one by one. In this case the following code tends to be much faster than calling Stream.ReadByte for each byte: void ReadFromStream(Stream stream) { var readBuffer = new ReadBuffer(stream.Read, 1024); while ((readBuffer.Index < readBuffer.Count) || readBuffer.Read()) { var theByte = readBuffer[readBuffer.Index++]; // Do something with the byte. } }

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

Public Methods

Method Description
Fill ( byte buffer, int offset, int count ) : void

Reads exactly count bytes from the buffer.

If count > Count - Index the callback specified during construction is called as necessary.

Fill ( int count ) : void

Ensures that count <= (Count - Index).

Reads bytes into the buffer by repeatedly calling the callback specified during construction until at least count bytes are available.

Read ( ) : bool

Reads bytes into the buffer by calling the callback specified during construction exactly once.

After each call to this method, Count contains the number of bytes in the buffer and Index equals 0.

This function is usually called when Index equals Count, that is, when the client has processed all bytes in the buffer (the whole buffer is filled in this case). If it is called earlier (when Index < Count) then the remaining bytes at the end of the buffer are first copied to the start of the buffer and the now empty part of the buffer is filled by calling the callback.

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

Reads bytes from the buffer by calling the callback specified during construction at most once.

The callback specified during construction is only called if Index equals Count.

ReadBuffer ( ReadCallback read, int bufferSize ) : System

Initializes a new instance of the ReadBuffer class.

ReadUtf8 ( int count ) : string

Reads an UTF-8-encoded string from the buffer.

First calls Fill(int) to read all the necessary bytes into the buffer.

Private Methods

Method Description
CheckAndAddToCount ( int readCount ) : void
Compact ( ) : void
EnsureSpace ( int count ) : void
FillAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
FillAsync ( int count, CancellationToken cancellationToken ) : Task
FillCore ( int count ) : void
FillCoreAsync ( int count, CancellationToken cancellationToken ) : Task
ReadAsync ( CancellationToken cancellationToken ) : Task
ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
ReadBuffer ( ReadAsyncCallback readAsync, int bufferSize ) : System
ReadFromBuffer ( byte buffer, int offset, int count ) : int

Method Details

Fill() public method

Reads exactly count bytes from the buffer.
If count > Count - Index the callback specified during construction is called as necessary.
The length of is less than /// plus . equals null. and/or /// are negative. The end of the stream has been reached before /// could be filled to bytes. The object was created by calling /// .
public Fill ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void

Fill() public method

Ensures that count <= (Count - Index).
Reads bytes into the buffer by repeatedly calling the callback specified during construction until at least count bytes are available.
The end of the stream has been reached before the buffer could be /// filled to bytes. The object was created by calling /// .
public Fill ( int count ) : void
count int The minimum number of bytes that will be available when this method returns.
return void

Read() public method

Reads bytes into the buffer by calling the callback specified during construction exactly once.

After each call to this method, Count contains the number of bytes in the buffer and Index equals 0.

This function is usually called when Index equals Count, that is, when the client has processed all bytes in the buffer (the whole buffer is filled in this case). If it is called earlier (when Index < Count) then the remaining bytes at the end of the buffer are first copied to the start of the buffer and the now empty part of the buffer is filled by calling the callback.

The object was created by calling /// .
public Read ( ) : bool
return bool

Read() public method

Reads bytes from the buffer by calling the callback specified during construction at most once.
The callback specified during construction is only called if Index equals Count.
The length of is less than /// plus . equals null. and/or /// are negative. The object was created by calling /// .
public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int

ReadBuffer() public method

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

ReadUtf8() public method

Reads an UTF-8-encoded string from the buffer.
First calls Fill(int) to read all the necessary bytes into the buffer.
is negative. The end of the stream has been reached before the /// the buffer could be filled to bytes. The object was created by calling /// .
public ReadUtf8 ( int count ) : string
count int The number of bytes the UTF-8 representation occupies in the buffer.
return string