Class 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.
    Author:
    Roman Levenstein
    • Field Detail

      • varIntsEnabled

        protected boolean varIntsEnabled
      • nativeOrder

        protected static final ByteOrder nativeOrder
    • Constructor Detail

      • ByteBufferOutput

        public ByteBufferOutput()
        Creates an uninitialized Output. A buffer must be set before the Output is used.
        See Also:
        setBuffer(ByteBuffer, int)
      • 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 Detail

      • order

        public void order​(ByteOrder byteOrder)
      • 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(ByteBuffer, int)
      • 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
      • 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
      • 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 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
        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
        optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
        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
      • 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
        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
        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
      • 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
      • 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 -