Skip to content

Struct BufferSpan

Namespace: Void.Minecraft.Buffers
Assembly: Void.Minecraft.dll

Manages a span of bytes, allowing access and manipulation of its position within the buffer.

public ref struct BufferSpan : IMinecraftBuffer<BufferSpan>, IReadMinecraftBuffer, IWriteMinecraftBuffer, ICommonMinecraftBuffer, IDisposable

Implements

IMinecraftBuffer<BufferSpan>, IReadMinecraftBuffer, IWriteMinecraftBuffer, ICommonMinecraftBuffer, IDisposable

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ReadMinecraftBufferExtensions.Dump<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.Read<BufferSpan>(ref BufferSpan, int), ReadMinecraftBufferExtensions.ReadBoolean<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadByteArray<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadComponent<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadDouble<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadFloat<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadInt<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadIntArray<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadLong<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadProperty<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadPropertyArray<BufferSpan>(ref BufferSpan, int), ReadMinecraftBufferExtensions.ReadShort<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadString<BufferSpan>(ref BufferSpan, int), ReadMinecraftBufferExtensions.ReadTag<BufferSpan>(ref BufferSpan, bool), ReadMinecraftBufferExtensions.ReadToEnd<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadUnsignedByte<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadUnsignedShort<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadUuid<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadUuidAsIntArray<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadVarInt<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadVarIntArray<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadVarLong<BufferSpan>(ref BufferSpan), ReadMinecraftBufferExtensions.ReadVarShort<BufferSpan>(ref BufferSpan), WriteMinecraftBufferExtensions.Write<BufferSpan>(ref BufferSpan, scoped ReadOnlySpan<byte>), WriteMinecraftBufferExtensions.Write<BufferSpan>(ref BufferSpan, Stream), WriteMinecraftBufferExtensions.WriteBoolean<BufferSpan>(ref BufferSpan, bool), WriteMinecraftBufferExtensions.WriteByteArray<BufferSpan>(ref BufferSpan, scoped ReadOnlySpan<byte>), WriteMinecraftBufferExtensions.WriteComponent<BufferSpan>(ref BufferSpan, Component), WriteMinecraftBufferExtensions.WriteDouble<BufferSpan>(ref BufferSpan, double), WriteMinecraftBufferExtensions.WriteFloat<BufferSpan>(ref BufferSpan, float), WriteMinecraftBufferExtensions.WriteInt<BufferSpan>(ref BufferSpan, int), WriteMinecraftBufferExtensions.WriteIntArray<BufferSpan>(ref BufferSpan, scoped ReadOnlySpan<int>), WriteMinecraftBufferExtensions.WriteLong<BufferSpan>(ref BufferSpan, long), WriteMinecraftBufferExtensions.WriteProperty<BufferSpan>(ref BufferSpan, Property), WriteMinecraftBufferExtensions.WritePropertyArray<BufferSpan>(ref BufferSpan, Property[]?), WriteMinecraftBufferExtensions.WriteShort<BufferSpan>(ref BufferSpan, short), WriteMinecraftBufferExtensions.WriteString<BufferSpan>(ref BufferSpan, ReadOnlySpan<char>), WriteMinecraftBufferExtensions.WriteTag<BufferSpan>(ref BufferSpan, NbtTag, bool), WriteMinecraftBufferExtensions.WriteUnsignedByte<BufferSpan>(ref BufferSpan, byte), WriteMinecraftBufferExtensions.WriteUnsignedShort<BufferSpan>(ref BufferSpan, ushort), WriteMinecraftBufferExtensions.WriteUuid<BufferSpan>(ref BufferSpan, Uuid), WriteMinecraftBufferExtensions.WriteUuidAsIntArray<BufferSpan>(ref BufferSpan, Uuid), WriteMinecraftBufferExtensions.WriteVarInt<BufferSpan>(ref BufferSpan, int), WriteMinecraftBufferExtensions.WriteVarIntArray<BufferSpan>(ref BufferSpan, scoped ReadOnlySpan<int>), WriteMinecraftBufferExtensions.WriteVarLong<BufferSpan>(ref BufferSpan, long), WriteMinecraftBufferExtensions.WriteVarShort<BufferSpan>(ref BufferSpan, int)

Constructors

BufferSpan(Span<byte>)

Initializes a new instance of the BufferSpan class with a byte span.

public BufferSpan(Span<byte> source)

Parameters

source Span<byte>

The byte span provides the data to be managed within the BufferSpan instance.

Properties

Length

Returns the length of the source as an integer. It is a read-only property.

public readonly int Length { get; }

Property Value

int

Position

Gets or sets the position within a buffer. The position must be between 0 and the length of the source, otherwise an exception is thrown.

public int Position { readonly get; set; }

Property Value

int

Remaining

Gets how many bytes can still be read or written from the current to the end of the span.

public readonly int Remaining { get; }

Property Value

int

Remarks

Extension methods such as use this value to consume unread bytes.

uses this property as a completeness guard and throws when the value is greater than 0.

See Also

BufferSpan.Length, BufferSpan.Position, BufferSpan.Dispose()

Methods

Access(int)

Returns a writable view of length bytes starting at the current .

public readonly Span<byte> Access(int length)

Parameters

length int

The number of bytes to expose from the current position.

Returns

Span<byte>

A writable over the requested region. When length is 0, an empty span at the current is returned.

Examples

var buffer = new BufferSpan(stackalloc byte[8]);
var payload = buffer.Access(4);
payload[0] = 0x01;
buffer.Seek(4);

Remarks

This method intentionally does not advance ; callers that consume or fill the returned span must move the cursor explicitly, for example with .

The returned span aliases the underlying buffer, so writing through it mutates the same storage owned by this instance.

Exceptions

ArgumentOutOfRangeException

Thrown when length is negative.

EndOfBufferException

Thrown when the requested range extends past the end of the underlying span.

See Also

BufferSpan.Access(int, int), BufferSpan.Seek(int, SeekOrigin)

Access(int, int)

Returns a writable view of length bytes starting at an absolute position.

public readonly Span<byte> Access(int position, int length)

Parameters

position int

The zero-based index in the underlying span where the returned region begins.

length int

The number of bytes in the returned region.

Returns

Span<byte>

A writable over the specified region. When length is 0, an empty span at position is returned.

Examples

var buffer = new BufferSpan(stackalloc byte[16]);
var header = buffer.Access(0, 2);
header[0] = 0xAA;
header[1] = 0xBB;

Remarks

This overload performs bounds validation and then delegates to on the underlying storage.

It does not modify , which allows random access reads or writes without changing the current cursor.

Exceptions

ArgumentOutOfRangeException

Thrown when position or length is negative.

EndOfBufferException

Thrown when position + length exceeds .

See Also

BufferSpan.Access(int), BufferSpan.Position

Dispose()

Validates that all bytes in this span were consumed before ending its usage scope.

public readonly void Dispose()

Examples

var buffer = new BufferSpan(stackalloc byte[4]);
buffer.WriteInt(42);
buffer.Seek(0, SeekOrigin.Begin);
_ = buffer.ReadInt();
buffer.Dispose();

Remarks

This method is used as a guard: it does not release unmanaged resources.

When bytes remain unread or unwritten ( > 0), it throws so callers can detect incomplete protocol parsing/serialization.

When is 0, it returns normally.

Exceptions

ArgumentOutOfRangeException

Thrown when is negative, which indicates an invalid cursor state.

BufferRemainingDataException

Thrown when there is unconsumed data left in the span.

See Also

BufferSpan.Remaining, BufferSpan.Seek(int, SeekOrigin)

Seek(int, SeekOrigin)

Moves the current position within the underlying span relative to the specified origin.

public void Seek(int offset, SeekOrigin origin = SeekOrigin.Current)

Parameters

offset int

The byte offset applied from origin.

origin SeekOrigin

The reference point used to compute the new position.

Exceptions

ArgumentException

Thrown when origin is not a supported value.

ArgumentOutOfRangeException

Thrown when the resulting position would be negative.

EndOfBufferException

Thrown when the resulting position would move past the end of the span.

Slice(int, int)

Creates a new over a sub-range of the current underlying storage.

public readonly BufferSpan Slice(int position, int length)

Parameters

position int

The zero-based offset in the current underlying span where the slice begins.

length int

The number of bytes included in the slice.

Returns

BufferSpan

A new representing length bytes starting at position.

Examples

var source = new BufferSpan(stackalloc byte[8]);
var header = source.Slice(0, 2);
header.WriteUnsignedByte(0xAA);

Remarks

The returned points to the same backing memory as this instance but is constrained to the requested region.

The new instance starts with set to 0 relative to the sliced region, while this instance's is not modified.

Exceptions

ArgumentOutOfRangeException

Thrown when position or length is negative.

EndOfBufferException

Thrown when position + length exceeds .

See Also

BufferSpan.Access(int, int), BufferSpan.Position