Table of Contents

Class PooledBufferWriter

Namespace
DotBoxD.Services.Buffers
Assembly
DotBoxD.Services.dll

An IBufferWriter<T> backed by an array rented from ArrayPool<T>. Either hand the written bytes off via DetachPayload() or release them via Dispose() — never both.

public sealed class PooledBufferWriter : IBufferWriter<byte>, IDisposable
Inheritance
PooledBufferWriter
Implements
Inherited Members

Constructors

PooledBufferWriter(int)

public PooledBufferWriter(int initialCapacity = 256)

Parameters

initialCapacity int

Properties

WrittenCount

The number of bytes written so far.

public int WrittenCount { get; }

Property Value

int

WrittenMemory

The bytes written so far.

public ReadOnlyMemory<byte> WrittenMemory { get; }

Property Value

ReadOnlyMemory<byte>

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>.

DetachPayload()

Hands the rented array and written length to a new Payload and relinquishes ownership. The writer must not be used afterward.

public Payload DetachPayload()

Returns

Payload

Dispose()

Returns the rented array to the pool. A no-op after DetachPayload().

public void Dispose()

GetMemory(int)

Returns a Memory<T> to write to that is at least the requested size (specified by sizeHint).

public Memory<byte> 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<byte>

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<byte> 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<byte>

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