Class ByteBufferOutput

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.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.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferOutput()
      Creates an uninitialized Output.
      ByteBufferOutput​(int bufferSize)
      Creates a new Output for writing to a direct ByteBuffer.
      ByteBufferOutput​(int bufferSize, int maxBufferSize)
      Creates a new Output for writing to a direct ByteBuffer.
      ByteBufferOutput​(java.io.OutputStream outputStream)
      Creates a new Output for writing to an OutputStream.
      ByteBufferOutput​(java.io.OutputStream outputStream, int bufferSize)
      Creates a new Output for writing to an OutputStream.
      ByteBufferOutput​(java.nio.ByteBuffer buffer)
      Creates a new Output for writing to a ByteBuffer.
      ByteBufferOutput​(java.nio.ByteBuffer buffer, int maxBufferSize)
      Creates a new Output for writing to a ByteBuffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Sets the position and total to zero.
      void close()
      Flushes any buffered bytes and closes the underlying OutputStream, if any.
      void flush()
      Writes the buffered bytes to the underlying OutputStream, if any.
      java.nio.ByteBuffer getByteBuffer()
      Returns the buffer.
      java.io.OutputStream getOutputStream()  
      boolean getVarIntsEnabled()
      Return current setting for variable length encoding of integers
      private boolean isNativeOrder()  
      java.nio.ByteOrder order()  
      void order​(java.nio.ByteOrder byteOrder)  
      protected boolean require​(int required)  
      void setBuffer​(java.nio.ByteBuffer buffer)
      Sets the buffer that will be written to.
      void setBuffer​(java.nio.ByteBuffer buffer, int maxBufferSize)
      Sets the buffer that will be written to.
      void setOutputStream​(java.io.OutputStream outputStream)
      Sets a new OutputStream.
      void setPosition​(int position)
      Sets the current position in the buffer.
      void setVarIntsEnabled​(boolean varIntsEnabled)
      Controls if a variable length encoding for integer types should be used when serializers suggest it.
      byte[] toBytes()
      Returns a new byte array containing the bytes currently in the buffer between zero and Output.position().
      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 writeAscii​(java.lang.String value)
      Writes a string that is known to contain only ASCII characters.
      private void writeAscii_slow​(java.lang.String value, int charCount)  
      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 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.
      int writeLongS​(long value, boolean optimizePositive)
      Writes a 1-9 byte long.
      void writeShort​(int value)
      Writes a 2 byte short.
      void writeShorts​(short[] object)
      Bulk output of a short array.
      void writeString​(java.lang.CharSequence value)
      Writes the length and CharSequence as UTF8, or null.
      void writeString​(java.lang.String value)
      Writes the length and string, or null.
      private void writeString_slow​(java.lang.CharSequence value, int charCount, int charIndex)  
      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.
      int writeVarInt​(int val, 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.lang.Object

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

      • niobuffer

        protected java.nio.ByteBuffer niobuffer
      • varIntsEnabled

        protected boolean varIntsEnabled
      • byteOrder

        java.nio.ByteOrder byteOrder
      • nativeOrder

        protected static final java.nio.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​(java.io.OutputStream outputStream)
        Creates a new Output for writing to an OutputStream. A buffer size of 4096 is used.
      • ByteBufferOutput

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

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

        public ByteBufferOutput​(java.nio.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 java.nio.ByteOrder order()
      • order

        public void order​(java.nio.ByteOrder byteOrder)
      • getOutputStream

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

        public void setOutputStream​(java.io.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​(java.nio.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​(java.nio.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 java.nio.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
      • flush

        public void flush()
                   throws KryoException
        Writes the buffered bytes to the underlying OutputStream, if any.
        Specified by:
        flush in interface java.io.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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close 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
      • 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
      • writeAscii

        public void writeAscii​(java.lang.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​(java.lang.CharSequence value,
                                      int charCount,
                                      int charIndex)
      • writeAscii_slow

        private void writeAscii_slow​(java.lang.String value,
                                     int charCount)
                              throws KryoException
        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
      • 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 -