Class PrefixingBufferWriter<T>
An IBufferWriter<T> that reserves some fixed size for a header.
public class PrefixingBufferWriter<T> : IBufferWriter<T>
Type Parameters
T
The type of element written by this writer.
- Inheritance
-
PrefixingBufferWriter<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
This type is used for inserting the length of list in the header when the length is not known beforehand. It is optimized to minimize or avoid copying.
Constructors
PrefixingBufferWriter(IBufferWriter<T>, int, int, MemoryPool<T>?)
Initializes a new instance of the PrefixingBufferWriter<T> class.
public PrefixingBufferWriter(IBufferWriter<T> innerWriter, int prefixSize, int payloadSizeHint = 0, MemoryPool<T>? memoryPool = null)
Parameters
innerWriter
IBufferWriter<T>The underlying writer that should ultimately receive the prefix and payload.
prefixSize
intThe length of the header to reserve space for. Must be a positive number.
payloadSizeHint
intA hint at the expected max size of the payload. The real size may be more or less than this, but additional copying is avoided if it does not exceed this amount. If 0, a reasonable guess is made.
memoryPool
MemoryPool<T>The memory pool to use for allocating additional memory when the payload exceeds
payloadSizeHint
.
Properties
Length
Gets the sum of all values passed to Advance(int) since the last call to Commit().
public long Length { get; }
Property Value
Prefix
Gets the memory reserved for the prefix.
public Memory<T> Prefix { get; }
Property Value
- Memory<T>
Methods
Advance(int)
Notifies the IBufferWriter<T> that count
data items were written to the output Span<T> or Memory<T>.
public void Advance(int count)
Parameters
Commit()
Commits all the elements written and the prefix to the underlying writer and advances the underlying writer past the prefix and payload.
public void Commit()
Remarks
This instance is safe to reuse after this call.
GetMemory(int)
Returns a Memory<T> to write to that is at least the requested size (specified by sizeHint
).
public Memory<T> GetMemory(int sizeHint = 0)
Parameters
Returns
Exceptions
- OutOfMemoryException
The requested buffer size is not available.
GetSpan(int)
Returns a Span<T> to write to that is at least the requested size (specified by sizeHint
).
public Span<T> GetSpan(int sizeHint = 0)