Class ByteBufferOutput

java.lang.Object
java.io.OutputStream
com.esotericsoftware.kryo.io.Output
com.esotericsoftware.kryo.io.ByteBufferOutput
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ByteBufferOutput extends Output
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.
  • Field Details

    • niobuffer

      protected ByteBuffer niobuffer
    • varIntsEnabled

      protected boolean varIntsEnabled
    • byteOrder

      ByteOrder byteOrder
    • nativeOrder

      protected static final ByteOrder nativeOrder
  • Constructor Details

    • ByteBufferOutput

      public ByteBufferOutput()
      Creates an uninitialized Output. A buffer must be set before the Output is used.
      See Also:
    • ByteBufferOutput

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

      public ByteBufferOutput(int bufferSize, int maxBufferSize)
      Creates a new Output for writing to a direct ByteBuffer.
      Parameters:
      bufferSize - The initial size of the buffer.
      maxBufferSize - The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown.
    • ByteBufferOutput

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

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

      public ByteBufferOutput(ByteBuffer buffer)
      Creates a new Output for writing to a ByteBuffer.
    • ByteBufferOutput

      public ByteBufferOutput(ByteBuffer buffer, int maxBufferSize)
      Creates a new Output for writing to a ByteBuffer.
      Parameters:
      maxBufferSize - The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.
  • Method Details

    • order

      public ByteOrder order()
    • order

      public void order(ByteOrder byteOrder)
    • getOutputStream

      public OutputStream getOutputStream()
      Overrides:
      getOutputStream in class Output
    • setOutputStream

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

      public void setBuffer(ByteBuffer buffer)
      Sets the buffer that will be written to. maxCapacity is set to the specified buffer's capacity.
      See Also:
    • setBuffer

      public void setBuffer(ByteBuffer buffer, int maxBufferSize)
      Sets the buffer that will be written to. The byte order, position and capacity are set to match the specified buffer. The total is set to 0. The OutputStream is set to null.
      Parameters:
      maxBufferSize - The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.
    • getByteBuffer

      public ByteBuffer getByteBuffer()
      Returns the buffer. The bytes between zero and Output.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 Output.position().
      Overrides:
      toBytes in class Output
    • setPosition

      public void setPosition(int position)
      Sets the current position in the buffer.
      Overrides:
      setPosition in class Output
    • clear

      public void clear()
      Sets the position and total to zero.
      Overrides:
      clear in class Output
    • require

      protected boolean require(int required) throws KryoException
      Overrides:
      require in class Output
      Returns:
      true if the buffer has been resized.
      Throws:
      KryoException
    • flush

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

      public void close() throws 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 Output
      Throws:
      KryoException
    • write

      public void write(int value) throws KryoException
      Writes a byte.
      Overrides:
      write in class Output
      Throws:
      KryoException
    • write

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

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

      public void writeByte(byte value) throws KryoException
      Overrides:
      writeByte in class Output
      Throws:
      KryoException
    • writeByte

      public void writeByte(int value) throws KryoException
      Overrides:
      writeByte in class Output
      Throws:
      KryoException
    • writeBytes

      public void writeBytes(byte[] bytes) throws KryoException
      Writes the bytes. Note the byte[] length is not written.
      Overrides:
      writeBytes in class Output
      Throws:
      KryoException
    • writeBytes

      public void writeBytes(byte[] bytes, int offset, int count) throws KryoException
      Writes the bytes. Note the byte[] length is not written.
      Overrides:
      writeBytes in class Output
      Throws:
      KryoException
    • writeInt

      public void writeInt(int value) throws KryoException
      Writes a 4 byte int.
      Overrides:
      writeInt in class Output
      Throws:
      KryoException
    • writeInt

      public int writeInt(int value, boolean optimizePositive) throws KryoException
      Description copied from class: Output
      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.
      Overrides:
      writeInt in class Output
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
      Throws:
      KryoException
    • writeVarInt

      public int writeVarInt(int val, boolean optimizePositive) throws KryoException
      Description copied from class: Output
      Writes a 1-5 byte int. It is guaranteed that a varible length encoding will be used.
      Overrides:
      writeVarInt in class Output
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
      Throws:
      KryoException
    • writeString

      public void writeString(String value) throws 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 Input.readString() or Input.readStringBuilder().
      Overrides:
      writeString in class Output
      Parameters:
      value - May be null.
      Throws:
      KryoException
    • writeString

      public void writeString(CharSequence value) throws KryoException
      Writes the length and CharSequence as UTF8, or null. The string can be read using Input.readString() or Input.readStringBuilder().
      Overrides:
      writeString in class Output
      Parameters:
      value - May be null.
      Throws:
      KryoException
    • writeAscii

      public void writeAscii(String value) throws 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 Input.readString() or Input.readStringBuilder().
      Overrides:
      writeAscii in class Output
      Parameters:
      value - May be null.
      Throws:
      KryoException
    • writeUtf8Length

      private void writeUtf8Length(int value)
      Writes the length of a string, which is a variable length encoded int except the first byte uses bit 8 to denote UTF8 and bit 7 to denote if another byte is present.
    • writeString_slow

      private void writeString_slow(CharSequence value, int charCount, int charIndex)
    • writeAscii_slow

      private void writeAscii_slow(String value, int charCount) throws KryoException
      Throws:
      KryoException
    • writeFloat

      public void writeFloat(float value) throws KryoException
      Writes a 4 byte float.
      Overrides:
      writeFloat in class Output
      Throws:
      KryoException
    • writeFloat

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

      public void writeShort(int value) throws KryoException
      Writes a 2 byte short.
      Overrides:
      writeShort in class Output
      Throws:
      KryoException
    • writeLong

      public void writeLong(long value) throws KryoException
      Writes an 8 byte long.
      Overrides:
      writeLong in class Output
      Throws:
      KryoException
    • writeLong

      public int writeLong(long value, boolean optimizePositive) throws KryoException
      Description copied from class: Output
      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.
      Overrides:
      writeLong in class Output
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      KryoException
    • writeVarLong

      public int writeVarLong(long value, boolean optimizePositive) throws KryoException
      Description copied from class: Output
      Writes a 1-9 byte long. It is guaranteed that a varible length encoding will be used.
      Overrides:
      writeVarLong in class Output
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      KryoException
    • writeLongS

      public int writeLongS(long value, boolean optimizePositive) throws KryoException
      Writes a 1-9 byte long.
      Parameters:
      optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).
      Throws:
      KryoException
    • writeBoolean

      public void writeBoolean(boolean value) throws KryoException
      Writes a 1 byte boolean.
      Overrides:
      writeBoolean in class Output
      Throws:
      KryoException
    • writeChar

      public void writeChar(char value) throws KryoException
      Writes a 2 byte char.
      Overrides:
      writeChar in class Output
      Throws:
      KryoException
    • writeDouble

      public void writeDouble(double value) throws KryoException
      Writes an 8 byte double.
      Overrides:
      writeDouble in class Output
      Throws:
      KryoException
    • writeDouble

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

      public void writeInts(int[] object) throws KryoException
      Bulk output of an int array.
      Overrides:
      writeInts in class Output
      Throws:
      KryoException
    • writeLongs

      public void writeLongs(long[] object) throws KryoException
      Bulk output of an long array.
      Overrides:
      writeLongs in class Output
      Throws:
      KryoException
    • writeFloats

      public void writeFloats(float[] object) throws KryoException
      Bulk output of a float array.
      Overrides:
      writeFloats in class Output
      Throws:
      KryoException
    • writeShorts

      public void writeShorts(short[] object) throws KryoException
      Bulk output of a short array.
      Overrides:
      writeShorts in class Output
      Throws:
      KryoException
    • writeChars

      public void writeChars(char[] object) throws KryoException
      Bulk output of a char array.
      Overrides:
      writeChars in class Output
      Throws:
      KryoException
    • writeDoubles

      public void writeDoubles(double[] object) throws KryoException
      Bulk output of a double array.
      Overrides:
      writeDoubles in class Output
      Throws:
      KryoException
    • isNativeOrder

      private boolean isNativeOrder()
    • getVarIntsEnabled

      public boolean getVarIntsEnabled()
      Return current setting for variable length encoding of integers
      Returns:
      current setting for variable length encoding of integers
    • setVarIntsEnabled

      public void setVarIntsEnabled(boolean varIntsEnabled)
      Controls if a variable length encoding for integer types should be used when serializers suggest it.
      Parameters:
      varIntsEnabled -