Class BinaryOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.aurumsmods.littlebigio.BinaryOutputStream
All Implemented Interfaces:
ByteOrdered, Closeable, DataOutput, Flushable, AutoCloseable

public class BinaryOutputStream extends FilterOutputStream implements DataOutput, ByteOrdered
A binary output stream provides methods to write primitive data types from an underlying input stream. The order in which the bytes are interpreted can be either little endian or big endian (see ByteOrder).

This implements DataOutput but intentionally violates the specifications prescribed by the contract. This is because DataInput explicitly states the implementation of big endian byte ordering.

BinaryOutputStream is not necessarily thread-safe due to it using a buffer to write multiple bytes at once.

  • Field Details

    • written

      protected int written
      Number of bytes written to the output stream. Caps at Integer.MAX_VALUE
  • Constructor Details

  • Method Details

    • getOrder

      public ByteOrder getOrder()
      Returns the ByteOrder.
      Specified by:
      getOrder in interface ByteOrdered
      Returns:
      the ByteOrder.
    • setOrder

      public void setOrder(ByteOrder byteOrder)
      Sets the ByteOrder to the specified value. If the value is null, a NullPointerException is thrown.
      Specified by:
      setOrder in interface ByteOrdered
      Parameters:
      byteOrder - the ByteOrder.
      Throws:
      NullPointerException - if byteOrder is null.
    • incWritten

      protected void incWritten(int value)
      Increases the counter of written bytes by the specified value. If the counter overflows, the value will be capped at Integer.MAX_VALUE.
      Parameters:
      value - the number to increase the counter by.
    • size

      public final int size()
      Returns the number of bytes written to this stream so far. If the value overflows, it will be capped at Integer.MAX_VALUE.
      Returns:
      the number of bytes written to this stream so far.
    • write

      public void write(int b) throws IOException
      Writes the specified byte to this output stream. This is done by calling the write method of its underlying output stream with the same argument.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the byte.
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Writes len bytes from the specified byte array starting at offset off to this output stream. This is done by calling the write method of its underlying output stream with the same arguments.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the data.
      off - the start offset in the data.
      len - the number of bytes to write.
      Throws:
      IOException - if an I/O error occurs.
    • writeBoolean

      public final void writeBoolean(boolean v) throws IOException
      Writes a boolean to the output stream. The value true is written out as (byte) 1; the value false is written out as (byte) 0. If no exception is thrown, written is incremented by 1.
      Specified by:
      writeBoolean in interface DataOutput
      Parameters:
      v - the boolean value to be written.
      Throws:
      IOException - if an I/O error occurs.
    • writeByte

      public final void writeByte(int v) throws IOException
      Writes a byte value to the output stream. If no exception is thrown, written is incremented by 1.
      Specified by:
      writeByte in interface DataOutput
      Parameters:
      v - the byte value.
      Throws:
      IOException - if an I/O error occurs.
    • writeShort

      public final void writeShort(int v) throws IOException
      Writes a short value to the output stream. If no exception is thrown, written is incremented by 2.
      Specified by:
      writeShort in interface DataOutput
      Parameters:
      v - the short value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeChar

      public final void writeChar(int v) throws IOException
      Writes a char value to the output stream. If no exception is thrown, written is incremented by 2.
      Specified by:
      writeChar in interface DataOutput
      Parameters:
      v - the char value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeInt

      public final void writeInt(int v) throws IOException
      Writes a int value to the output stream. If no exception is thrown, written is incremented by 4.
      Specified by:
      writeInt in interface DataOutput
      Parameters:
      v - the int value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeLong

      public final void writeLong(long v) throws IOException
      Writes a long value to the output stream. If no exception is thrown, written is incremented by 8.
      Specified by:
      writeLong in interface DataOutput
      Parameters:
      v - the long value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeFloat

      public final void writeFloat(float v) throws IOException
      Writes a float value to the output stream. If no exception is thrown, written is incremented by 4.
      Specified by:
      writeFloat in interface DataOutput
      Parameters:
      v - the float value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeDouble

      public final void writeDouble(double v) throws IOException
      Writes a double value to the output stream. If no exception is thrown, written is incremented by 8.
      Specified by:
      writeDouble in interface DataOutput
      Parameters:
      v - the double value.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • writeBytes

      public final void writeBytes(String s) throws IOException
      Writes a string to the output stream as a sequence of bytes. For every character in the string the low eight bits are written. If no exception is thrown, the counter written is incremented by the length of s.
      Specified by:
      writeBytes in interface DataOutput
      Parameters:
      s - a String value to be written.
      Throws:
      IOException - if an I/O error occurs.
    • writeChars

      public final void writeChars(String s) throws IOException
      Writes a string to the output stream as a sequence of characters. Each character is written to the data output stream using writeChar(int). If no exception is thrown, written is incremented by twice the length of s.
      Specified by:
      writeChars in interface DataOutput
      Parameters:
      s - a String value to be written.
      Throws:
      IOException - if an I/O error occurs.
    • writeUTF

      public final void writeUTF(String s) throws IOException
      Writes a string to the output stream using modified UTF-8 encoding. This calls DataOutputStream.writeUTF(String). See the class documentation for DataInput for details on the modified UTF-8 format. If no exception is thrown, written is incremented by the amount of encoded bytes written.
      Specified by:
      writeUTF in interface DataOutput
      Parameters:
      s - the String value.
      Throws:
      IOException - if an I/O error occurs.