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, IDisposableImplements
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
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
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
Remaining
Gets how many bytes can still be read or written from the current
public readonly int Remaining { get; }Property Value
Remarks
Extension methods such as
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
A writable length is 0, an empty span at the current
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
The returned span aliases the underlying buffer, so writing through it mutates the same storage owned by this
Exceptions
Thrown when length is negative.
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
A writable 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
It does not modify
Exceptions
Thrown when position or length is negative.
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
When 0, it returns normally.
Exceptions
Thrown when
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
Thrown when origin is not a supported
Thrown when the resulting position would be negative.
Thrown when the resulting position would move past the end of the span.
Slice(int, int)
Creates a new
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
A new 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
The new instance starts with 0 relative to the sliced region, while this instance's
Exceptions
Thrown when position or length is negative.
Thrown when position + length exceeds