SpanWriter

Namespace: SnowBank.Buffers · struct

Implements: ISpanEncodable

Helper type for writing binary data into a Span of bytes

Constructors

SpanWriter

SpanWriter(int capacity)

Constructs a SpanWriter with an initial capacity

  • capacity — Capacity of the initial buffer

SpanWriter(Span<byte> buffer)

Constructs a SpanWriter with an already allocated initial buffer

  • buffer — Buffer that should be used by this writer

Properties

Tail

Span<byte> Tail { get; }

Return the rest of the buffer, starting from the current position

Methods

Allocate

Span<byte> Allocate(int count)

Allocate a span of bytes

  • count — Size of the span (in bytes) to allocate.

Returns: Span that maps the corresponding free space in the buffer. The length of the span will be equal to count, even if there are more space available in the buffer.

CopyTo

void CopyTo(Span<byte> destination)

Copies everything that was written so far into a destination span

EnsureBytes

Span<byte> EnsureBytes(int count)

Ensures that the buffer can store count additional bytes

  • count — Number of bytes expected to be written soon.

Returns: Span that maps the corresponding free space in the buffer. The length of the span will be equal to count, even if there are more space available in the buffer.

This does not advance the cursor.

SizeOfVarInt

static int SizeOfVarInt(int value)

Return the size (in bytes) that a 32-bit number would need when encoded as a VarInt

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-5)

ToSpan

ReadOnlySpan<byte> ToSpan()

Returns a span of everything that was written so far

TryCopyTo

bool TryCopyTo(Span<byte> destination)

Copies everything that was written so far into a destination span, if it is large enough

bool TryCopyTo(Span<byte> destination, out int bytesWritten)

Copies everything that was written so far into a destination span, if it is large enough

WriteByte

void WriteByte(byte value)

Adds a byte at the end of the buffer

void WriteByte(sbyte value)

Adds a byte at the end of the buffer

void WriteByte(int value)

Adds a byte at the end of the buffer

Only the lowest 8 bits will be used.

void WriteByte(char value)

Adds a byte at the end of the buffer

Only the lowest 8 bits will be used.

WriteBytes

void WriteBytes(in ReadOnlySpan<byte> bytes)

Adds a span of bytes at the end of the buffer

void WriteBytes(in ReadOnlyMemory<byte> bytes)

Adds a span of bytes at the end of the buffer

void WriteBytes(byte value1, byte value2)

Adds two bytes at the end of the buffer

void WriteBytes(byte value1, byte value2, byte value3)

Adds three bytes at the end of the buffer

void WriteBytes(byte value1, byte value2, byte value3, byte value4)

Adds four bytes at the end of the buffer

void WriteBytes(byte value1, byte value2, byte value3, byte value4, byte value5)

Adds five bytes at the end of the buffer

WriteInt16

void WriteInt16(short value)

Writes a 16-bit little-endian integer at the end of buffer, and advance the cursor by 2.

static void WriteInt16(in Span<byte> span, short value)

Writes a 16-bit little-endian integer at the start of a span of bytes.

WriteInt16BE

void WriteInt16BE(short value)

Writes a 16-bit big-endian integer at the end of buffer, and advance the cursor by 2.

static void WriteInt16BE(in Span<byte> span, short value)

Writes a 16-bit big-endian integer at the start of a span of bytes.

WriteInt32

void WriteInt32(int value)

Writes a 32-bit little-endian integer at the end of buffer, and advance the cursor by 4.

static void WriteInt32(in Span<byte> span, int value)

Writes a 32-bit little-endian integer at the start of a span of bytes.

WriteInt32BE

void WriteInt32BE(int value)

Writes a 32-bit big-endian integer at the end of buffer, and advance the cursor by 4.

static void WriteInt32BE(in Span<byte> span, int value)

Writes a 32-bit big-endian integer at the start of a span of bytes.

WriteInt64

void WriteInt64(long value)

Writes a 64-bit little-endian integer at the end of buffer, and advance the cursor by 8.

static void WriteInt64(in Span<byte> span, long value)

Writes a 64-bit little-endian integer at the start of a span of bytes.

WriteInt64BE

void WriteInt64BE(long value)

Writes a 64-bit big-endian integer at the end of buffer, and advance the cursor by 8.

static void WriteInt64BE(in Span<byte> span, long value)

Writes a 64-bit big-endian integer at the start of a span of bytes.

WriteUInt16

void WriteUInt16(ushort value)

Writes a 16-bit little-endian integer at the end of buffer, and advance the cursor by 2.

static void WriteUInt16(in Span<byte> span, ushort value)

Writes a 16-bit little-endian integer at the start of a span of bytes.

WriteUInt16BE

void WriteUInt16BE(ushort value)

Writes a 16-bit big-endian integer at the end of buffer, and advance the cursor by 2.

static void WriteUInt16BE(in Span<byte> span, ushort value)

Writes a 16-bit big-endian integer at the start of a span of bytes.

WriteUInt32

void WriteUInt32(uint value)

Writes a 32-bit little-endian integer at the end of buffer, and advance the cursor by 4.

static void WriteUInt32(in Span<byte> span, uint value)

Writes a 32-bit little-endian integer at the start of a span of bytes.

WriteUInt32BE

void WriteUInt32BE(uint value)

Writes a 32-bit big-endian integer at the end of buffer, and advance the cursor by 4.

static void WriteUInt32BE(in Span<byte> span, uint value)

Writes a 32-bit big-endian integer at the start of a span of bytes.

WriteUInt64

void WriteUInt64(ulong value)

Writes a 64-bit little-endian integer at the end of buffer, and advance the cursor by 8.

static void WriteUInt64(in Span<byte> span, ulong value)

Writes a 64-bit little-endian integer at the start of a span of bytes.

WriteUInt64BE

void WriteUInt64BE(ulong value)

Writes a 64-bit big-endian integer at the end of buffer, and advance the cursor by 8.

static void WriteUInt64BE(in Span<byte> span, ulong value)

Writes a 64-bit big-endian integer at the start of a span of bytes.

WriteVarInt16

void WriteVarInt16(ushort value)

Writes a 7-bit encoded unsigned int (aka 'Varint16') at the end, and advances the cursor

WriteVarInt32

void WriteVarInt32(uint value)

Writes a 7-bit encoded unsigned int (aka 'Varint32') at the end, and advances the cursor

WriteVarInt64

void WriteVarInt64(ulong value)

Writes a 7-bit encoded unsigned long (aka 'Varint64') at the end, and advances the cursor

WriteVarStringUtf8

void WriteVarStringUtf8(ReadOnlySpan<char> text)

Adds a variable-length string encoded as UTF-8, and preceded by its length encoded as a VarInt32

Fields

Buffer

readonly Span<byte> Buffer

Buffer where to write bytes

Position

int Position

Current position in the buffer