Click or drag to resize
ReadBuffer Class
Provides a thin wrapper for a buffer that is filled by calling the provided callback.
Inheritance Hierarchy

Namespace: Lawo.IO
Assembly: Lawo (in Lawo.dll) Version: 1.4.1707.27006
Syntax
C#
public sealed class ReadBuffer : Buffer

The ReadBuffer type exposes the following members.

Constructors
  NameDescription
Public methodReadBuffer(ReadAsyncCallback, Int32)
Initializes a new instance of the ReadBuffer class.
Public methodReadBuffer(ReadCallback, Int32)
Initializes a new instance of the ReadBuffer class.
Top
Properties
  NameDescription
Public propertyCapacity
Gets the number of bytes the buffer can contain.
(Inherited from Buffer.)
Public propertyCount
Gets the number of bytes actually contained in the buffer.
Public propertyIndex
Gets or sets the current index into the buffer.
Public propertyItem
Gets or sets the byte in the buffer at index.
(Inherited from Buffer.)
Public propertyPosition
Gets the current position within the stream.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodFill(Int32)
Ensures that count <= (Count - Index).
Public methodFill(Byte, Int32, Int32)
Reads exactly count bytes from the buffer.
Public methodFillAsync(Int32, CancellationToken)
Asynchronously ensures that count <= (Count - Index).
Public methodFillAsync(Byte, Int32, Int32, CancellationToken)
Asynchronously reads exactly count bytes from the buffer.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodRead
Reads bytes into the buffer by calling the callback specified during construction exactly once.
Public methodRead(Byte, Int32, Int32)
Reads bytes from the buffer by calling the callback specified during construction at most once.
Public methodReadAsync(CancellationToken)
Asynchronously reads bytes into the buffer by calling the callback specified during construction exactly once.
Public methodReadAsync(Byte, Int32, Int32, CancellationToken)
Asynchronously reads bytes from the buffer by calling the callback specified during construction at most once.
Public methodReadUtf8
Reads an UTF-8-encoded string from the buffer.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

The fields in this class and its base are not encapsulated properly for performance reasons. For example, ItemInt32 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 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.
    }
}

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also