Use DataInput/DataOutput for I/O, add little endian support.

This commit is contained in:
Steveice10 2016-12-15 18:15:04 -08:00
parent 894d52ebe5
commit cee66db745
20 changed files with 611 additions and 132 deletions

View File

@ -14,7 +14,7 @@ import java.util.zip.GZIPOutputStream;
*/
public class NBTIO {
/**
* Reads the root CompoundTag from the given file.
* Reads the compressed, big endian root CompoundTag from the given file.
*
* @param path Path of the file.
* @return The read compound tag.
@ -25,43 +25,45 @@ public class NBTIO {
}
/**
* Reads the root CompoundTag from the given file.
* Reads the compressed, big endian root CompoundTag from the given file.
*
* @param file File to read from.
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(File file) throws IOException {
return readFile(file, true);
return readFile(file, true, false);
}
/**
* Reads the root CompoundTag from the given file.
*
* @param path Path of the file.
* @param compressed Whether the NBT file is compressed.
* @param path Path of the file.
* @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(String path, boolean compressed) throws IOException {
return readFile(new File(path), compressed);
public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException {
return readFile(new File(path), compressed, littleEndian);
}
/**
* Reads the root CompoundTag from the given file.
*
* @param file File to read from.
* @param compressed Whether the NBT file is compressed.
* @param file File to read from.
* @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs.
*/
public static CompoundTag readFile(File file, boolean compressed) throws IOException {
public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException {
InputStream in = new FileInputStream(file);
if(compressed) {
in = new GZIPInputStream(in);
}
Tag tag = readTag(in);
Tag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!");
}
@ -70,7 +72,7 @@ public class NBTIO {
}
/**
* Writes the given root CompoundTag to the given file.
* Writes the given root CompoundTag to the given file, compressed and in big endian.
*
* @param tag Tag to write.
* @param path Path to write to.
@ -81,37 +83,39 @@ public class NBTIO {
}
/**
* Writes the given root CompoundTag to the given file.
* Writes the given root CompoundTag to the given file, compressed and in big endian.
*
* @param tag Tag to write.
* @param file File to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, File file) throws IOException {
writeFile(tag, file, true);
writeFile(tag, file, true, false);
}
/**
* Writes the given root CompoundTag to the given file.
*
* @param tag Tag to write.
* @param path Path to write to.
* @param compressed Whether the NBT file should be compressed.
* @param tag Tag to write.
* @param path Path to write to.
* @param compressed Whether the NBT file should be compressed.
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException {
writeFile(tag, new File(path), compressed);
public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException {
writeFile(tag, new File(path), compressed, littleEndian);
}
/**
* Writes the given root CompoundTag to the given file.
*
* @param tag Tag to write.
* @param file File to write to.
* @param compressed Whether the NBT file should be compressed.
* @param tag Tag to write.
* @param file File to write to.
* @param compressed Whether the NBT file should be compressed.
* @param littleEndian Whether to write little endian NBT.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException {
public static void writeFile(CompoundTag tag, File file, boolean compressed, boolean littleEndian) throws IOException {
if(!file.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
@ -125,50 +129,355 @@ public class NBTIO {
out = new GZIPOutputStream(out);
}
writeTag(out, tag);
writeTag(out, tag, littleEndian);
out.close();
}
/**
* 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 readTag(InputStream in) throws IOException {
return readTag(in, false);
}
/**
* Reads an NBT tag.
*
* @param in Input stream to read from.
* @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 readTag(InputStream in) throws IOException {
DataInputStream dataIn = new DataInputStream(in);
int id = dataIn.readUnsignedByte();
if(id == 0) {
return null;
}
String name = dataIn.readUTF();
Tag tag;
try {
tag = TagRegistry.createInstance(id, name);
} catch(TagCreateException e) {
throw new IOException("Failed to create tag.", e);
}
tag.read(dataIn);
return tag;
public static Tag readTag(InputStream in, boolean littleEndian) throws IOException {
return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : new DataInputStream(in)));
}
/**
* Writes a tag to an output stream.
*
* @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 writeTag(OutputStream out, Tag tag) throws IOException {
DataOutputStream dataOut = new DataOutputStream(out);
/**
* 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 readTag(DataInput in) throws IOException {
int id = in.readUnsignedByte();
if(id == 0) {
return null;
}
dataOut.writeByte(TagRegistry.getIdFor(tag.getClass()));
dataOut.writeUTF(tag.getName());
tag.write(dataOut);
String name = in.readUTF();
Tag tag;
try {
tag = TagRegistry.createInstance(id, name);
} catch(TagCreateException e) {
throw new IOException("Failed to create tag.", e);
}
tag.read(in);
return tag;
}
/**
* 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 writeTag(OutputStream out, Tag tag) throws IOException {
writeTag(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 writeTag(OutputStream out, Tag tag, boolean littleEndian) throws IOException {
writeTag((DataOutput) (littleEndian ? new LittleEndianDataOutputStream(out) : new DataOutputStream(out)), tag);
}
/**
* Writes an NBT tag.
*
* @param out Data output to write to.
* @param tag Tag to write.
* @throws java.io.IOException If an I/O error occurs.
*/
public static void writeTag(DataOutput out, Tag tag) throws IOException {
out.writeByte(TagRegistry.getIdFor(tag.getClass()));
out.writeUTF(tag.getName());
tag.write(out);
}
private static class LittleEndianDataInputStream extends FilterInputStream implements DataInput {
public LittleEndianDataInputStream(InputStream in) {
super(in);
}
@Override
public int read(byte[] b) throws IOException {
return this.in.read(b, 0, b.length);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
@Override
public void readFully(byte[] b) throws IOException {
this.readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
if(len < 0) {
throw new IndexOutOfBoundsException();
} else {
int read;
for(int pos = 0; pos < len; pos += read) {
read = this.in.read(b, off + pos, len - pos);
if(read < 0) {
throw new EOFException();
}
}
}
}
@Override
public int skipBytes(int n) throws IOException {
int total = 0;
int skipped = 0;
while(total < n && (skipped = (int) this.in.skip(n - total)) > 0) {
total += skipped;
}
return total;
}
@Override
public boolean readBoolean() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val != 0;
}
@Override
public byte readByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return (byte) val;
}
@Override
public int readUnsignedByte() throws IOException {
int val = this.in.read();
if(val < 0) {
throw new EOFException();
}
return val;
}
@Override
public short readShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (short) (b1 | (b2 << 8));
}
@Override
public int readUnsignedShort() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8);
}
@Override
public char readChar() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
if((b1 | b2) < 0) {
throw new EOFException();
}
return (char) (b1 | (b2 << 8));
}
@Override
public int readInt() throws IOException {
int b1 = this.in.read();
int b2 = this.in.read();
int b3 = this.in.read();
int b4 = this.in.read();
if((b1 | b2 | b3 | b4) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
}
@Override
public long readLong() throws IOException {
long b1 = this.in.read();
long b2 = this.in.read();
long b3 = this.in.read();
long b4 = this.in.read();
long b5 = this.in.read();
long b6 = this.in.read();
long b7 = this.in.read();
long b8 = this.in.read();
if((b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8) < 0) {
throw new EOFException();
}
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
}
@Override
public float readFloat() throws IOException {
return Float.intBitsToFloat(this.readInt());
}
@Override
public double readDouble() throws IOException {
return Double.longBitsToDouble(this.readLong());
}
@Override
public String readLine() throws IOException {
throw new UnsupportedOperationException("Use readUTF.");
}
@Override
public String readUTF() throws IOException {
byte[] bytes = new byte[this.readUnsignedShort()];
this.readFully(bytes);
return new String(bytes, "UTF-8");
}
}
private static class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput {
public LittleEndianDataOutputStream(OutputStream out) {
super(out);
}
@Override
public synchronized void write(int b) throws IOException {
this.out.write(b);
}
@Override
public synchronized void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
this.out.flush();
}
@Override
public void writeBoolean(boolean b) throws IOException {
this.out.write(b ? 1 : 0);
}
@Override
public void writeByte(int b) throws IOException {
this.out.write(b);
}
@Override
public void writeShort(int s) throws IOException {
this.out.write(s & 0xFF);
this.out.write((s >>> 8) & 0xFF);
}
@Override
public void writeChar(int c) throws IOException {
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
@Override
public void writeInt(int i) throws IOException {
this.out.write(i & 0xFF);
this.out.write((i >>> 8) & 0xFF);
this.out.write((i >>> 16) & 0xFF);
this.out.write((i >>> 24) & 0xFF);
}
@Override
public void writeLong(long l) throws IOException {
this.out.write((int) (l & 0xFF));
this.out.write((int) ((l >>> 8) & 0xFF));
this.out.write((int) ((l >>> 16) & 0xFF));
this.out.write((int) ((l >>> 24) & 0xFF));
this.out.write((int) ((l >>> 32) & 0xFF));
this.out.write((int) ((l >>> 40) & 0xFF));
this.out.write((int) ((l >>> 48) & 0xFF));
this.out.write((int) ((l >>> 56) & 0xFF));
}
@Override
public void writeFloat(float f) throws IOException {
this.writeInt(Float.floatToIntBits(f));
}
@Override
public void writeDouble(double d) throws IOException {
this.writeLong(Double.doubleToLongBits(d));
}
@Override
public void writeBytes(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
this.out.write((byte) s.charAt(index));
}
}
@Override
public void writeChars(String s) throws IOException {
int len = s.length();
for(int index = 0; index < len; index++) {
char c = s.charAt(index);
this.out.write(c & 0xFF);
this.out.write((c >>> 8) & 0xFF);
}
}
@Override
public void writeUTF(String s) throws IOException {
byte[] bytes = s.getBytes("UTF-8");
this.writeShort(bytes.length);
this.write(bytes);
}
}
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -78,13 +78,13 @@ public class ByteArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new byte[in.readInt()];
in.readFully(this.value);
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
out.write(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class ByteTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readByte();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeByte(this.value);
}

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin;
import org.spacehq.opennbt.NBTIO;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.IOException;
import java.util.*;
@ -141,7 +141,7 @@ public class CompoundTag extends Tag implements Iterable<Tag> {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
List<Tag> tags = new ArrayList<Tag>();
try {
Tag tag;
@ -158,7 +158,7 @@ public class CompoundTag extends Tag implements Iterable<Tag> {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
for(Tag tag : this.value.values()) {
NBTIO.writeTag(out, tag);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class DoubleTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readDouble();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeDouble(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class FloatTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readFloat();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeFloat(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -78,7 +78,7 @@ public class IntArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new int[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readInt();
@ -86,7 +86,7 @@ public class IntArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeInt(this.value[index]);

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class IntTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readInt();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value);
}

View File

@ -3,8 +3,8 @@ package org.spacehq.opennbt.tag.builtin;
import org.spacehq.opennbt.tag.TagCreateException;
import org.spacehq.opennbt.tag.TagRegistry;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
@ -143,7 +143,7 @@ public class ListTag extends Tag implements Iterable<Tag> {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
int id = in.readUnsignedByte();
this.type = TagRegistry.getClassFor(id);
this.value = new ArrayList<Tag>();
@ -166,7 +166,7 @@ public class ListTag extends Tag implements Iterable<Tag> {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
if(this.value.isEmpty()) {
out.writeByte(0);
} else {

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class LongTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readLong();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeLong(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class ShortTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readShort();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeShort(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -45,12 +45,12 @@ public class StringTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = in.readUTF();
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeUTF(this.value);
}

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Array;
@ -45,7 +45,7 @@ public abstract class Tag implements Cloneable {
* @param in Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void read(DataInputStream in) throws IOException;
public abstract void read(DataInput in) throws IOException;
/**
* Writes this tag to an output stream.
@ -53,7 +53,7 @@ public abstract class Tag implements Cloneable {
* @param out Stream to write to.
* @throws java.io.IOException If an I/O error occurs.
*/
public abstract void write(DataOutputStream out) throws IOException;
public abstract void write(DataOutput out) throws IOException;
@Override
public abstract Tag clone();

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -80,7 +80,7 @@ public class DoubleArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new double[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readDouble();
@ -88,7 +88,7 @@ public class DoubleArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeDouble(this.value[index]);

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -80,7 +80,7 @@ public class FloatArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new float[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readFloat();
@ -88,7 +88,7 @@ public class FloatArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeFloat(this.value[index]);

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -80,7 +80,7 @@ public class LongArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong();
@ -88,7 +88,7 @@ public class LongArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeLong(this.value[index]);

View File

@ -78,9 +78,9 @@ public class SerializableArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(in);
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
for(int index = 0; index < this.value.length; index++) {
try {
this.value[index] = (Serializable) str.readObject();
@ -91,9 +91,9 @@ public class SerializableArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(out);
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]);
}
@ -103,4 +103,89 @@ public class SerializableArrayTag extends Tag {
public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;
public DataInputInputStream(DataInput in) {
this.in = in;
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
}
}

View File

@ -45,8 +45,8 @@ public class SerializableTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
ObjectInputStream str = new ObjectInputStream(in);
public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try {
this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
@ -55,8 +55,8 @@ public class SerializableTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(out);
public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value);
}
@ -64,4 +64,89 @@ public class SerializableTag extends Tag {
public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;
public DataInputInputStream(DataInput in) {
this.in = in;
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
}
}

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -80,7 +80,7 @@ public class ShortArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new short[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readShort();
@ -88,7 +88,7 @@ public class ShortArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeShort(this.value[index]);

View File

@ -2,8 +2,8 @@ package org.spacehq.opennbt.tag.builtin.custom;
import org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
@ -80,7 +80,7 @@ public class StringArrayTag extends Tag {
}
@Override
public void read(DataInputStream in) throws IOException {
public void read(DataInput in) throws IOException {
this.value = new String[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readUTF();
@ -88,7 +88,7 @@ public class StringArrayTag extends Tag {
}
@Override
public void write(DataOutputStream out) throws IOException {
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeUTF(this.value[index]);