Table of Contents

Class PrefixingBufferWriter<T>

Namespace
Nerdbank.Streams
Assembly
Nerdbank.Streams.dll

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 int

The length of the header to reserve space for. Must be a positive number.

payloadSizeHint int

A 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

long

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

count int

The number of data items written to the Span<T> or Memory<T>.

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

sizeHint int

The minimum length of the returned Memory<T>. If 0, a non-empty buffer is returned.

Returns

Memory<T>

A Memory<T> of at least the size sizeHint. If sizeHint is 0, returns a non-empty buffer.

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)

Parameters

sizeHint int

The minimum length of the returned Span<T>. If 0, a non-empty buffer is returned.

Returns

Span<T>

A Span<T> of at least the size sizeHint. If sizeHint is 0, returns a non-empty buffer.