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 { 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. * @param path Path of the file.
* @return The read compound tag. * @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. * @param file File to read from.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(File file) throws IOException { 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. * Reads the root CompoundTag from the given file.
* *
* @param path Path of the file. * @param path Path of the file.
* @param compressed Whether the NBT file is compressed. * @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static CompoundTag readFile(String path, boolean compressed) throws IOException { public static CompoundTag readFile(String path, boolean compressed, boolean littleEndian) throws IOException {
return readFile(new File(path), compressed); return readFile(new File(path), compressed, littleEndian);
} }
/** /**
* Reads the root CompoundTag from the given file. * Reads the root CompoundTag from the given file.
* *
* @param file File to read from. * @param file File to read from.
* @param compressed Whether the NBT file is compressed. * @param compressed Whether the NBT file is compressed.
* @param littleEndian Whether the NBT file is little endian.
* @return The read compound tag. * @return The read compound tag.
* @throws java.io.IOException If an I/O error occurs. * @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); InputStream in = new FileInputStream(file);
if(compressed) { if(compressed) {
in = new GZIPInputStream(in); in = new GZIPInputStream(in);
} }
Tag tag = readTag(in); Tag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) { if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a 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 tag Tag to write.
* @param path Path to write to. * @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 tag Tag to write.
* @param file File to write to. * @param file File to write to.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, File file) throws IOException { 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. * Writes the given root CompoundTag to the given file.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param path Path to write to. * @param path Path to write to.
* @param compressed Whether the NBT file should be compressed. * @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. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException { public static void writeFile(CompoundTag tag, String path, boolean compressed, boolean littleEndian) throws IOException {
writeFile(tag, new File(path), compressed); writeFile(tag, new File(path), compressed, littleEndian);
} }
/** /**
* Writes the given root CompoundTag to the given file. * Writes the given root CompoundTag to the given file.
* *
* @param tag Tag to write. * @param tag Tag to write.
* @param file File to write to. * @param file File to write to.
* @param compressed Whether the NBT file should be compressed. * @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. * @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.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) { if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
@ -125,50 +129,355 @@ public class NBTIO {
out = new GZIPOutputStream(out); out = new GZIPOutputStream(out);
} }
writeTag(out, tag); writeTag(out, tag, littleEndian);
out.close(); 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. * 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. * @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static Tag readTag(InputStream in) throws IOException { public static Tag readTag(InputStream in, boolean littleEndian) throws IOException {
DataInputStream dataIn = new DataInputStream(in); return readTag((DataInput) (littleEndian ? new LittleEndianDataInputStream(in) : 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;
} }
/** /**
* Writes a tag to an output stream. * Reads an NBT tag.
* *
* @param out Output stream to write to. * @param in Data input to read from.
* @param tag Tag to write. * @return The read tag, or null if the tag is an end tag.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public static void writeTag(OutputStream out, Tag tag) throws IOException { public static Tag readTag(DataInput in) throws IOException {
DataOutputStream dataOut = new DataOutputStream(out); int id = in.readUnsignedByte();
if(id == 0) {
return null;
}
dataOut.writeByte(TagRegistry.getIdFor(tag.getClass())); String name = in.readUTF();
dataOut.writeUTF(tag.getName()); Tag tag;
tag.write(dataOut);
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; package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -78,13 +78,13 @@ public class ByteArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new byte[in.readInt()]; this.value = new byte[in.readInt()];
in.readFully(this.value); in.readFully(this.value);
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
out.write(this.value); out.write(this.value);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package org.spacehq.opennbt.tag.builtin; package org.spacehq.opennbt.tag.builtin;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array; import java.lang.reflect.Array;
@ -45,7 +45,7 @@ public abstract class Tag implements Cloneable {
* @param in Stream to write to. * @param in Stream to write to.
* @throws java.io.IOException If an I/O error occurs. * @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. * Writes this tag to an output stream.
@ -53,7 +53,7 @@ public abstract class Tag implements Cloneable {
* @param out Stream to write to. * @param out Stream to write to.
* @throws java.io.IOException If an I/O error occurs. * @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 @Override
public abstract Tag clone(); 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 org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -80,7 +80,7 @@ public class DoubleArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new double[in.readInt()]; this.value = new double[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readDouble(); this.value[index] = in.readDouble();
@ -88,7 +88,7 @@ public class DoubleArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput 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++) {
out.writeDouble(this.value[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 org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -80,7 +80,7 @@ public class FloatArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new float[in.readInt()]; this.value = new float[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readFloat(); this.value[index] = in.readFloat();
@ -88,7 +88,7 @@ public class FloatArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput 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++) {
out.writeFloat(this.value[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 org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -80,7 +80,7 @@ public class LongArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new long[in.readInt()]; this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong(); this.value[index] = in.readLong();
@ -88,7 +88,7 @@ public class LongArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput 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++) {
out.writeLong(this.value[index]); out.writeLong(this.value[index]);

View File

@ -78,9 +78,9 @@ public class SerializableArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()]; 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++) { for(int index = 0; index < this.value.length; index++) {
try { try {
this.value[index] = (Serializable) str.readObject(); this.value[index] = (Serializable) str.readObject();
@ -91,9 +91,9 @@ public class SerializableArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length); 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++) { for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]); str.writeObject(this.value[index]);
} }
@ -103,4 +103,89 @@ public class SerializableArrayTag extends Tag {
public SerializableArrayTag clone() { public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue()); 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 @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(in); ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try { try {
this.value = (Serializable) str.readObject(); this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
@ -55,8 +55,8 @@ public class SerializableTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(out); ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value); str.writeObject(this.value);
} }
@ -64,4 +64,89 @@ public class SerializableTag extends Tag {
public SerializableTag clone() { public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue()); 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 org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -80,7 +80,7 @@ public class ShortArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput in) throws IOException {
this.value = new short[in.readInt()]; this.value = new short[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readShort(); this.value[index] = in.readShort();
@ -88,7 +88,7 @@ public class ShortArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput 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++) {
out.writeShort(this.value[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 org.spacehq.opennbt.tag.builtin.Tag;
import java.io.DataInputStream; import java.io.DataInput;
import java.io.DataOutputStream; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
/** /**
@ -80,7 +80,7 @@ public class StringArrayTag extends Tag {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInput 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++) {
this.value[index] = in.readUTF(); this.value[index] = in.readUTF();
@ -88,7 +88,7 @@ public class StringArrayTag extends Tag {
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutput 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++) {
out.writeUTF(this.value[index]); out.writeUTF(this.value[index]);