Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from basaigh/anytag
Browse files Browse the repository at this point in the history
Add the read/writeAnyTag methods introduced in 23w31a
  • Loading branch information
Camotoy authored Sep 7, 2023
2 parents 413f8c7 + c9bca81 commit eae1397
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.steveice10</groupId>
<artifactId>opennbt</artifactId>
<version>1.5</version>
<version>1.6</version>
<packaging>jar</packaging>

<name>OpenNBT</name>
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/github/steveice10/opennbt/NBTIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,54 @@ public static Tag readTag(DataInput in) throws IOException {
return tag;
}

/**
* Reads a big endian NBT tag.
*
* @param in Input stream to read from.
* @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static Tag readAnyTag(InputStream in) throws IOException {
return readAnyTag(in, false);
}

/**
* Reads an NBT tag.
*
* @param in Input stream to read from.
* @param littleEndian Whether to read little endian NBT.
* @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static Tag readAnyTag(InputStream in, boolean littleEndian) throws IOException {
return readAnyTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in)));
}

/**
* Reads an NBT tag.
*
* @param in Data input to read from.
* @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static Tag readAnyTag(DataInput in) throws IOException {
int id = in.readUnsignedByte();
if(id == 0) {
return null;
}

Tag tag;

try {
tag = TagRegistry.createInstance(id, "");
} catch(TagCreateException e) {
throw new IOException("Failed to create tag.", e);
}

tag.read(in);
return tag;
}

/**
* Writes an NBT tag in big endian.
*
Expand Down Expand Up @@ -241,6 +289,38 @@ public static void writeTag(DataOutput out, Tag tag) throws IOException {
}
}

/**
* Writes an NBT tag in big endian.
*
* @param out Output stream to write to.
* @param tag Tag to write.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeAnyTag(OutputStream out, Tag tag) throws IOException {
writeAnyTag(out, tag, false);
}

/**
* Writes an NBT tag.
*
* @param out Output stream to write to.
* @param tag Tag to write.
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeAnyTag(OutputStream out, Tag tag, boolean littleEndian) throws IOException {
writeAnyTag((DataOutput) (littleEndian ? new LittleEndianDataOutputStream(out) : new DataOutputStream(out)), tag);
}

public static void writeAnyTag(DataOutput out, Tag tag) throws IOException {
if (tag != null) {
out.writeByte(TagRegistry.getIdFor(tag.getClass()));
tag.write(out);
} else {
out.writeByte(0);
}
}

private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput {
public LittleEndianDataInputStream(InputStream in) {
super(in);
Expand Down

0 comments on commit eae1397

Please sign in to comment.