Table of Contents

Class NbtWriter

Namespace
fNbt
Assembly
fNbt.dll

An efficient writer for writing NBT data directly to streams. Each instance of NbtWriter writes one complete file. NbtWriter enforces all constraints of the NBT file format EXCEPT checking for duplicate tag names within a compound.

public sealed class NbtWriter
Inheritance
NbtWriter
Inherited Members

Constructors

NbtWriter(Stream, string)

Initializes a new instance of the NbtWriter class.

public NbtWriter(Stream stream, string rootTagName)

Parameters

stream Stream

Stream to write to.

rootTagName string

Name to give to the root tag (written immediately).

Remarks

Assumes that data in the stream should be Big-Endian encoded.

Exceptions

ArgumentNullException

stream or rootTagName is null.

ArgumentException

stream is not writable.

NbtWriter(Stream, string, bool)

Initializes a new instance of the NbtWriter class.

public NbtWriter(Stream stream, string rootTagName, bool bigEndian)

Parameters

stream Stream

Stream to write to.

rootTagName string

Name to give to the root tag (written immediately).

bigEndian bool

Whether NBT data should be in Big-Endian encoding.

Exceptions

ArgumentNullException

stream or rootTagName is null.

ArgumentException

stream is not writable.

Properties

BaseStream

Gets the underlying stream of the NbtWriter.

public Stream BaseStream { get; }

Property Value

Stream

IsDone

Gets whether the root tag has been closed. No more tags may be written after the root tag has been closed.

public bool IsDone { get; }

Property Value

bool

Methods

BeginCompound()

Begins an unnamed compound tag.

public void BeginCompound()

Exceptions

NbtFormatException

No more tags can be written -OR- a named compound tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

BeginCompound(string)

Begins a named compound tag.

public void BeginCompound(string tagName)

Parameters

tagName string

Name to give to this compound tag. May not be null.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed compound tag was expected -OR- a tag of a different type was expected.

BeginList(string, NbtTagType, int)

Begins an unnamed list tag.

public void BeginList(string tagName, NbtTagType elementType, int size)

Parameters

tagName string

Name to give to this compound tag. May not be null.

elementType NbtTagType

Type of elements of this list.

size int

Number of elements in this list. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed list tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

size is negative -OR- elementType is not a valid NbtTagType.

BeginList(NbtTagType, int)

Begins an unnamed list tag.

public void BeginList(NbtTagType elementType, int size)

Parameters

elementType NbtTagType

Type of elements of this list.

size int

Number of elements in this list. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- a named list tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

size is negative -OR- elementType is not a valid NbtTagType.

EndCompound()

Ends a compound tag.

public void EndCompound()

Exceptions

NbtFormatException

Not currently in a compound.

EndList()

Ends a list tag.

public void EndList()

Exceptions

NbtFormatException

Not currently in a list -OR- not all list elements have been written yet.

Finish()

Ensures that file has been written in its entirety, with no tags left open. This method is for verification only, and does not actually write any data. Calling this method is optional (but probably a good idea, to catch any usage errors).

public void Finish()

Exceptions

NbtFormatException

Not all tags have been closed yet.

WriteByte(byte)

Writes an unnamed byte tag.

public void WriteByte(byte value)

Parameters

value byte

The unsigned byte to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named byte tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteByte(string, byte)

Writes an unnamed byte tag.

public void WriteByte(string tagName, byte value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value byte

The unsigned byte to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte tag was expected -OR- a tag of a different type was expected.

WriteByteArray(byte[])

Writes an unnamed byte array tag, copying data from an array.

public void WriteByteArray(byte[] data)

Parameters

data byte[]

A byte array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named byte array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentNullException

data is null

WriteByteArray(byte[], int, int)

Writes an unnamed byte array tag, copying data from an array.

public void WriteByteArray(byte[] data, int offset, int count)

Parameters

data byte[]

A byte array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of bytes to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- a named byte array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteByteArray(Stream, int)

Writes an unnamed byte array tag, copying data from a stream.

public void WriteByteArray(Stream dataSource, int count)

Parameters

dataSource Stream

A Stream from which data will be copied.

count int

The number of bytes to write. Must not be negative.

Remarks

A temporary buffer will be allocated, of size up to 8192 bytes. To manually specify a buffer, use one of the other WriteByteArray() overloads.

Exceptions

NbtFormatException

No more tags can be written -OR- a named byte array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

count is negative.

ArgumentNullException

dataSource is null.

ArgumentException

Given stream does not support reading.

WriteByteArray(Stream, int, byte[])

Writes an unnamed byte array tag, copying data from a stream.

public void WriteByteArray(Stream dataSource, int count, byte[] buffer)

Parameters

dataSource Stream

A Stream from which data will be copied.

count int

The number of bytes to write. Must not be negative.

buffer byte[]

Buffer to use for copying. Size must be greater than 0. Must not be null.

Exceptions

NbtFormatException

No more tags can be written -OR- a named byte array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

count is negative.

ArgumentNullException

dataSource is null.

ArgumentException

Given stream does not support reading -OR- buffer size is 0.

WriteByteArray(string, byte[])

Writes a named byte array tag, copying data from an array.

public void WriteByteArray(string tagName, byte[] data)

Parameters

tagName string

Name to give to this byte array tag. May not be null.

data byte[]

A byte array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte array tag was expected -OR- a tag of a different type was expected.

ArgumentNullException

tagName or data is null

WriteByteArray(string, byte[], int, int)

Writes a named byte array tag, copying data from an array.

public void WriteByteArray(string tagName, byte[] data, int offset, int count)

Parameters

tagName string

Name to give to this byte array tag. May not be null.

data byte[]

A byte array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of bytes to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte array tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

tagName or data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteByteArray(string, Stream, int)

Writes a named byte array tag, copying data from a stream.

public void WriteByteArray(string tagName, Stream dataSource, int count)

Parameters

tagName string

Name to give to this byte array tag. May not be null.

dataSource Stream

A Stream from which data will be copied.

count int

The number of bytes to write. Must not be negative.

Remarks

A temporary buffer will be allocated, of size up to 8192 bytes. To manually specify a buffer, use one of the other WriteByteArray() overloads.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte array tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

count is negative.

ArgumentNullException

dataSource is null.

ArgumentException

Given stream does not support reading.

WriteByteArray(string, Stream, int, byte[])

Writes an unnamed byte array tag, copying data from another stream.

public void WriteByteArray(string tagName, Stream dataSource, int count, byte[] buffer)

Parameters

tagName string

Name to give to this byte array tag. May not be null.

dataSource Stream

A Stream from which data will be copied.

count int

The number of bytes to write. Must not be negative.

buffer byte[]

Buffer to use for copying. Size must be greater than 0. Must not be null.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte array tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

count is negative.

ArgumentNullException

dataSource is null.

ArgumentException

Given stream does not support reading -OR- buffer size is 0.

WriteDouble(double)

Writes an unnamed double tag.

public void WriteDouble(double value)

Parameters

value double

The eight-byte floating-point value to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named double tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteDouble(string, double)

Writes an unnamed byte tag.

public void WriteDouble(string tagName, double value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value double

The unsigned byte to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed byte tag was expected -OR- a tag of a different type was expected.

WriteFloat(float)

Writes an unnamed float tag.

public void WriteFloat(float value)

Parameters

value float

The four-byte floating-point value to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named float tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteFloat(string, float)

Writes an unnamed float tag.

public void WriteFloat(string tagName, float value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value float

The four-byte floating-point value to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed float tag was expected -OR- a tag of a different type was expected.

WriteInt(int)

Writes an unnamed int tag.

public void WriteInt(int value)

Parameters

value int

The four-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named int tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteInt(string, int)

Writes an unnamed int tag.

public void WriteInt(string tagName, int value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value int

The four-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed int tag was expected -OR- a tag of a different type was expected.

WriteIntArray(int[])

Writes an unnamed int array tag, copying data from an array.

public void WriteIntArray(int[] data)

Parameters

data int[]

An int array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named int array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentNullException

data is null

WriteIntArray(int[], int, int)

Writes an unnamed int array tag, copying data from an array.

public void WriteIntArray(int[] data, int offset, int count)

Parameters

data int[]

An int array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of elements to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- a named int array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteIntArray(string, int[])

Writes a named int array tag, copying data from an array.

public void WriteIntArray(string tagName, int[] data)

Parameters

tagName string

Name to give to this int array tag. May not be null.

data int[]

An int array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed int array tag was expected -OR- a tag of a different type was expected.

ArgumentNullException

tagName or data is null

WriteIntArray(string, int[], int, int)

Writes a named int array tag, copying data from an array.

public void WriteIntArray(string tagName, int[] data, int offset, int count)

Parameters

tagName string

Name to give to this int array tag. May not be null.

data int[]

An int array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of elements to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed int array tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

tagName or data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteLong(long)

Writes an unnamed long tag.

public void WriteLong(long value)

Parameters

value long

The eight-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named long tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteLong(string, long)

Writes an unnamed long tag.

public void WriteLong(string tagName, long value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value long

The eight-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed long tag was expected -OR- a tag of a different type was expected.

WriteLongArray(long[])

Writes an unnamed long array tag, copying data from an array.

public void WriteLongArray(long[] data)

Parameters

data long[]

A long array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named long array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentNullException

data is null

WriteLongArray(long[], int, int)

Writes an unnamed long array tag, copying data from an array.

public void WriteLongArray(long[] data, int offset, int count)

Parameters

data long[]

A long array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of elements to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- a named long array tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteLongArray(string, long[])

Writes a named long array tag, copying data from an array.

public void WriteLongArray(string tagName, long[] data)

Parameters

tagName string

Name to give to this long array tag. May not be null.

data long[]

A long array containing the data to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed long array tag was expected -OR- a tag of a different type was expected.

ArgumentNullException

tagName or data is null

WriteLongArray(string, long[], int, int)

Writes a named long array tag, copying data from an array.

public void WriteLongArray(string tagName, long[] data, int offset, int count)

Parameters

tagName string

Name to give to this long array tag. May not be null.

data long[]

A long array containing the data to write.

offset int

The starting point in data at which to begin writing. Must not be negative.

count int

The number of elements to write. Must not be negative.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed long array tag was expected -OR- a tag of a different type was expected.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentNullException

tagName or data is null

ArgumentException

count is greater than offset subtracted from the array length.

WriteShort(short)

Writes an unnamed short tag.

public void WriteShort(short value)

Parameters

value short

The two-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named short tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteShort(string, short)

Writes an unnamed short tag.

public void WriteShort(string tagName, short value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value short

The two-byte signed integer to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed short tag was expected -OR- a tag of a different type was expected.

WriteString(string)

Writes an unnamed string tag.

public void WriteString(string value)

Parameters

value string

The string to write.

Exceptions

NbtFormatException

No more tags can be written -OR- a named string tag was expected -OR- a tag of a different type was expected -OR- the size of a parent list has been exceeded.

WriteString(string, string)

Writes an unnamed string tag.

public void WriteString(string tagName, string value)

Parameters

tagName string

Name to give to this compound tag. May not be null.

value string

The string to write.

Exceptions

NbtFormatException

No more tags can be written -OR- an unnamed string tag was expected -OR- a tag of a different type was expected.

WriteTag(NbtTag)

Writes a NbtTag object, and all of its child tags, to stream. Use this method sparingly with NbtWriter -- constructing NbtTag objects defeats the purpose of this class. If you already have lots of NbtTag objects, you might as well use NbtFile to write them all at once.

public void WriteTag(NbtTag tag)

Parameters

tag NbtTag

Tag to write. Must not be null.

Exceptions

NbtFormatException

No more tags can be written -OR- given tag is unacceptable at this time.

ArgumentNullException

tag is null