SpanReader

Namespace: SnowBank.Buffers · struct

Helper type for ready binary data from a Span of bytes

Constructors

SpanReader

SpanReader(in ReadOnlyMemory<byte> buffer)

Constructs a SpanReader that will read from a place in memory

  • buffer — Slice that will be used as the underlying buffer

SpanReader(ReadOnlySpan<byte> buffer)

Constructs a SpanReader that will read from a span of bytes in memory

  • buffer — Span of bytes that will be used as the underlying buffer

Properties

HasMore

bool HasMore { get; }

Returns true if there are more bytes to parse

ReadOnlySpan<byte> Head { get; }

Returns a slice with all the bytes read so far in the buffer

Remaining

int Remaining { get; }

Returns the number of bytes remaining

Tail

ReadOnlySpan<byte> Tail { get; }

Returns a slice with all the remaining bytes in the buffer

Methods

EnsureBytes

void EnsureBytes(int count)

Ensure that there are at least count bytes remaining in the buffer

PeekByte

int PeekByte()

Returns the value of the next byte in the buffer, or -1 if we reached the end

PeekByteAt

int PeekByteAt(int offset)

Returns the value of the byte at a specified offset from the current position, or -1 if this is after the end, or before the start

PeekBytes

ReadOnlySpan<byte> PeekBytes(int count)

Returns the value of the next bytes from the current pointer in the buffer

  • count — Number of bytes to peek

Returns: Span of the next bytes

ReadByte

byte ReadByte()

Read the next byte from the buffer

ReadByteString

ReadOnlySpan<byte> ReadByteString()

Read an encoded nul-terminated byte array from the buffer

ReadBytes

ReadOnlySpan<byte> ReadBytes(int count)

Read the next count bytes from the buffer

ReadOnlySpan<byte> ReadBytes(uint count)

Read the next count bytes from the buffer

ReadDouble

double ReadDouble()

Read the next 8 bytes as an IEEE 64-bit floating point number

ReadEightBytes

ReadOnlySpan<byte> ReadEightBytes()

Read the next 8 bytes from the buffer

ReadFourBytes

ReadOnlySpan<byte> ReadFourBytes()

Read the next 4 bytes from the buffer

ReadGuid

Guid ReadGuid()

Reads a 128-bit Guid

ReadInt16

short ReadInt16()

Read the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian

ReadInt16BE

short ReadInt16BE()

Read the next 2 bytes as a signed 16-bit integer, encoded in big-endian

ReadInt24

int ReadInt24()

Read the next 3 bytes as a signed 24-bit integer, encoded in little-endian

Bits 24 to 31 will sign expanded

ReadInt24BE

int ReadInt24BE()

Read the next 3 bytes as a signed 24-bit integer, encoded in big-endian

Bits 24 to 31 will sign expanded

ReadInt32

int ReadInt32()

Read the next 4 bytes as a signed 32-bit integer, encoded in little-endian

ReadInt32BE

int ReadInt32BE()

Read the next 4 bytes as a signed 32-bit integer, encoded in big-endian

ReadInt64

long ReadInt64()

Read the next 8 bytes as a signed 64-bit integer, encoded in little-endian

ReadInt64BE

long ReadInt64BE()

Read the next 8 bytes as a signed 64-bit integer, encoded in big-endian

ReadSingle

float ReadSingle()

Read the next 4 bytes as an IEEE 32-bit floating point number

ReadSixteenBytes

ReadOnlySpan<byte> ReadSixteenBytes()

Read the next 16 bytes from the buffer

ReadToEnd

ReadOnlySpan<byte> ReadToEnd()

Reads all the remaining bytes in the buffer

Returns: Rest of the buffer

ReadTwoBytes

ReadOnlySpan<byte> ReadTwoBytes()

Read the next 2 bytes from the buffer

ReadUInt16

ushort ReadUInt16()

Read the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian

ReadUInt16BE

ushort ReadUInt16BE()

Read the next 2 bytes as an unsigned 16-bit integer, encoded in big-endian

ReadUInt24

uint ReadUInt24()

Read the next 3 bytes as an unsigned 24-bit integer, encoded in little-endian

Bits 24 to 31 will always be zero

ReadUInt24BE

uint ReadUInt24BE()

Read the next 3 bytes as an unsigned 24-bit integer, encoded in big-endian

ReadUInt32

uint ReadUInt32()

Read the next 4 bytes as an unsigned 32-bit integer, encoded in little-endian

ReadUInt32BE

uint ReadUInt32BE()

Read the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian

ReadUInt64

ulong ReadUInt64()

Read the next 8 bytes as an unsigned 64-bit integer, encoded in little-endian

ReadUInt64BE

ulong ReadUInt64BE()

Read the next 8 bytes as an unsigned 64-bit integer, encoded in big-endian

ReadUuid128

Uuid128 ReadUuid128()

Reads a 128-bit UUID

ReadUuid64

Uuid64 ReadUuid64()

Reads a 64-bit UUID

ReadVarBytes

ReadOnlySpan<byte> ReadVarBytes()

Reads a variable sized slice, by first reading its size (stored as a Varint32) and then the data

ReadVarInt16

ushort ReadVarInt16()

Reads a 7-bit encoded unsigned int (aka 'Varint16') from the buffer, and advances the cursor

Can Read up to 3 bytes from the input

ReadVarInt32

uint ReadVarInt32()

Reads a 7-bit encoded unsigned int (aka 'Varint32') from the buffer, and advances the cursor

Can Read up to 5 bytes from the input

ReadVarInt64

ulong ReadVarInt64()

Reads a 7-bit encoded unsigned long (aka 'Varint32') from the buffer, and advances the cursor

Can Read up to 10 bytes from the input

ReadVarString

string ReadVarString()

Reads an utf-8 encoded string prefixed by a variable-sized length

string ReadVarString(Encoding encoding)

Reads a string prefixed by a variable-sized length, using the specified encoding

Encoding used for this string (or UTF-8 if null)

ReadWhile

ReadOnlySpan<byte> ReadWhile(Func<byte, int, bool> handler)

Read until handler returns true, or we reach the end of the buffer

Skip

void Skip(int count)

Skip the next count bytes of the buffer

TryCopyBytes

bool TryCopyBytes(int count, Span<byte> buffer)

Read the next count bytes from the buffer, unless we already reached the end.

TryPeekBytes

bool TryPeekBytes(int count, out ReadOnlySpan<byte> bytes)

Attempt to peek at the next count bytes from the reader, without advancing the pointer

  • count — Number of bytes to peek
  • bytes — Receives the corresponding slice if there are enough bytes remaining.

Returns: If true, the next count are available in bytes. If false, there are not enough bytes remaining in the buffer.

TryReadByteString

bool TryReadByteString(out ReadOnlySpan<byte> bytes)

Read an encoded nul-terminated byte array from the buffer

TryReadBytes

bool TryReadBytes(int count, out ReadOnlySpan<byte> value)

Read the next count bytes from the buffer, unless we already reached the end.

Fields

Buffer

readonly ReadOnlySpan<byte> Buffer

Buffer containing the bytes to read

Position

int Position

Current position inside the buffer