ReadBuffer Class |
Namespace: Lawo.IO
public sealed class ReadBuffer : Buffer
The ReadBuffer type exposes the following members.
Name | Description | |
---|---|---|
ReadBuffer(ReadAsyncCallback, Int32) | Initializes a new instance of the ReadBuffer class. | |
ReadBuffer(ReadCallback, Int32) | Initializes a new instance of the ReadBuffer class. |
Name | Description | |
---|---|---|
Capacity | Gets the number of bytes the buffer can contain. (Inherited from Buffer.) | |
Count | Gets the number of bytes actually contained in the buffer. | |
Index | Gets or sets the current index into the buffer. | |
Item | Gets or sets the byte in the buffer at index. (Inherited from Buffer.) | |
Position | Gets the current position within the stream. |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Fill(Int32) | ||
Fill(Byte, Int32, Int32) | Reads exactly count bytes from the buffer. | |
FillAsync(Int32, CancellationToken) | ||
FillAsync(Byte, Int32, Int32, CancellationToken) | Asynchronously reads exactly count bytes from the buffer. | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Read | Reads bytes into the buffer by calling the callback specified during construction exactly once.
| |
Read(Byte, Int32, Int32) | Reads bytes from the buffer by calling the callback specified during construction at most once.
| |
ReadAsync(CancellationToken) | Asynchronously reads bytes into the buffer by calling the callback specified during construction
exactly once. | |
ReadAsync(Byte, Int32, Int32, CancellationToken) | Asynchronously reads bytes from the buffer by calling the callback specified during construction at
most once. | |
ReadUtf8 | Reads an UTF-8-encoded string from the buffer. | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
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. } }