/** * Creates a new {@link NBTInputStream}, which sources its data from the specified input stream. A flag must be passed which indicates if the stream is compressed with GZIP or not. * * @param is The input stream. * @param compressed A flag indicating if the stream is compressed. * @param endianness Whether to read numbers from the InputStream with little endian encoding. * @throws java.io.IOException if an I/O error occurs. */ public NBTInputStream(InputStream is, boolean compressed, ByteOrder endianness) throws IOException { this.is = new EndianSwitchableInputStream(compressed ? new GZIPInputStream(is) : is, endianness); }
public void close() throws IOException { is.close(); }
public String readUTF() throws IOException { return getBackingStream().readUTF(); } }
return new ByteTag(name, is.readByte()); return new ShortTag(name, is.readShort()); return new IntTag(name, is.readInt()); return new LongTag(name, is.readLong()); return new FloatTag(name, is.readFloat()); return new DoubleTag(name, is.readDouble()); int length = is.readInt(); byte[] bytes = new byte[length]; is.readFully(bytes); return new ByteArrayTag(name, bytes); length = is.readShort(); bytes = new byte[length]; is.readFully(bytes); return new StringTag(name, new String(bytes, NBTConstants.CHARSET.name())); TagType childType = TagType.getById(is.readByte()); length = is.readInt(); length = is.readInt(); int[] ints = new int[length]; for (int i = 0; i < length; i++) { ints[i] = is.readInt();
/** * Reads an NBT {@link Tag} from the stream. * * @param depth The depth of this tag. * @return The tag that was read. * @throws java.io.IOException if an I/O error occurs. */ private Tag readTag(int depth) throws IOException { int typeId = is.readByte() & 0xFF; TagType type = TagType.getById(typeId); String name; if (type != TagType.TAG_END) { int nameLength = is.readShort() & 0xFFFF; byte[] nameBytes = new byte[nameLength]; is.readFully(nameBytes); name = new String(nameBytes, NBTConstants.CHARSET.name()); } else { name = ""; } return readTagPayload(type, name, depth); }
@Test public void testWriteLEUnsignedShort() throws IOException { int unsigned = Short.MAX_VALUE + 5; char testChar = 'b'; ByteArrayOutputStream rawOutput = new ByteArrayOutputStream(); EndianSwitchableOutputStream output = new EndianSwitchableOutputStream(rawOutput, ByteOrder.LITTLE_ENDIAN); output.writeShort(unsigned); output.writeChar(testChar); EndianSwitchableInputStream input = new EndianSwitchableInputStream(new ByteArrayInputStream(rawOutput.toByteArray()), ByteOrder.LITTLE_ENDIAN); assertEquals(unsigned, input.readUnsignedShort()); assertEquals(testChar, input.readChar()); } }
/** * @return whether this NBTInputStream reads numbers in little-endian format. */ public ByteOrder getByteOrder() { return is.getEndianness(); } }
public float readFloat() throws IOException { int result = readInt(); if (endianness == ByteOrder.LITTLE_ENDIAN) { result = Integer.reverseBytes(result); } return Float.intBitsToFloat(result); }
public double readDouble() throws IOException { long result = readLong(); if (endianness == ByteOrder.LITTLE_ENDIAN) { result = Long.reverseBytes(result); } return Double.longBitsToDouble(result); }
return new ByteTag(name, is.readByte()); return new ShortTag(name, is.readShort()); return new IntTag(name, is.readInt()); return new LongTag(name, is.readLong()); return new FloatTag(name, is.readFloat()); return new DoubleTag(name, is.readDouble()); int length = is.readInt(); byte[] bytes = new byte[length]; is.readFully(bytes); return new ByteArrayTag(name, bytes); length = is.readShort(); bytes = new byte[length]; is.readFully(bytes); return new StringTag(name, new String(bytes, NBTConstants.CHARSET.name())); TagType childType = TagType.getById(is.readByte()); length = is.readInt(); length = is.readInt(); int[] ints = new int[length]; for (int i = 0; i < length; i++) { ints[i] = is.readInt();
/** * Reads an NBT {@link Tag} from the stream. * * @param depth The depth of this tag. * @return The tag that was read. * @throws java.io.IOException if an I/O error occurs. */ private Tag readTag(int depth) throws IOException { int typeId = is.readByte() & 0xFF; TagType type = TagType.getById(typeId); String name; if (type != TagType.TAG_END) { int nameLength = is.readShort() & 0xFFFF; byte[] nameBytes = new byte[nameLength]; is.readFully(nameBytes); name = new String(nameBytes, NBTConstants.CHARSET.name()); } else { name = ""; } return readTagPayload(type, name, depth); }
/** * @return whether this NBTInputStream reads numbers in little-endian format. */ public ByteOrder getByteOrder() { return is.getEndianness(); } }
public float readFloat() throws IOException { int result = readInt(); if (endianness == ByteOrder.LITTLE_ENDIAN) { result = Integer.reverseBytes(result); } return Float.intBitsToFloat(result); }
public double readDouble() throws IOException { long result = readLong(); if (endianness == ByteOrder.LITTLE_ENDIAN) { result = Long.reverseBytes(result); } return Double.longBitsToDouble(result); }
return new ByteTag(name, is.readByte()); return new ShortTag(name, is.readShort()); return new IntTag(name, is.readInt()); return new LongTag(name, is.readLong()); return new FloatTag(name, is.readFloat()); return new DoubleTag(name, is.readDouble()); int length = is.readInt(); byte[] bytes = new byte[length]; is.readFully(bytes); return new ByteArrayTag(name, bytes); length = is.readShort(); bytes = new byte[length]; is.readFully(bytes); return new StringTag(name, new String(bytes, NBTConstants.CHARSET.name())); TagType childType = TagType.getById(is.readByte()); length = is.readInt(); length = is.readInt(); int[] ints = new int[length]; for (int i = 0; i < length; i++) { ints[i] = is.readInt();
public String readUTF() throws IOException { return getBackingStream().readUTF(); } }
/** * Reads an NBT {@link Tag} from the stream. * * @param depth The depth of this tag. * @return The tag that was read. * @throws java.io.IOException if an I/O error occurs. */ private Tag readTag(int depth) throws IOException { int typeId = is.readByte() & 0xFF; TagType type = TagType.getById(typeId); String name; if (type != TagType.TAG_END) { int nameLength = is.readShort() & 0xFFFF; byte[] nameBytes = new byte[nameLength]; is.readFully(nameBytes); name = new String(nameBytes, NBTConstants.CHARSET.name()); } else { name = ""; } return readTagPayload(type, name, depth); }
/** * Creates a new {@link NBTInputStream}, which sources its data from the specified input stream. A flag must be passed which indicates if the stream is compressed with GZIP or not. * * @param is The input stream. * @param compressed A flag indicating if the stream is compressed. * @param endianness Whether to read numbers from the InputStream with little endian encoding. * @throws java.io.IOException if an I/O error occurs. */ public NBTInputStream(InputStream is, boolean compressed, ByteOrder endianness) throws IOException { this.is = new EndianSwitchableInputStream(compressed ? new GZIPInputStream(is) : is, endianness); }
/** * @return whether this NBTInputStream reads numbers in little-endian format. */ public ByteOrder getByteOrder() { return is.getEndianness(); } }
public float readFloat() throws IOException { int result = readInt(); if (endianness == ByteOrder.LITTLE_ENDIAN) { result = Integer.reverseBytes(result); } return Float.intBitsToFloat(result); }