PooledSliceWriter

Namespace: SnowBank.Buffers · class

Implements: ISliceBufferWriter, IBufferWriter<byte>, IDisposable

Buffer writer that writes all data into a single consecutive buffer allocated from a pool

Remarks

This buffer will grow and copy the underlying array as needed. For performance reason, prefer using SlabSliceWriter if you don't require the final output to be consecutive in memory

All allocated memory will be returned to the pool when this instance is disposed.

All slices allocated from this instance MUST NOT be used after that instance has beed disposed or cleared! Consider using ArraySliceWriter if you need allocated data to survive this instance.

Constructors

PooledSliceWriter

PooledSliceWriter()

Creates an instance of an PooledSliceWriter, in which data can be written to, with the default initial capacity.

PooledSliceWriter(ArrayPool<byte> pool)

Creates an instance of an PooledSliceWriter, in which data can be written to, with the default initial capacity.

PooledSliceWriter(int initialCapacity, ArrayPool<byte> pool)

Creates an instance of an PooledSliceWriter, in which data can be written to, with an initial capacity specified.

  • initialCapacity — The minimum capacity with which to initialize the underlying buffer.

Properties

Capacity

int Capacity { get; }

Returns the total amount of space within the underlying buffer.

FreeCapacity

int FreeCapacity { get; }

Returns the amount of space available that can still be written into without forcing the underlying buffer to grow.

WrittenCount

int WrittenCount { get; }

Returns the amount of data written to the underlying buffer so far.

WrittenSlice

Slice WrittenSlice { get; }

Returns the data written to the underlying buffer so far, as a Slice.

WrittenSpan

ReadOnlySpan<byte> WrittenSpan { get; }

Returns the data written to the underlying buffer so far, as a ReadOnlyMemory.

Methods

Advance

void Advance(int count)

Notifies the ISliceBufferWriter that count data items were written to the output Slice.

  • count — The number of data items written to the Slice.

Clear

void Clear()

Clears the data written to the underlying buffer.

You must clear the before trying to re-use it.

CopyTo

void CopyTo(Span<byte> output)

Copies the contents of this buffer to the specified destination

void CopyTo(Span<byte> output, out int written)

Copies the contents of this buffer to the specified destination

Dispose

void Dispose()

Clears the writer, and returns the buffer to the pool

GetMemory

Memory<byte> GetMemory(int sizeHint = 0)

Returns a Memory to write to that is at least the requested length (specified by sizeHint). If no sizeHint is provided (or it's equal to 0), some non-empty buffer is returned.

This will never return an empty .

GetSlice

ArraySegment<byte> GetSlice(int sizeHint = 0)

Returns a ArraySegment to write to that is at least the requested size (specified by sizeHint).

  • sizeHint — The minimum length of the returned Slice. If 0, a non-empty buffer is returned.

Returns: A ArraySegment of at least the size sizeHint. If sizeHint is 0, returns a non-empty buffer.

If the requested size exceeds the , the internal buffer will be resized

GetSpan

Span<byte> GetSpan(int sizeHint = 0)

Returns a Span to write to that is at least the requested length (specified by sizeHint). If no sizeHint is provided (or it's equal to 0), some non-empty buffer is returned.

This will never return an empty .

TryCopyTo

bool TryCopyTo(Span<byte> output)

Copies the contents of this buffer to the specified destination, if it is large enough

bool TryCopyTo(Span<byte> output, out int written)

Copies the contents of this buffer to the specified destination, if it is large enough