ArraySliceWriter

Namespace: SnowBank.Buffers · class

Implements: ISliceBufferWriter, IBufferWriter<byte>, ISpanEncodable

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

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

If all data allocated from this writer is guaranteed to not be used outside its lifetime, consider using PooledSliceWriter for performance reasons.

Constructors

ArraySliceWriter

ArraySliceWriter()

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

ArraySliceWriter(int initialCapacity)

Creates an instance of an ArraySliceWriter, 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 content of the buffer into the specified destination

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

Copies the content of the buffer into the specified destination

Dispose

void Dispose()

Releases the memory allocated by this instance

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 an 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 content of the buffer into the specified destination, if it is large enough

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

Copies the content of the buffer into the specified destination, if it is large enough