mirror of
https://github.com/ViaVersion/ViaNBT.git
synced 2024-10-02 04:37:34 +02:00
Improved string read/write.
This commit is contained in:
parent
c9852292d3
commit
efc086aa6b
@ -6,7 +6,6 @@ import org.spacehq.opennbt.tag.builtin.CompoundTag;
|
|||||||
import org.spacehq.opennbt.tag.builtin.Tag;
|
import org.spacehq.opennbt.tag.builtin.Tag;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
@ -14,7 +13,6 @@ import java.util.zip.GZIPOutputStream;
|
|||||||
* A class containing methods for reading/writing NBT tags.
|
* A class containing methods for reading/writing NBT tags.
|
||||||
*/
|
*/
|
||||||
public class NBTIO {
|
public class NBTIO {
|
||||||
public static final Charset CHARSET = Charset.forName("UTF-8");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the root CompoundTag from the given file.
|
* Reads the root CompoundTag from the given file.
|
||||||
@ -145,10 +143,9 @@ public class NBTIO {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] nameBytes = new byte[in.readUnsignedShort()];
|
String name = in.readUTF();
|
||||||
in.readFully(nameBytes);
|
Tag tag;
|
||||||
String name = new String(nameBytes, CHARSET);
|
|
||||||
Tag tag = null;
|
|
||||||
try {
|
try {
|
||||||
tag = TagRegistry.createInstance(id, name);
|
tag = TagRegistry.createInstance(id, name);
|
||||||
} catch(TagCreateException e) {
|
} catch(TagCreateException e) {
|
||||||
@ -167,10 +164,8 @@ public class NBTIO {
|
|||||||
* @throws java.io.IOException If an I/O error occurs.
|
* @throws java.io.IOException If an I/O error occurs.
|
||||||
*/
|
*/
|
||||||
public static void writeTag(DataOutputStream out, Tag tag) throws IOException {
|
public static void writeTag(DataOutputStream out, Tag tag) throws IOException {
|
||||||
byte[] nameBytes = tag.getName().getBytes(CHARSET);
|
|
||||||
out.writeByte(TagRegistry.getIdFor(tag.getClass()));
|
out.writeByte(TagRegistry.getIdFor(tag.getClass()));
|
||||||
out.writeShort(nameBytes.length);
|
out.writeUTF(tag.getName());
|
||||||
out.write(nameBytes);
|
|
||||||
tag.write(out);
|
tag.write(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.spacehq.opennbt.tag.builtin;
|
package org.spacehq.opennbt.tag.builtin;
|
||||||
|
|
||||||
import org.spacehq.opennbt.NBTIO;
|
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -48,16 +46,12 @@ public class StringTag extends Tag {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(DataInputStream in) throws IOException {
|
public void read(DataInputStream in) throws IOException {
|
||||||
byte[] bytes = new byte[in.readShort()];
|
this.value = in.readUTF();
|
||||||
in.readFully(bytes);
|
|
||||||
this.value = new String(bytes, NBTIO.CHARSET);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(DataOutputStream out) throws IOException {
|
public void write(DataOutputStream out) throws IOException {
|
||||||
byte[] bytes = this.value.getBytes(NBTIO.CHARSET);
|
out.writeUTF(this.value);
|
||||||
out.writeShort(bytes.length);
|
|
||||||
out.write(bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.spacehq.opennbt.tag.builtin.custom;
|
package org.spacehq.opennbt.tag.builtin.custom;
|
||||||
|
|
||||||
import org.spacehq.opennbt.NBTIO;
|
|
||||||
import org.spacehq.opennbt.tag.builtin.Tag;
|
import org.spacehq.opennbt.tag.builtin.Tag;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -84,9 +83,7 @@ public class StringArrayTag extends Tag {
|
|||||||
public void read(DataInputStream in) throws IOException {
|
public void read(DataInputStream in) throws IOException {
|
||||||
this.value = new String[in.readInt()];
|
this.value = new String[in.readInt()];
|
||||||
for(int index = 0; index < this.value.length; index++) {
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
byte[] bytes = new byte[in.readShort()];
|
this.value[index] = in.readUTF();
|
||||||
in.readFully(bytes);
|
|
||||||
this.value[index] = new String(bytes, NBTIO.CHARSET);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,9 +91,7 @@ public class StringArrayTag extends Tag {
|
|||||||
public void write(DataOutputStream out) throws IOException {
|
public void write(DataOutputStream out) throws IOException {
|
||||||
out.writeInt(this.value.length);
|
out.writeInt(this.value.length);
|
||||||
for(int index = 0; index < this.value.length; index++) {
|
for(int index = 0; index < this.value.length; index++) {
|
||||||
byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET);
|
out.writeUTF(this.value[index]);
|
||||||
out.writeShort(bytes.length);
|
|
||||||
out.write(bytes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user