SliceWriter

Namespace: SnowBank.Buffers · struct

Implements: IBufferWriter<byte>, ISliceSerializable, ISpanEncodable, IDisposable

Slice buffer that emulates a pseudo-stream using a byte array that will automatically grow in size, if necessary

Remarks

This struct MUST be passed by reference!

Constructors

SliceWriter

SliceWriter(int capacity)

Create a new empty binary buffer with an initial allocated size

  • capacity — Initial capacity of the buffer

SliceWriter(ArrayPool<byte> pool)

Create a new empty binary buffer with an initial allocated size

  • pool — Pool that will be used to manage the buffers

SliceWriter(byte[] buffer)

Create a new binary writer using an existing buffer

  • buffer — Initial buffer

Since the content of the will be modified, only a temporary or scratch buffer should be used. If the writer needs to grow, a new buffer will be allocated.

SliceWriter(int capacity, ArrayPool<byte> pool)

Create a new empty binary buffer with an initial allocated size

  • capacity — Initial capacity of the buffer
  • pool — Pool that will be used to manage the buffers

SliceWriter(byte[] buffer, int index)

Create a new binary buffer using an existing buffer and with the cursor to a specific location

Since the content of the will be modified, only a temporary or scratch buffer should be used. If the writer needs to grow, a new buffer will be allocated.

SliceWriter(Slice prefix, int capacity = 0)

Creates a new binary buffer, initialized by copying pre-existing data

  • prefix — Data that will be copied at the start of the buffer
  • capacity — Optional initial capacity of the buffer

The cursor will already be placed at the end of the prefix

Properties

Capacity

int Capacity { get; }

Capacity of the internal buffer

HasData

bool HasData { get; }

Returns true if the buffer contains at least some data

Item

byte Item { get; }

Slice Item { get; }

byte Item { get; }

Slice Item { get; }

RemainingCapacity

int RemainingCapacity { get; }

Return the remaining capacity in the current underlying buffer

Methods

Advance

void Advance(int count)

Align

void Align(int alignment, byte pad = 0)

Advance the cursor by the amount required end up on an aligned byte position

  • alignment — Number of bytes to align to
  • pad — Pad value (0 by default)

Allocate

Span<byte> Allocate(int count)

Advance the cursor by the specified amount, and return the skipped over chunk (that can be filled later by the caller)

  • count — Number of bytes to allocate

Returns: Slice that corresponds to the reserved segment in the buffer

Span<byte> Allocate(int count, byte pad)

Advance the cursor by the specified amount, and return the skipped over chunk (that can be filled later by the caller)

  • count — Number of bytes to allocate
  • pad — Pad value (0xFF by default)

Returns: Slice that corresponds to the reserved segment in the buffer

Will fill the reserved segment with and the cursor will be positioned immediately after the segment.

AllocateMemory

Memory<byte> AllocateMemory(int count)

Allocates a buffer at the current cursor location, and advance the cursor

  • count — Number of bytes to allocate

Returns: Buffer located at the current cursor position, and of size equal to count

The buffer must be completely filled, otherwise any pre-existing data in the buffer will leak!

AllocateSpan

Span<byte> AllocateSpan(int count)

Allocates a buffer at the current cursor location, and advance the cursor

  • count — Number of bytes to allocate

Returns: Buffer located at the current cursor position, and of size equal to count

The buffer must be completely filled, otherwise any pre-existing data in the buffer will leak!

AppendBytes

Slice AppendBytes(byte[] data)

Append a byte array to the end of the buffer

Slice AppendBytes(Slice data)

Append a segment of bytes to the end of the buffer

  • data — Buffer containing the data to append

Returns: Slice that maps the interned data using the writer's buffer.

If you do not need the resulting Slice, you should call instead!

Slice AppendBytes(ReadOnlySpan<byte> data)

Append a segment of bytes to the end of the buffer

  • data — Buffer containing the data to append

Returns: Slice that maps the interned data using the writer's buffer.

If you do not need the resulting Slice, you should call instead!

Slice AppendBytes(Span<byte> data)

Append a segment of bytes to the end of the buffer

  • data — Buffer containing the data to append

Returns: Slice that maps the interned data using the writer's buffer.

If you do not need the resulting Slice, you should call instead!

Slice AppendBytes(byte[] data, int offset, int count)

Append a chunk of a byte array to the end of the buffer

Dispose

void Dispose()

Releases the resources allocated by this instance

EnsureBytes

byte[] EnsureBytes(int count)

Ensures that we can fit the specified amount of data at the end of the buffer

  • count — Number of bytes that will be written

If the buffer is too small, it will be resized, and all previously written data will be copied

byte[] EnsureBytes(uint count)

Ensures that we can fit the specified amount of data at the end of the buffer

  • count — Number of bytes that will be written

If the buffer is too small, it will be resized, and all previously written data will be copied

byte[] EnsureBytes(int count, ArrayPool<byte> pool)

Ensures that we can fit the specified amount of data at the end of the buffer

  • count — Number of bytes that will be written

If the buffer is too small, it will be resized, and all previously written data will be copied

EnsureOffsetAndSize

void EnsureOffsetAndSize(int offset, int count)

Ensures that we can fit data at a specific offset in the buffer

  • offset — Offset into the buffer (from the start)
  • count — Number of bytes that will be written at this offset

If the buffer is too small, it will be resized, and all previously written data will be copied

Flush

int Flush(int bytes)

Delete the first N bytes of the buffer, and shift the remaining to the front

  • bytes — Number of bytes to remove at the head of the buffer

Returns: New size of the buffer (or 0 if it is empty)

This should be called after every successful write to the underlying stream, to update the buffer.

GetBufferUnsafe

byte[] GetBufferUnsafe()

Returns the underlying buffer holding the data

This will never return null, unlike which can be null if the instance was never written to.

GetBytes

byte[] GetBytes()

Returns a byte array filled with the contents of the buffer

The buffer is copied in the byte array. And change to one will not impact the other

GetMemory

Memory<byte> GetMemory(int minCapacity)

Allocates a buffer at the current cursor location, but do not advance the cursor

  • minCapacity — Minimum allocated capacity

Returns: Buffer located at the current cursor position, and of size at least equal to minCapacity

After filling the returned buffer, the caller MUST advance the cursor manually!

This is intented to be used in combination with methods like TryFormat

GetSpan

Span<byte> GetSpan(int minCapacity)

Allocates a buffer at the current cursor location, but do not advance the cursor

  • minCapacity — Minimum allocated capacity

Returns: Buffer located at the current cursor position, and of size at least equal to minCapacity

After filling the returned buffer, the caller MUST advance the cursor manually!

This is intended to be used in combination with methods like TryFormat

GrowBuffer

static byte[] GrowBuffer(ref byte[] buffer, int keep, int minimumCapacity, ArrayPool<byte> pool)

Resize a buffer by doubling its capacity

  • buffer — Reference to the variable holding the buffer to create/resize. If null, a new buffer will be allocated. If not, the content of the buffer will be copied into the new buffer.
  • minimumCapacity — Minimum guaranteed buffer size after resizing.
  • pool — Optional pool used by this buffer

The buffer will be resized to the maximum between the previous size multiplied by 2, and . The capacity will always be rounded to a multiple of 16 to reduce memory fragmentation

Slice Head(int count)

Returns a slice pointing to the first count bytes of the buffer

  • count — Size of the segment to return.

Returns: Slice that contains the first count bytes written to this buffer

Any change to the slice will change the buffer !

({HELLO WORLD}).Head(5) => {HELLO}
({HELLO WORLD}).Head(1) => {H}
({HELLO WORLD}).Head(0) => {}

Slice Head(uint count)

Returns a slice pointing to the first count bytes of the buffer

  • count — Size of the segment to return.

Returns: Slice that contains the first count bytes written to this buffer

Any change to the slice will change the buffer !

().Head(5) => ().Head(1) => ().Head(0) =>

PatchByte

void PatchByte(int offset, byte value)

Overwrite a byte of the buffer that was already written

You must ensure that replaced location is before the current position!

void PatchByte(int offset, sbyte value)

Overwrite a byte of the buffer that was already written

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

void PatchByte(int offset, int value)

Overwrite a byte of the buffer that was already written

  • offset — Offset, from the start of the buffer, of the location to patch
  • value — Value that contains the byte to replace. The upper 24-bits are ignored.

You must ensure that replaced location is before the current position!

PatchBytes

void PatchBytes(int index, Slice data)

Overwrite a section of the buffer that was already written, with the specified data

  • index — Offset from the start of the buffer where to start replacing
  • data — Data that will overwrite the buffer at the specified index

You must ensure that replaced section does not overlap with the current position!

void PatchBytes(int index, ReadOnlySpan<byte> data)

Overwrite a section of the buffer that was already written, with the specified data

  • index — Offset from the start of the buffer where to start replacing
  • data — Data that will overwrite the buffer at the specified index

You must ensure that replaced section does not overlap with the current position!

void PatchBytes(int index, ReadOnlyMemory<byte> data)

Overwrite a section of the buffer that was already written, with the specified data

  • index — Offset from the start of the buffer where to start replacing
  • data — Data that will overwrite the buffer at the specified index

You must ensure that replaced section does not overlap with the current position!

void PatchBytes(int index, byte[] buffer, int offset, int count)

Overwrite a section of the buffer that was already written, with the specified data

You must ensure that replaced section does not overlap with the current position!

PatchInt128

void PatchInt128(int offset, Int128 value)

Overwrites a 128-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt128BE

void PatchInt128BE(int offset, Int128 value)

Overwrites a 128-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt16

void PatchInt16(int offset, short value)

Overwrites a 16-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch
  • value — Value that contains the byte to replace. The upper 24-bits are ignored.

You must ensure that replaced location is before the current position!

PatchInt16BE

void PatchInt16BE(int offset, short value)

Overwrites a 16-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt24

void PatchInt24(int offset, int value)

Overwrites a 24-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location. The upper 8-bits are ignored.

You must ensure that replaced location is before the current position!

PatchInt24BE

void PatchInt24BE(int offset, int value)

Overwrites a 24-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location. The upper 8-bits are ignored.

You must ensure that replaced location is before the current position!

PatchInt32

void PatchInt32(int offset, int value)

Overwrites a 32-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt32BE

void PatchInt32BE(int offset, int value)

Overwrites a 32-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt64

void PatchInt64(int offset, long value)

Overwrites a 64-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchInt64BE

void PatchInt64BE(int offset, long value)

Overwrites a 64-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt128

void PatchUInt128(int offset, UInt128 value)

Overwrites a 128-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt128BE

void PatchUInt128BE(int offset, UInt128 value)

Overwrites a 128-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt16

void PatchUInt16(int offset, ushort value)

Overwrites a 16-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt16BE

void PatchUInt16BE(int offset, ushort value)

Overwrites a 16-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt24

void PatchUInt24(int offset, uint value)

Overwrites a 24-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location. The upper 8-bits are ignored.

You must ensure that replaced location is before the current position!

PatchUInt24BE

void PatchUInt24BE(int offset, uint value)

Overwrites a 24-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location. The upper 8-bits are ignored.

You must ensure that replaced location is before the current position!

PatchUInt32

void PatchUInt32(int offset, uint value)

Overwrites a 32-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt32BE

void PatchUInt32BE(int offset, uint value)

Overwrites a 32-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt64

void PatchUInt64(int offset, ulong value)

Overwrites a 64-bit location in the buffer, using little-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUInt64BE

void PatchUInt64BE(int offset, ulong value)

Overwrites a 64-bit location in the buffer, using big-endian encoding

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUuid128

void PatchUuid128(int offset, Uuid128 value)

Overwrites a 128-bit location in the buffer

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUuid64

void PatchUuid64(int offset, Uuid64 value)

Overwrites a 96-bit location in the buffer

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUuid80

void PatchUuid80(int offset, Uuid80 value)

Overwrites a 96-bit location in the buffer

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

PatchUuid96

void PatchUuid96(int offset, Uuid96 value)

Overwrites a 96-bit location in the buffer

  • offset — Offset, from the start of the buffer, of the location to patch.
  • value — Value to insert at this location.

You must ensure that replaced location is before the current position!

Release

void Release(bool clear = false)

Returns the current buffer to the pool

Reset

void Reset(bool shrink = false, bool zeroes = false)

Empties the current buffer after a successful write

  • shrink — If true, release the buffer if it was large and mostly unused. If false, keep the same buffer independent of its current size
  • zeroes — If true, fill the existing buffer with zeroes, if it is reused, to ensure that no previous data can leak.

If the current buffer is large enough, and less than 1/8th was used, then it will be discarded and a new smaller one will be allocated as needed

Rewind

void Rewind(out int cursor, int position)

Rewinds the cursor to a previous position in the buffer, while saving the current position

  • cursor — Will receive the current cursor position
  • position — Previous position in the buffer

SetLength

void SetLength(int position)

Truncate the buffer by setting the cursor to the specified position.

  • position — New size of the buffer

If the buffer was smaller, it will be resized and filled with zeroes. If it was bigger, the cursor will be set to the specified position, but previous data will not be deleted.

Skip

int Skip(int skip, byte pad = 255)

Advance the cursor of the buffer without writing anything, and return the previous position

  • skip — Number of bytes to skip
  • pad — Pad value (0xFF by default)

Returns: Position of the cursor BEFORE moving it. Can be used as a marker to go back later and fill some value

Will fill the skipped bytes with

Substring

Slice Substring(int offset)

Returns a slice pointing to a segment inside the buffer

  • offset — Offset of the segment from the start of the buffer

Any change to the slice will change the buffer !

Slice Substring(Range range)

Returns a slice pointing to a segment inside the buffer

  • range — Range to return

Any change to the slice will change the buffer !

Slice Substring(int offset, int count)

Returns a slice pointing to a segment inside the buffer

  • offset — Offset of the segment from the start of the buffer
  • count — Size of the segment

Any change to the slice will change the buffer !

Tail

Slice Tail(int count)

Returns a slice pointer to the last count bytes of the buffer

  • count — Size of the segment to return.

Returns: Slice that contains the last count bytes written to this buffer

Any change to the slice will change the buffer !

().Tail(5) => ().Tail(1) => ().Tail(0) =>

Slice Tail(uint count)

Returns a slice pointer to the last count bytes of the buffer

  • count — Size of the segment to return.

Returns: Slice that contains the last count bytes written to this buffer

Any change to the slice will change the buffer !

({HELLO WORLD}).Tail(5) => {WORLD}
({HELLO WORLD}).Tail(1) => {D}
({HELLO WORLD}).Tail(0) => {}

ToArraySegment

ArraySegment<byte> ToArraySegment()

Returns a buffer segment pointing to the content of the buffer

Any change to the segment will change the buffer !

ToSlice

Slice ToSlice()

Returns a Slice pointing to the content of the buffer

Any change to the slice will change the buffer !

ToSliceOwner

SliceOwner ToSliceOwner()

Returns a SliceOwner with the content that was written to this writer

The caller MUST dispose the returned instance, otherwise the buffer will not be returned to the pool

The writer is reset to 0, and can be reused immediately

SliceOwner ToSliceOwner(bool clearAfterUse)

Returns a SliceOwner with the content that was written to this writer, with optional clearing of the buffer after use.

  • clearAfterUse — If true, the content of the buffer will be cleared when the returned SliceOwner is disposed

The caller MUST dispose the returned instance, otherwise the buffer will not be returned to the pool

The writer is reset to 0, and can be reused immediately

ToSpan

ReadOnlySpan<byte> ToSpan()

Returns a ReadOnlySpan<byte> pointing to the content of the buffer

UnsafeWriteByte

void UnsafeWriteByte(byte value)

Dangerously writes a single byte at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

UnsafeWriteBytes

void UnsafeWriteBytes(Slice data)

Dangerously write a segment of bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

void UnsafeWriteBytes(ReadOnlySpan<byte> data)

Dangerously write a segment of bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

void UnsafeWriteBytes(byte value1, byte value2)

Dangerously writes two bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

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

Dangerously writes three bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

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

Dangerously write four bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

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

Dangerously write five bytes at the end of the buffer, without any capacity checks!

This method DOES NOT check the buffer capacity before writing, and caller MUST have resized the buffer beforehand! Failure to do so may introduce memory correction (buffer overflow!). This should ONLY be used in performance-sensitive code paths that have been audited thoroughly!

WriteBase10

void WriteBase10(int value)

Writes a base 10 integer

void WriteBase10(long value)

Writes a base 10 integer

void WriteBase10(uint value)

Writes a base 10 integer

void WriteBase10(ulong value)

Writes a base 10 integer

WriteByte

void WriteByte(byte value)

Adds a byte to the end of the buffer, and advance the cursor

  • value — Byte, 8 bits

void WriteByte(char value)

Adds a byte to the end of the buffer, and advance the cursor

  • value — Byte, 8 bits

void WriteByte(int value)

Adds a byte to the end of the buffer, and advance the cursor

  • value — Byte, 8 bits

void WriteByte(sbyte value)

Adds a byte to the end of the buffer, and advance the cursor

  • value — Byte, 8 bits

void WriteByte(bool value)

Adds a 1-byte boolean to the end of the buffer, and advance the cursor

  • value — Boolean, encoded as either 0 or 1.

WriteBytes

void WriteBytes(byte[] data)

Write a byte array to the end of the buffer

void WriteBytes(Slice data)

Write a slice of bytes to the end of the buffer

void WriteBytes(ReadOnlySpan<byte> data)

Write a span of bytes to the end of the buffer

void WriteBytes(byte value1, byte value2)

Writes two bytes at the end of the buffer

void WriteBytes(byte prefix, Slice data)

Write a segment of bytes to the end of the buffer, with a prefix

void WriteBytes(byte prefix, ReadOnlySpan<byte> data)

Write a segment of bytes to the end of the buffer, with a prefix

void WriteBytes(byte prefix, Span<byte> data)

Write a segment of bytes to the end of the buffer, with a prefix

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

Writes three bytes at the end of the buffer

void WriteBytes(byte[] data, int offset, int count)

Write a chunk of a byte array to the end of the buffer

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

Write four bytes at the end of the buffer

void WriteBytes(byte prefix, byte[] data, int offset, int count)

Write a chunk of a byte array to the end of the buffer, with a prefix

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

Writes five bytes at the end of the buffer

WriteDouble

void WriteDouble(double value)

Writes a 64-bit IEEE floating point number, using little-endian encoding

Advances the cursor by 8 bytes

void WriteDouble(byte prefix, double value)

Writes a 64-bit IEEE floating point number, using little-endian encoding, preceded by a single byte

Advances the cursor by 9 bytes

WriteDoubleBE

void WriteDoubleBE(double value)

Writes a 64-bit IEEE floating point number, using big-endian encoding

Advances the cursor by 8 bytes

void WriteDoubleBE(byte prefix, double value)

Writes a 64-bit IEEE floating point number, using little-endian encoding, preceded by a single byte

Advances the cursor by 9 bytes

WriteFixed128

void WriteFixed128(Int128 value)

Writes a 128-bit signed integer, using little-endian encoding

Advances the cursor by 16 bytes

void WriteFixed128(UInt128 value)

Writes a 128-bit unsigned integer, using little-endian encoding

Advances the cursor by 16 bytes

WriteFixed128BE

void WriteFixed128BE(Int128 value)

Writes a 128-bit signed integer, using big-endian encoding

Advances the cursor by 16 bytes

void WriteFixed128BE(UInt128 value)

Writes a 128-bit unsigned integer, using big-endian encoding

Advances the cursor by 16 bytes

WriteFixed16

void WriteFixed16(short value)

Writes a 16-bit unsigned integer, using little-endian encoding

Advances the cursor by 2 bytes

void WriteFixed16(ushort value)

Writes a 16-bit unsigned integer, using little-endian encoding

Advances the cursor by 2 bytes

WriteFixed16BE

void WriteFixed16BE(short value)

Writes a 16-bit signed integer, using big-endian encoding

Advances the cursor by 2 bytes

void WriteFixed16BE(ushort value)

Writes a 16-bit unsigned integer, using big-endian encoding

Advances the cursor by 2 bytes

WriteFixed24

void WriteFixed24(int value)

Writes a 24-bit unsigned integer, using little-endian encoding

  • value — Value to write. The upper 8-bits are ignored.

Advances the cursor by 3 bytes

void WriteFixed24(uint value)

Writes a 24-bit unsigned integer, using little-endian encoding

  • value — Value to write. The upper 8-bits are ignored.

Advances the cursor by 3 bytes

WriteFixed24BE

void WriteFixed24BE(int value)

Writes a 24-bit signed integer, using big-endian encoding

Advances the cursor by 2 bytes

void WriteFixed24BE(uint value)

Writes a 24-bit unsigned integer, using big-endian encoding

Advances the cursor by 3 bytes

WriteFixed32

void WriteFixed32(int value)

Writes a 32-bit signed integer, using little-endian encoding

Advances the cursor by 4 bytes

void WriteFixed32(uint value)

Writes a 32-bit unsigned integer, using little-endian encoding

Advances the cursor by 4 bytes

WriteFixed32BE

void WriteFixed32BE(int value)

Writes a 32-bit signed integer, using big-endian encoding

Advances the cursor by 4 bytes

void WriteFixed32BE(uint value)

Writes a 32-bit unsigned integer, using big-endian encoding

Advances the cursor by 4 bytes

WriteFixed64

void WriteFixed64(long value)

Writes a 64-bit signed integer, using little-endian encoding

Advances the cursor by 8 bytes

void WriteFixed64(ulong value)

Writes a 64-bit unsigned integer, using little-endian encoding

Advances the cursor by 8 bytes

WriteFixed64BE

void WriteFixed64BE(long value)

Writes a 64-bit signed integer, using big-endian encoding

Advances the cursor by 8 bytes

void WriteFixed64BE(ulong value)

Writes a 64-bit unsigned integer, using big-endian encoding

Advances the cursor by 8 bytes

WriteInt128

void WriteInt128(Int128 value)

Writes a 128-bit signed integer, using little-endian encoding

Advances the cursor by 16 bytes

WriteInt128BE

void WriteInt128BE(Int128 value)

Writes a 128-bit signed integer, using big-endian encoding

Advances the cursor by 16 bytes

WriteInt16

void WriteInt16(short value)

Writes a 16-bit unsigned integer, using little-endian encoding

Advances the cursor by 2 bytes

WriteInt16BE

void WriteInt16BE(short value)

Writes a 16-bit signed integer, using big-endian encoding

Advances the cursor by 2 bytes

WriteInt24

void WriteInt24(int value)

Writes a 24-bit unsigned integer, using little-endian encoding

  • value — Value to write. The upper 8-bits are ignored.

Advances the cursor by 3 bytes

WriteInt24BE

void WriteInt24BE(int value)

Writes a 24-bit signed integer, using big-endian encoding

Advances the cursor by 2 bytes

WriteInt32

void WriteInt32(int value)

Writes a 32-bit signed integer, using little-endian encoding

Advances the cursor by 4 bytes

WriteInt32BE

void WriteInt32BE(int value)

Writes a 32-bit signed integer, using big-endian encoding

Advances the cursor by 4 bytes

WriteInt64

void WriteInt64(long value)

Writes a 64-bit signed integer, using little-endian encoding

Advances the cursor by 8 bytes

WriteInt64BE

void WriteInt64BE(long value)

Writes a 64-bit signed integer, using big-endian encoding

Advances the cursor by 8 bytes

WriteSingle

void WriteSingle(float value)

Writes a 32-bit IEEE floating point number, using little-endian encoding

Advances the cursor by 4 bytes

void WriteSingle(byte prefix, float value)

Writes a 32-bit IEEE floating point number, using little-endian encoding, preceded by a single byte

Advances the cursor by 5 bytes

WriteSingleBE

void WriteSingleBE(float value)

Writes a 32-bit IEEE floating point number, using big-endian encoding

Advances the cursor by 4 bytes

void WriteSingleBE(byte prefix, float value)

Writes a 32-bit IEEE floating point number, using big-endian encoding, preceded by a single byte

Advances the cursor by 5 bytes

WriteString

int WriteString(string value)

Write a string using UTF-8

  • value — Text to write

Returns: Number of bytes written

writer.WriteString("Hello, World!")

int WriteString(ReadOnlySpan<byte> value)

Write a string that is already encoded in UTF-8

  • value — Encoded text to write

Returns: Number of bytes written

writer.WriteString("Hello, World!"u8)

int WriteString(ReadOnlySpan<char> value)

Write a string using UTF-8

  • value — Text to write

Returns: Number of bytes written

writer.WriteString("Hello, World!".AsSpan(7))

int WriteString(Utf8String value)

Write an already UTF-8 encoded string

Returns: Number of bytes written

int WriteString(string value, Encoding encoding)

Write a string using the specified encoding

  • value — Text to write
  • encoding — Encoding used to convert the text to bytes

Returns: Number of bytes written

writer.WriteString("Héllô, Wörld!", Encoding.Latin1)

WriteStringAscii

int WriteStringAscii(string value)

Write a string that only contains ASCII

  • value — String with characters only in the 0..127 range

Returns: Number of bytes written

Faster than when writing Magic Strings or ascii keywords

WriteStringUtf8

int WriteStringUtf8(string value)

Write a string using UTF-8

  • value — Text to write

Returns: Number of bytes written

writer.WriteStringUtf8("Hello, World!")

int WriteStringUtf8(ReadOnlySpan<char> chars)

Writes a string using UTF-8

Returns: Number of bytes written

int WriteStringUtf8(char value)

Writes a character using UTF-8

Returns: Number of bytes written

int WriteStringUtf8(char[] chars, int offset, int count)

Write a string using UTF-8

Returns: Number of bytes written

WriteTo

void WriteTo(ref SliceWriter writer)

Appends the content of this writer to end of another writer

WriteUInt128

void WriteUInt128(UInt128 value)

Writes a 128-bit unsigned integer, using little-endian encoding

Advances the cursor by 16 bytes

WriteUInt128BE

void WriteUInt128BE(UInt128 value)

Writes a 128-bit unsigned integer, using big-endian encoding

Advances the cursor by 16 bytes

WriteUInt16

void WriteUInt16(ushort value)

Writes a 16-bit unsigned integer, using little-endian encoding

Advances the cursor by 2 bytes

WriteUInt16BE

void WriteUInt16BE(ushort value)

Writes a 16-bit unsigned integer, using big-endian encoding

Advances the cursor by 2 bytes

WriteUInt24

void WriteUInt24(uint value)

Writes a 24-bit unsigned integer, using little-endian encoding

  • value — Value to write. The upper 8-bits are ignored.

Advances the cursor by 3 bytes

WriteUInt24BE

void WriteUInt24BE(uint value)

Writes a 24-bit unsigned integer, using big-endian encoding

Advances the cursor by 3 bytes

WriteUInt32

void WriteUInt32(uint value)

Writes a 32-bit unsigned integer, using little-endian encoding

Advances the cursor by 4 bytes

WriteUInt32BE

void WriteUInt32BE(uint value)

Writes a 32-bit unsigned integer, using big-endian encoding

Advances the cursor by 4 bytes

WriteUInt64

void WriteUInt64(ulong value)

Writes a 64-bit unsigned integer, using little-endian encoding

Advances the cursor by 8 bytes

WriteUInt64BE

void WriteUInt64BE(ulong value)

Writes a 64-bit unsigned integer, using big-endian encoding

Advances the cursor by 8 bytes

WriteUuid128

void WriteUuid128(in Uuid128 value)

Writes a 128-bit UUID, and advances the cursor

WriteUuid48

void WriteUuid48(Uuid48 value)

Writes a 48-bit UUID, and advances the cursor

WriteUuid64

void WriteUuid64(Uuid64 value)

Writes a 64-bit UUID, and advances the cursor

WriteUuid80

void WriteUuid80(in Uuid80 value)

Writes an 80-bit UUID, and advances the cursor

WriteUuid96

void WriteUuid96(in Uuid96 value)

Writes a 96-bit UUID, and advances the cursor

WriteVarBytes

void WriteVarBytes(byte[] bytes)

Writes a length-prefixed byte array, and advances the cursor

void WriteVarBytes(Slice value)

Writes a length-prefixed byte array, and advances the cursor

void WriteVarBytes(ReadOnlySpan<byte> value)

Writes a length-prefixed byte array, and advances the cursor

void WriteVarBytes(Span<byte> value)

Writes a length-prefixed byte array, and advances the cursor

WriteVarInt128

void WriteVarInt128(UInt128 value)

Writes a 7-bit encoded unsigned 128-bit integer (aka 'Varint128') at the end, and advances the cursor

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

WriteVarString

void WriteVarString(string value, Encoding encoding = null)

Writes a variable-sized string, using the specified encoding

void WriteVarString(ReadOnlySpan<char> value, Encoding encoding = null)

Writes a variable-sized string, using the specified encoding

WriteVarStringAscii

void WriteVarStringAscii(string value)

Writes a variable-sized string, which is known to only contain ASCII characters (0..127)

This is faster than when the caller KNOWS that the string is ASCII only. This should only be used with keywords and constants, NOT with user input!

void WriteVarStringAscii(ReadOnlySpan<char> value)

Writes a variable-sized string, which is known to only contain ASCII characters (0..127)

This is faster than when the caller KNOWS that the string is ASCII only. This should only be used with keywords and constants, NOT with user input!

WriteVarStringUtf8

void WriteVarStringUtf8(string value)

Writes a variable-sized string, encoded using UTF-8

  • value — String to append

The null and empty string will be stored the same way. Caller must use a different technique if they must be stored differently.

void WriteVarStringUtf8(ReadOnlySpan<char> value)

Writes a variable-sized string, encoded using UTF-8

  • value — String to append

The empty string will be stored the same way. Caller must use a different technique if they must be stored differently.

Fields

Buffer

byte[] Buffer

Buffer holding the data

Consider calling to protect against this field being null.

Pool

readonly ArrayPool<byte> Pool

Optional pool used to allocate the buffers used by this writer

If null, will allocate on the heap

Position

int Position

Position in the buffer ( == number of already written bytes)