/** * Creates a new {@link NBTOutputStream}, which will write data to the specified underlying output stream. A flag indicates if the output should be compressed with GZIP or not. * * @param os The output stream. * @param compressed A flag that indicates if the output should be compressed. * @param endianness A flag that indicates if numbers in the output should be output in little-endian format. * @throws java.io.IOException if an I/O error occurs. */ public NBTOutputStream(OutputStream os, boolean compressed, ByteOrder endianness) throws IOException { this.os = new EndianSwitchableOutputStream(compressed ? new GZIPOutputStream(os) : os, endianness); }
/** * Writes a {@code TAG_String} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeStringTagPayload(StringTag tag) throws IOException { byte[] bytes = tag.getValue().getBytes(NBTConstants.CHARSET.name()); os.writeShort(bytes.length); os.write(bytes); }
public void close() throws IOException { os.close(); }
/** * Writes a {@code TAG_Short_Array} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeShortArrayTagPayload(ShortArrayTag tag) throws IOException { short[] shorts = tag.getValue(); os.writeInt(shorts.length); for (int i = 0; i < shorts.length; i++) { os.writeShort(shorts[i]); } }
/** * Writes a tag. * * @param tag The tag to write. * @throws java.io.IOException if an I/O error occurs. */ public void writeTag(Tag<?> tag) throws IOException { String name = tag.getName(); byte[] nameBytes = name.getBytes(NBTConstants.CHARSET.name()); os.writeByte(tag.getType().getId()); os.writeShort(nameBytes.length); os.write(nameBytes); if (tag.getType() == TagType.TAG_END) { throw new IOException("Named TAG_End not permitted."); } writeTagPayload(tag); }
public void writeUTF(String s) throws IOException { getBackingStream().writeUTF(s); } }
@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()); } }
/** * Writes a {@code TAG_List} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ @SuppressWarnings ("unchecked") private void writeListTagPayload(ListTag<?> tag) throws IOException { Class<? extends Tag<?>> clazz = tag.getElementType(); List<Tag<?>> tags = (List<Tag<?>>) tag.getValue(); int size = tags.size(); os.writeByte(TagType.getByTagClass(clazz).getId()); os.writeInt(size); for (Tag<?> tag1 : tags) { writeTagPayload(tag1); } }
/** * Writes a {@code TAG_Short} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeShortTagPayload(ShortTag tag) throws IOException { os.writeShort(tag.getValue()); }
/** * Writes a {@code TAG_Byte} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeByteTagPayload(ByteTag tag) throws IOException { os.writeByte(tag.getValue()); }
/** * Flushes the stream */ public void flush() throws IOException { os.flush(); } }
/** * Writes a {@code TAG_Double} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeDoubleTagPayload(DoubleTag tag) throws IOException { os.writeDouble(tag.getValue()); }
/** * Writes a {@code TAG_Float} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeFloatTagPayload(FloatTag tag) throws IOException { os.writeFloat(tag.getValue()); }
/** * @return whether this NBTInputStream writes numbers in little-endian format. */ public ByteOrder getEndianness() { return os.getEndianness(); }
/** * Writes a tag. * * @param tag The tag to write. * @throws java.io.IOException if an I/O error occurs. */ public void writeTag(Tag<?> tag) throws IOException { String name = tag.getName(); byte[] nameBytes = name.getBytes(NBTConstants.CHARSET.name()); os.writeByte(tag.getType().getId()); os.writeShort(nameBytes.length); os.write(nameBytes); if (tag.getType() == TagType.TAG_END) { throw new IOException("Named TAG_End not permitted."); } writeTagPayload(tag); }
public void writeShort(int i) throws IOException { if (endianness == ByteOrder.LITTLE_ENDIAN) { i = Integer.reverseBytes(i) >> 16; } getBackingStream().writeShort(i); }
/** * Writes a {@code TAG_Short_Array} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ private void writeShortArrayTagPayload(ShortArrayTag tag) throws IOException { short[] shorts = tag.getValue(); os.writeInt(shorts.length); for (int i = 0; i < shorts.length; i++) { os.writeShort(shorts[i]); } }
/** * Writes a {@code TAG_List} tag. * * @param tag The tag. * @throws java.io.IOException if an I/O error occurs. */ @SuppressWarnings ("unchecked") private void writeListTagPayload(ListTag<?> tag) throws IOException { Class<? extends Tag<?>> clazz = tag.getElementType(); List<Tag<?>> tags = (List<Tag<?>>) tag.getValue(); int size = tags.size(); os.writeByte(TagType.getByTagClass(clazz).getId()); os.writeInt(size); for (Tag<?> tag1 : tags) { writeTagPayload(tag1); } }