Improved string read/write.

This commit is contained in:
Vladislavs Golubs 2015-10-03 23:12:53 +03:00 committed by Steveice10
parent c9852292d3
commit efc086aa6b
3 changed files with 8 additions and 24 deletions

View File

@ -6,7 +6,6 @@ import org.spacehq.opennbt.tag.builtin.CompoundTag;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@ -14,7 +13,6 @@ import java.util.zip.GZIPOutputStream;
* A class containing methods for reading/writing NBT tags.
*/
public class NBTIO {
public static final Charset CHARSET = Charset.forName("UTF-8");
/**
* Reads the root CompoundTag from the given file.
@ -145,10 +143,9 @@ public class NBTIO {
return null;
}
byte[] nameBytes = new byte[in.readUnsignedShort()];
in.readFully(nameBytes);
String name = new String(nameBytes, CHARSET);
Tag tag = null;
String name = in.readUTF();
Tag tag;
try {
tag = TagRegistry.createInstance(id, name);
} catch(TagCreateException e) {
@ -167,10 +164,8 @@ public class NBTIO {
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeTag(DataOutputStream out, Tag tag) throws IOException {
byte[] nameBytes = tag.getName().getBytes(CHARSET);
out.writeByte(TagRegistry.getIdFor(tag.getClass()));
out.writeShort(nameBytes.length);
out.write(nameBytes);
out.writeUTF(tag.getName());
tag.write(out);
}
}

View File

@ -1,7 +1,5 @@
package org.spacehq.opennbt.tag.builtin;
import org.spacehq.opennbt.NBTIO;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@ -48,16 +46,12 @@ public class StringTag extends Tag {
@Override
public void read(DataInputStream in) throws IOException {
byte[] bytes = new byte[in.readShort()];
in.readFully(bytes);
this.value = new String(bytes, NBTIO.CHARSET);
this.value = in.readUTF();
}
@Override
public void write(DataOutputStream out) throws IOException {
byte[] bytes = this.value.getBytes(NBTIO.CHARSET);
out.writeShort(bytes.length);
out.write(bytes);
out.writeUTF(this.value);
}
@Override

View File

@ -1,6 +1,5 @@
package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.NBTIO;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
@ -84,9 +83,7 @@ public class StringArrayTag extends Tag {
public void read(DataInputStream in) throws IOException {
this.value = new String[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
byte[] bytes = new byte[in.readShort()];
in.readFully(bytes);
this.value[index] = new String(bytes, NBTIO.CHARSET);
this.value[index] = in.readUTF();
}
}
@ -94,9 +91,7 @@ public class StringArrayTag extends Tag {
public void write(DataOutputStream out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET);
out.writeShort(bytes.length);
out.write(bytes);
out.writeUTF(this.value[index]);
}
}