Skip to content

Class TagReader

Namespace: Void.Minecraft.Nbt
Assembly: Void.Minecraft.dll

Provides methods for reading NBT data from a stream.

public class TagReader : TagIO, IDisposable

Inheritance

objectTagIOTagReader

Derived

NbtReader

Implements

IDisposable

Inherited Members

TagIO.BaseStream, TagIO.SwapEndian, TagIO.UseVarInt, TagIO.ZigZagEncoding, TagIO.FormatOptions, TagIO.Dispose(), TagIO.DisposeAsync(), object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Constructors

TagReader(Stream, FormatOptions, bool)

Creates a new instance of the class from the given stream.

public TagReader(Stream stream, FormatOptions options, bool leaveOpen = false)

Parameters

stream Stream

A instance that the reader will be reading from.

options FormatOptions

Bitwise flags to configure how data should be handled for compatibility between different specifications.

leaveOpen bool

to leave the stream object open after disposing the object; otherwise, false.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public override void Dispose()

DisposeAsync()

Asynchronously releases the unmanaged resources used by the .

public override ValueTask DisposeAsync()

Returns

ValueTask

OnTagEncountered(TagType, bool)

Invokes the event when the stream is positioned at the beginning of an unread tag.

protected virtual Tag? OnTagEncountered(TagType type, bool named)

Parameters

type TagType

The type of tag next to be read from the stream.

named bool

Flag indicating if this tag is named.

Returns

Tag?

When handled by an event subscriber, returns a parsed instance, otherwise returns .

OnTagRead(Tag)

Invokes the event when a tag has been fully deserialized from the .

protected virtual void OnTagRead(Tag tag)

Parameters

tag Tag

The deserialized instance.

ReadByte(bool)

Reads a from the stream.

public ByteTag ReadByte(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

ByteTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadByteArray(bool)

Reads a from the stream.

public ByteArrayTag ReadByteArray(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

ByteArrayTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadCompound(bool)

Reads a from the stream.

public CompoundTag ReadCompound(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

CompoundTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadDouble(bool)

Reads a from the stream.

public DoubleTag ReadDouble(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

DoubleTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadFloat(bool)

Reads a from the stream.

public FloatTag ReadFloat(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

FloatTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadInt(bool)

Reads a from the stream.

public IntTag ReadInt(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

IntTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadIntArray(bool)

Reads a from the stream.

public IntArrayTag ReadIntArray(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

IntArrayTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadList(bool)

Reads a from the stream.

public ListTag ReadList(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

ListTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadLong(bool)

Reads a from the stream.

public LongTag ReadLong(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

LongTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadLongArray(bool)

Reads a from the stream.

public LongArrayTag ReadLongArray(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

LongArrayTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadShort(bool)

Reads a from the stream.

public ShortTag ReadShort(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

ShortTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadString(bool)

Reads a from the stream.

public StringTag ReadString(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

StringTag

The deserialized instance.

Remarks

It is assumed that the stream is positioned at the beginning of the tag payload.

ReadTag(bool)

Reads a from the current position in the stream.

public Tag ReadTag(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

Tag

The tag instance that was read from the stream.

ReadTag<T>(bool)

Convenience method to read a tag and cast it automatically.

public T ReadTag<T>(bool named = true) where T : Tag

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

T

The tag instance that was read from the stream.

Type Parameters

T

The tag type that is being read from the stream.

Remarks

This is typically only used when reading the top-level of a document where the type is already known.

ReadTagAsync(bool)

Asynchronously reads a from the current position in the stream.

public Task<Tag> ReadTagAsync(bool named = true)

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

Task<Tag>

The tag instance that was read from the stream.

ReadTagAsync<T>(bool)

Convenience method to asynchronously read a tag and cast it automatically.

public Task<T> ReadTagAsync<T>(bool named = true) where T : Tag

Parameters

named bool

Flag indicating if this tag is named, only false when a tag is a direct child of a .

Returns

Task<T>

The tag instance that was read from the stream.

Type Parameters

T

The tag type that is being read from the stream.

Remarks

This is typically only used when reading the top-level of a document where the type is already known.

ReadToFixSizedBuffer(Span<byte>)

Reads bytes from the streams and stores them into the buffer. The number of read bytes is dictated by the size of the buffer. This method ensures that all requested bytes are read.

protected void ReadToFixSizedBuffer(Span<byte> buffer)

Parameters

buffer Span<byte>

The buffer where the read bytes are written to. the buffer size defines the number of bytes to read.

Remarks

Use this instead of BaseStream.Read(buffer). There was a breaking change in .NET 6 where the can read less bytes than requested for certain streams. Read more here: https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams

Exceptions

EndOfStreamException

Throws if no more bytes could be read from the stream, but the buffer wasn’t completely filled yet.

ReadToFixSizedBuffer(byte[], int, int)

Reads bytes from the streams and stores them into the buffer. This method ensures that all requested bytes are read.

protected void ReadToFixSizedBuffer(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

The buffer where the read bytes are written to. The data will be stored starting at offset to offset + count - 1.

offset int

The offset in buffer where the read data is stored.

count int

The number of bytes to read. Must be positive.

Remarks

Use this instead of BaseStream.Read(buffer, offset, count). There was a breaking change in .NET 6 where the can read less bytes than requested for certain streams. Read more here: https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams

Exceptions

EndOfStreamException

Throws if no more bytes could be read from the stream, but the buffer wasn’t completely filled yet.

ReadUTF8String()

Reads a length-prefixed UTF-8 string from the stream.

protected string? ReadUTF8String()

Returns

string?

The deserialized string instance.

TagEncountered

Occurs when a tag has been encountered in the stream, after reading the first byte to determine its .

public event TagReaderCallback<TagHandledEventArgs>? TagEncountered

Event Type

TagReaderCallback<TagHandledEventArgs>?

TagRead

Occurs when a tag has been fully deserialized from the stream.

public event TagReaderCallback<TagEventArgs>? TagRead

Event Type

TagReaderCallback<TagEventArgs>?