WriteBuffer Class |
Namespace: Lawo.IO
public sealed class WriteBuffer : Buffer
The WriteBuffer type exposes the following members.
Name | Description | |
---|---|---|
WriteBuffer(WriteAsyncCallback, Int32) | Initializes a new instance of the WriteBuffer class. | |
WriteBuffer(WriteCallback, Int32) | Initializes a new instance of the WriteBuffer class. |
Name | Description | |
---|---|---|
Capacity | Gets the number of bytes the buffer can contain. (Inherited from Buffer.) | |
Count | Gets or sets the number of bytes actually contained in the buffer. | |
Item | Gets or sets the byte in the buffer at index. (Inherited from Buffer.) |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Flush | Empties the contents of the buffer by calling the callback specified at construction. | |
FlushAsync | Asynchronously empties the contents of the buffer by calling the callback specified at
construction. | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Reserve | ||
ReserveAsync | ||
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Write | Writes the array segment identified by buffer, offset and
count to the buffer. | |
WriteAsUtf8 | Writes the UTF-8 representation of value to the buffer. | |
WriteAsync | Asynchronously writes the array segment identified by buffer,
offset and count to the buffer. |
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 WriteByte(Byte) 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... } }