Class NbtCodec
- Namespace
- fNbt
- Assembly
- fNbt.dll
Reads and writes single NBT documents ("blobs") with a fixed set of NbtOptions, resolved once at construction. Create one per context and reuse it; instances are immutable and safe to share between threads.
public sealed class NbtCodec
- Inheritance
-
NbtCodec
- Inherited Members
Remarks
Blobs carry no file-level framing and are never compressed at this layer:
network packet payloads, LevelDB values, and NBT embedded inside other formats. Reads
stop exactly at the end of one document, leaving trailing bytes in place, and accept any
root tag type unless ValidateOnRead is set. Writes enforce the
flavor's root rules, and its conformance rules when ValidateOnWrite
is set. For one-off use with the current default options, For(NbtFlavor) returns a
cached per-flavor instance. The .NET 8 build also reads from ReadOnlySpan<byte> and
writes to IBufferWriter<byte>, for pooled buffers and pipelines.
Constructors
NbtCodec(NbtFlavor)
Creates a codec for the given flavor with the current default policy settings.
public NbtCodec(NbtFlavor flavor)
Parameters
flavorNbtFlavorEncoding to read and write.
Exceptions
- ArgumentNullException
flavorisnull.
NbtCodec(NbtOptions)
Creates a codec with the given options.
public NbtCodec(NbtOptions options)
Parameters
optionsNbtOptionsSettings to use, snapshotted here. Later changes to the instance do not affect this codec.
Exceptions
- ArgumentNullException
optionsor itsFlavorisnull.- ArgumentOutOfRangeException
MaxAllocationis zero or negative.
Properties
Flavor
The flavor this codec reads and writes, fixed at construction.
public NbtFlavor Flavor { get; }
Property Value
Methods
For(NbtFlavor)
Returns a cached codec with the current default policy settings for the
given flavor, for one-off use: NbtCodec.For(NbtFlavor.Bedrock).ReadTag(stream).
A returned codec never changes; after a policy default changes, a later call returns
a fresh instance built from the new defaults.
public static NbtCodec For(NbtFlavor flavor)
Parameters
flavorNbtFlavorEncoding to read and write.
Returns
Exceptions
- ArgumentNullException
flavorisnull.
ReadConcatenatedTags(Stream)
Lazily reads back-to-back NBT documents from the given stream until it ends, e.g. a Bedrock LevelDB value holding several roots. Enumeration ends cleanly when the stream runs out exactly on a document boundary; a partially-present document throws.
public IEnumerable<NbtTag> ReadConcatenatedTags(Stream stream)
Parameters
streamStreamStream to read from. Does not need to be seekable.
Returns
- IEnumerable<NbtTag>
A lazy sequence of root tags. The stream is read as the sequence is enumerated.
Exceptions
- ArgumentNullException
streamisnull.- EndOfStreamException
If the stream ends partway through a document.
- NbtFormatException
If a document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or consists of a lone
TAG_Endbyte.
ReadTag(byte[], int, int, out int)
Reads one NBT document from the given buffer.
public NbtTag ReadTag(byte[] buffer, int index, int length, out int bytesConsumed)
Parameters
bufferbyte[]Buffer to read from.
indexintIndex in
bufferat which the document begins.lengthintMaximum number of bytes the document may occupy. Trailing bytes past the document's actual end are ignored.
bytesConsumedintSet to the exact number of bytes the document occupied.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentOutOfRangeException
indexorlengthdo not describe a valid range withinbuffer.- EndOfStreamException
If the document extends past the given
length.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or consists of a lone
TAG_Endbyte.
ReadTag(byte[], int, int, NbtTagType, out int)
Reads one NBT document from the given buffer, requiring a specific root tag type.
public NbtTag ReadTag(byte[] buffer, int index, int length, NbtTagType expectedRootType, out int bytesConsumed)
Parameters
bufferbyte[]Buffer to read from.
indexintIndex in
bufferat which the document begins.lengthintMaximum number of bytes the document may occupy. Trailing bytes past the document's actual end are ignored.
expectedRootTypeNbtTagTypeRoot tag type that the document must have.
bytesConsumedintSet to the exact number of bytes the document occupied.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentOutOfRangeException
indexorlengthdo not describe a valid range withinbuffer; orexpectedRootTypeis not a concrete tag type.- EndOfStreamException
If the document extends past the given
length.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or its root tag type does not match
expectedRootType.
ReadTag(Stream)
Reads one NBT document from the given stream. The stream is left positioned exactly past the end of the document.
public NbtTag ReadTag(Stream stream)
Parameters
streamStreamStream to read from. Does not need to be seekable.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Exceptions
- ArgumentNullException
streamisnull.- EndOfStreamException
If the stream ends before the document does.
- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or consists of a lone
TAG_Endbyte (use TryReadTag(Stream, out NbtTag?) to accept absent documents).
ReadTag(Stream, NbtTagType)
Reads one NBT document from the given stream, requiring a specific root tag type.
public NbtTag ReadTag(Stream stream, NbtTagType expectedRootType)
Parameters
streamStreamStream to read from. Does not need to be seekable.
expectedRootTypeNbtTagTypeRoot tag type that the document must have.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Exceptions
- ArgumentNullException
streamisnull.- ArgumentOutOfRangeException
expectedRootTypeis not a concrete tag type.- EndOfStreamException
If the stream ends before the document does.
- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or its root tag type does not match
expectedRootType.
ReadTag(ReadOnlySpan<byte>, out int)
Reads one NBT document from the given span. The span is touched only during the call, so pooled or stack memory is fine: the returned tags hold copies of their data.
public NbtTag ReadTag(ReadOnlySpan<byte> buffer, out int bytesConsumed)
Parameters
bufferReadOnlySpan<byte>Bytes to read from. Trailing bytes past the document's actual end are ignored.
bytesConsumedintSet to the exact number of bytes the document occupied.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Remarks
Only the .NET 8 build has this overload.
Exceptions
- EndOfStreamException
If the document extends past the end of
buffer.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or consists of a lone
TAG_Endbyte (use TryReadTag(ReadOnlySpan<byte>, out NbtTag?, out int) to accept absent documents).
ReadTag(ReadOnlySpan<byte>, NbtTagType, out int)
Reads one NBT document from the given span, requiring a specific root tag type.
public NbtTag ReadTag(ReadOnlySpan<byte> buffer, NbtTagType expectedRootType, out int bytesConsumed)
Parameters
bufferReadOnlySpan<byte>Bytes to read from. Trailing bytes past the document's actual end are ignored.
expectedRootTypeNbtTagTypeRoot tag type that the document must have.
bytesConsumedintSet to the exact number of bytes the document occupied.
Returns
- NbtTag
The root tag. Its
Nameisnullfor flavors without root names.
Remarks
Only the .NET 8 build has this overload.
Exceptions
- ArgumentOutOfRangeException
expectedRootTypeis not a concrete tag type.- EndOfStreamException
If the document extends past the end of
buffer.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, fails enabled validation, or its root tag type does not match
expectedRootType.
TryReadTag(byte[], int, int, out NbtTag?, out int)
Attempts to read one NBT document from the given buffer. Returns false when
length is zero, or (for flavors that allow non-compound roots) when the
document is a lone TAG_End byte meaning "absent".
public bool TryReadTag(byte[] buffer, int index, int length, out NbtTag? tag, out int bytesConsumed)
Parameters
bufferbyte[]Buffer to read from.
indexintIndex in
bufferat which the document begins.lengthintMaximum number of bytes the document may occupy.
tagNbtTagSet to the root tag, or
nullif no tag was present.bytesConsumedintSet to the exact number of bytes consumed.
Returns
- bool
Whether a tag was read.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentOutOfRangeException
indexorlengthdo not describe a valid range withinbuffer.- EndOfStreamException
If the document extends past the given
length.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, or fails enabled validation.
TryReadTag(Stream, out NbtTag?)
Attempts to read one NBT document from the given stream. Returns false when
the stream is already at its end, or (for flavors that allow non-compound roots) when the
document is a lone TAG_End byte meaning "absent". Anything else is parsed as a complete
document, and a partial one still throws.
public bool TryReadTag(Stream stream, out NbtTag? tag)
Parameters
streamStreamStream to read from. Does not need to be seekable.
tagNbtTagSet to the root tag, or
nullif no tag was present.
Returns
- bool
Whether a tag was read.
Exceptions
- ArgumentNullException
streamisnull.- EndOfStreamException
If the stream ends partway through a document.
- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, or fails enabled validation.
TryReadTag(ReadOnlySpan<byte>, out NbtTag?, out int)
Attempts to read one NBT document from the given span. Returns false when
the span is empty, or (for flavors that allow non-compound roots) when the document is a
lone TAG_End byte meaning "absent".
public bool TryReadTag(ReadOnlySpan<byte> buffer, out NbtTag? tag, out int bytesConsumed)
Parameters
bufferReadOnlySpan<byte>Bytes to read from. Trailing bytes past the document's actual end are ignored.
tagNbtTagSet to the root tag, or
nullif no tag was present.bytesConsumedintSet to the exact number of bytes consumed.
Returns
- bool
Whether a tag was read.
Remarks
Only the .NET 8 build has this overload.
Exceptions
- EndOfStreamException
If the document extends past the end of
buffer.- NbtFormatException
If the document is malformed, nested more than 512 levels deep, exceeds a configured limit, or fails enabled validation.
WriteConcatenatedTags(IEnumerable<NbtTag>, IBufferWriter<byte>)
Writes back-to-back NBT documents to the given buffer writer. Behaves like WriteConcatenatedTags(IEnumerable<NbtTag>, Stream).
public void WriteConcatenatedTags(IEnumerable<NbtTag> tags, IBufferWriter<byte> output)
Parameters
tagsIEnumerable<NbtTag>Root tags to write, one document each. May not contain
null.outputIBufferWriter<byte>Buffer writer to write to.
Remarks
Only the .NET 8 build has this overload.
Exceptions
- ArgumentNullException
tagsoroutputisnull.- ArgumentException
tagscontains anulltag. Documents before it are already written when this throws.- NbtFormatException
If a tag is not a compound and the flavor requires one; if enabled validation rejects a tag type or string length; if a compound contains unnamed tags; if a list has Unknown list type and no elements; if a string is too long; or if tags are nested more than 512 levels deep. Documents before the offending one are already written when this throws.
WriteConcatenatedTags(IEnumerable<NbtTag>, Stream)
Writes back-to-back NBT documents to the given stream, e.g. a Bedrock LevelDB value holding several roots. Mirrors ReadConcatenatedTags(Stream), and is cheaper than repeated WriteTag(NbtTag?, Stream) calls when documents are many.
public void WriteConcatenatedTags(IEnumerable<NbtTag> tags, Stream stream)
Parameters
tagsIEnumerable<NbtTag>Root tags to write, one document each. May not contain
null: absent documents cannot appear in a concatenated stream.streamStreamStream to write to.
Exceptions
- ArgumentNullException
tagsorstreamisnull.- ArgumentException
tagscontains anulltag. Documents before it are already written when this throws.- NbtFormatException
If a tag is not a compound and the flavor requires one; if enabled validation rejects a tag type or string length; if a compound contains unnamed tags; if a list has Unknown list type and no elements; if a string is too long; or if tags are nested more than 512 levels deep. Documents before the offending one are already written when this throws.
WriteTag(NbtTag?)
Writes one NBT document to a new byte array of exactly the right size.
public byte[] WriteTag(NbtTag? tag)
Parameters
tagNbtTagRoot tag to write. For flavors that allow non-compound roots,
nullwrites an absent document (a loneTAG_Endbyte); other flavors require an NbtCompound. Anullroot name is written as an empty string.
Returns
- byte[]
Byte array containing the serialized document.
Exceptions
- ArgumentNullException
tagisnulland the flavor requires a compound root.- NotSupportedException
The document does not fit in a single array.
- NbtFormatException
If
tagis not a compound and the flavor requires one; if enabled validation rejects a tag type or string length; if a compound contains unnamed tags; if a list has Unknown list type and no elements; if a string is too long; or if tags are nested more than 512 levels deep.
WriteTag(NbtTag?, IBufferWriter<byte>)
Writes one NBT document to the given buffer writer, such as a pipe or an
ArrayBufferWriter.
public void WriteTag(NbtTag? tag, IBufferWriter<byte> output)
Parameters
tagNbtTagRoot tag to write. For flavors that allow non-compound roots,
nullwrites an absent document (a loneTAG_Endbyte); other flavors require an NbtCompound. Anullroot name is written as an empty string.outputIBufferWriter<byte>Buffer writer to write to.
Remarks
Only the .NET 8 build has this overload.
Exceptions
- ArgumentNullException
outputisnull; ortagisnulland the flavor requires a compound root.- NbtFormatException
If
tagis not a compound and the flavor requires one; if enabled validation rejects a tag type or string length; if a compound contains unnamed tags; if a list has Unknown list type and no elements; if a string is too long; or if tags are nested more than 512 levels deep.
WriteTag(NbtTag?, Stream)
Writes one NBT document to the given stream.
public void WriteTag(NbtTag? tag, Stream stream)
Parameters
tagNbtTagRoot tag to write. For flavors that allow non-compound roots,
nullwrites an absent document (a loneTAG_Endbyte); other flavors require an NbtCompound. Anullroot name is written as an empty string.streamStreamStream to write to.
Exceptions
- ArgumentNullException
streamisnull; ortagisnulland the flavor requires a compound root.- NbtFormatException
If
tagis not a compound and the flavor requires one; if enabled validation rejects a tag type or string length; if a compound contains unnamed tags; if a list has Unknown list type and no elements; if a string is too long; or if tags are nested more than 512 levels deep.