Class FixedOutput

java.lang.Object
java.io.OutputStream
com.prineside.kryo.FixedOutput
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class FixedOutput extends OutputStream
An OutputStream that buffers data in a byte array and optionally flushes to another OutputStream. Utility methods are provided for efficiently writing primitive types and strings. Encoding of integers: BIG_ENDIAN is used for storing fixed native size integer values LITTLE_ENDIAN is used for a variable length encoding of integer values
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
     
    protected int
     
    protected int
     
    protected OutputStream
     
    protected int
     
    protected long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an uninitialized Output.
    FixedOutput(byte[] buffer)
    Creates a new Output for writing to a byte array.
    FixedOutput(byte[] buffer, int maxBufferSize)
    Creates a new Output for writing to a byte array.
    FixedOutput(int bufferSize)
    Creates a new Output for writing to a byte array.
    FixedOutput(int bufferSize, int maxBufferSize)
    Creates a new Output for writing to a byte array.
    FixedOutput(OutputStream outputStream)
    Creates a new Output for writing to an OutputStream.
    FixedOutput(OutputStream outputStream, int bufferSize)
    Creates a new Output for writing to an OutputStream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Sets the position and total to zero.
    void
    Flushes any buffered bytes and closes the underlying OutputStream, if any.
    void
    Writes the buffered bytes to the underlying OutputStream, if any.
    byte[]
    Returns the buffer.
     
    static int
    intLength(int value, boolean optimizePositive)
    Returns the number of bytes that would be written with writeInt(int, boolean).
    static int
    longLength(long value, boolean optimizePositive)
    Returns the number of bytes that would be written with writeLong(long, boolean).
    final int
    Returns the current position in the buffer.
    protected boolean
    require(int required)
     
    void
    setBuffer(byte[] buffer)
    Sets the buffer that will be written to.
    void
    setBuffer(byte[] buffer, int maxBufferSize)
    Sets the buffer that will be written to.
    void
    Sets a new OutputStream.
    void
    setPosition(int position)
    Sets the current position in the buffer.
    byte[]
    Returns a new byte array containing the bytes currently in the buffer between zero and position().
    final long
    Returns the total number of bytes written.
    void
    write(byte[] bytes)
    Writes the bytes.
    void
    write(byte[] bytes, int offset, int length)
    Writes the bytes.
    void
    write(int value)
    Writes a byte.
    void
    Writes a string that is known to contain only ASCII characters.
    void
    writeBoolean(boolean value)
    Writes a 1 byte boolean.
    void
    writeByte(byte value)
     
    void
    writeByte(int value)
     
    void
    writeBytes(byte[] bytes)
    Writes the bytes.
    void
    writeBytes(byte[] bytes, int offset, int count)
    Writes the bytes.
    void
    writeChar(char value)
    Writes a 2 byte char.
    void
    writeChars(char[] object)
    Bulk output of a char array.
    void
    writeDouble(double value)
    Writes an 8 byte double.
    int
    writeDouble(double value, double precision, boolean optimizePositive)
    Writes a 1-9 byte double with reduced precision.
    void
    writeDoubles(double[] object)
    Bulk output of a double array.
    void
    writeFloat(float value)
    Writes a 4 byte float.
    int
    writeFloat(float value, float precision, boolean optimizePositive)
    Writes a 1-5 byte float with reduced precision.
    void
    writeFloats(float[] object)
    Bulk output of a float array.
    void
    writeInt(int value)
    Writes a 4 byte int.
    int
    writeInt(int value, boolean optimizePositive)
    Writes a 1-5 byte int.
    void
    writeInts(int[] object)
    Bulk output of an int array.
    void
    writeInts(int[] object, boolean optimizePositive)
    Bulk output of an int array.
    void
    writeLong(long value)
    Writes an 8 byte long.
    int
    writeLong(long value, boolean optimizePositive)
    Writes a 1-9 byte long.
    void
    writeLongs(long[] object)
    Bulk output of an long array.
    void
    writeLongs(long[] object, boolean optimizePositive)
    Bulk output of an long array.
    void
    writeShort(int value)
    Writes a 2 byte short.
    void
    writeShorts(short[] object)
    Bulk output of a short array.
    void
    Writes the length and CharSequence as UTF8, or null.
    void
    Writes the length and string, or null.
    int
    writeVarInt(int value, boolean optimizePositive)
    Writes a 1-5 byte int.
    int
    writeVarLong(long value, boolean optimizePositive)
    Writes a 1-9 byte long.

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • maxCapacity

      protected int maxCapacity
    • total

      protected long total
    • position

      protected int position
    • capacity

      protected int capacity
    • buffer

      protected byte[] buffer
    • outputStream

      protected OutputStream outputStream
  • Constructor Details

    • FixedOutput

      public FixedOutput()
      Creates an uninitialized Output. setBuffer(byte[], int) must be called before the Output is used.
    • FixedOutput

      public FixedOutput(int bufferSize)
      Creates a new Output for writing to a byte array.
      Parameters:
      bufferSize - The initial and maximum size of the buffer. An exception is thrown if this size is exceeded.
    • FixedOutput

      public FixedOutput(int bufferSize, int maxBufferSize)
      Creates a new Output for writing to a byte array.
      Parameters:
      bufferSize - The initial size of the buffer.
      maxBufferSize - The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
    • FixedOutput

      public FixedOutput(byte[] buffer)
      Creates a new Output for writing to a byte array.
      See Also:
    • FixedOutput

      public FixedOutput(byte[] buffer, int maxBufferSize)
      Creates a new Output for writing to a byte array.
      See Also:
    • FixedOutput

      public FixedOutput(OutputStream outputStream)
      Creates a new Output for writing to an OutputStream. A buffer size of 4096 is used.
    • FixedOutput

      public FixedOutput(OutputStream outputStream, int bufferSize)
      Creates a new Output for writing to an OutputStream.
  • Method Details

    • getOutputStream

      public OutputStream getOutputStream()
    • setOutputStream

      public void setOutputStream(OutputStream outputStream)
      Sets a new OutputStream. The position and total are reset, discarding any buffered bytes.
      Parameters:
      outputStream - May be null.
    • setBuffer

      public void setBuffer(byte[] buffer)
      Sets the buffer that will be written to. setBuffer(byte[], int) is called with the specified buffer's length as the maxBufferSize.
    • setBuffer

      public void setBuffer(byte[] buffer, int maxBufferSize)
      Sets the buffer that will be written to. The position and total are reset, discarding any buffered bytes. The OutputStream is set to null.
      Parameters:
      maxBufferSize - The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown.
    • getBuffer

      public byte[] getBuffer()
      Returns the buffer. The bytes between zero and position() are the data that has been written.
    • toBytes

      public byte[] toBytes()
      Returns a new byte array containing the bytes currently in the buffer between zero and position().
    • position

      public final int position()
      Returns the current position in the buffer. This is the number of bytes that have not been flushed.
    • setPosition

      public void setPosition(int position)
      Sets the current position in the buffer.
    • total

      public final long total()
      Returns the total number of bytes written. This may include bytes that have not been flushed.
    • clear

      public void clear()
      Sets the position and total to zero.
    • require

      protected boolean require(int required) throws com.esotericsoftware.kryo.KryoException
      Returns:
      true if the buffer has been resized.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • flush

      public void flush() throws com.esotericsoftware.kryo.KryoException
      Writes the buffered bytes to the underlying OutputStream, if any.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • close

      public void close() throws com.esotericsoftware.kryo.KryoException
      Flushes any buffered bytes and closes the underlying OutputStream, if any.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • write

      public void write(int value) throws com.esotericsoftware.kryo.KryoException
      Writes a byte.
      Specified by:
      write in class OutputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • write

      public void write(byte[] bytes) throws com.esotericsoftware.kryo.KryoException
      Writes the bytes. Note the byte[] length is not written.
      Overrides:
      write in class OutputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • write

      public void write(byte[] bytes, int offset, int length) throws com.esotericsoftware.kryo.KryoException
      Writes the bytes. Note the byte[] length is not written.
      Overrides:
      write in class OutputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeByte

      public void writeByte(byte value) throws com.esotericsoftware.kryo.KryoException
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeByte

      public void writeByte(int value) throws com.esotericsoftware.kryo.KryoException
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeBytes

      public void writeBytes(byte[] bytes) throws com.esotericsoftware.kryo.KryoException
      Writes the bytes. Note the byte[] length is not written.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeBytes

      public void writeBytes(byte[] bytes, int offset, int count) throws com.esotericsoftware.kryo.KryoException
      Writes the bytes. Note the byte[] length is not written.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeInt

      public void writeInt(int value) throws com.esotericsoftware.kryo.KryoException
      Writes a 4 byte int. Uses BIG_ENDIAN byte order.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeInt

      public int writeInt(int value, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-5 byte int. This stream may consider such a variable length encoding request as a hint. It is not guaranteed that a variable length encoding will be really used. The stream may decide to use native-sized integer representation for efficiency reasons.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeVarInt

      public int writeVarInt(int value, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-5 byte int. It is guaranteed that a varible length encoding will be used.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeString

      public void writeString(String value) throws com.esotericsoftware.kryo.KryoException
      Writes the length and string, or null. Short strings are checked and if ASCII they are written more efficiently, else they are written as UTF8. If a string is known to be ASCII, writeAscii(String) may be used. The string can be read using FixedInput.readString() or FixedInput.readStringBuilder().
      Parameters:
      value - May be null.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeString

      public void writeString(CharSequence value) throws com.esotericsoftware.kryo.KryoException
      Writes the length and CharSequence as UTF8, or null. The string can be read using FixedInput.readString() or FixedInput.readStringBuilder().
      Parameters:
      value - May be null.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeAscii

      public void writeAscii(String value) throws com.esotericsoftware.kryo.KryoException
      Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted. Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more efficient than writeString(String). The string can be read using FixedInput.readString() or FixedInput.readStringBuilder().
      Parameters:
      value - May be null.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeFloat

      public void writeFloat(float value) throws com.esotericsoftware.kryo.KryoException
      Writes a 4 byte float.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeFloat

      public int writeFloat(float value, float precision, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-5 byte float with reduced precision.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeShort

      public void writeShort(int value) throws com.esotericsoftware.kryo.KryoException
      Writes a 2 byte short. Uses BIG_ENDIAN byte order.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeLong

      public void writeLong(long value) throws com.esotericsoftware.kryo.KryoException
      Writes an 8 byte long. Uses BIG_ENDIAN byte order.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeLong

      public int writeLong(long value, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-9 byte long. This stream may consider such a variable length encoding request as a hint. It is not guaranteed that a variable length encoding will be really used. The stream may decide to use native-sized integer representation for efficiency reasons.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeVarLong

      public int writeVarLong(long value, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-9 byte long. It is guaranteed that a varible length encoding will be used.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeBoolean

      public void writeBoolean(boolean value) throws com.esotericsoftware.kryo.KryoException
      Writes a 1 byte boolean.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeChar

      public void writeChar(char value) throws com.esotericsoftware.kryo.KryoException
      Writes a 2 byte char. Uses BIG_ENDIAN byte order.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeDouble

      public void writeDouble(double value) throws com.esotericsoftware.kryo.KryoException
      Writes an 8 byte double.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeDouble

      public int writeDouble(double value, double precision, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Writes a 1-9 byte double with reduced precision.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • intLength

      public static int intLength(int value, boolean optimizePositive)
      Returns the number of bytes that would be written with writeInt(int, boolean).
    • longLength

      public static int longLength(long value, boolean optimizePositive)
      Returns the number of bytes that would be written with writeLong(long, boolean).
    • writeInts

      public void writeInts(int[] object, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Bulk output of an int array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeLongs

      public void writeLongs(long[] object, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Bulk output of an long array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeInts

      public void writeInts(int[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of an int array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeLongs

      public void writeLongs(long[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of an long array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeFloats

      public void writeFloats(float[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of a float array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeShorts

      public void writeShorts(short[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of a short array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeChars

      public void writeChars(char[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of a char array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • writeDoubles

      public void writeDoubles(double[] object) throws com.esotericsoftware.kryo.KryoException
      Bulk output of a double array.
      Throws:
      com.esotericsoftware.kryo.KryoException