Class ByteBufferOutput

  • All Implemented Interfaces:
    Pool.Poolable, java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    UnsafeByteBufferOutput

    public class ByteBufferOutput
    extends Output
    An Output that uses a ByteBuffer rather than a byte[].

    Note that the byte[] buffer is not used. Code taking an Output and expecting the byte[] to be used may not work correctly.

    Author:
    Roman Levenstein , Nathan Sweet
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferOutput()
      Creates an uninitialized Output, setBuffer(ByteBuffer) must be called before the Output is used.
      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)  
      ByteBufferOutput​(java.io.OutputStream outputStream, int bufferSize)  
      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 Deprecated Methods 
      Modifier and Type Method Description
      void close()
      Flushes any buffered bytes and closes the underlying OutputStream, if any.
      void flush()
      Flushes the buffered bytes.
      byte[] getBuffer()
      Deprecated. 
      java.nio.ByteBuffer getByteBuffer()
      Returns the buffer.
      java.io.OutputStream getOutputStream()  
      protected boolean require​(int required)
      Ensures the buffer is large enough to read the specified number of bytes.
      void reset()
      Sets the position and total to 0.
      void setBuffer​(byte[] buffer)
      Deprecated. 
      void setBuffer​(byte[] buffer, int maxBufferSize)
      Deprecated. 
      void setBuffer​(byte[] bytes, int offset, int count)
      Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
      void setBuffer​(java.nio.ByteBuffer buffer)
      Sets a new buffer to write to.
      void setBuffer​(java.nio.ByteBuffer buffer, int maxBufferSize)
      Sets a new buffer to write to.
      void setPosition​(int position)
      Sets the current position in the buffer.
      byte[] toBytes()
      Allocates and returns a new byte[] containing the bytes currently in the buffer between 0 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.
      void writeBoolean​(boolean value)
      Writes a 1 byte boolean.
      void writeBooleans​(boolean[] array, int offset, int count)
      Writes a boolean array in bulk.
      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[] array, int offset, int count)
      Writes a char array in bulk.
      void writeDouble​(double value)
      Writes an 8 byte double.
      void writeDoubles​(double[] array, int offset, int count)
      Writes a double array in bulk.
      void writeFloat​(float value)
      Writes a 4 byte float.
      void writeFloats​(float[] array, int offset, int count)
      Writes a float array in bulk.
      void writeInt​(int value)
      Writes a 4 byte int.
      void writeInts​(int[] array, int offset, int count)
      Writes an int array in bulk.
      void writeLong​(long value)
      Writes an 8 byte long.
      void writeLongs​(long[] array, int offset, int count)
      Writes a long array in bulk.
      void writeShort​(int value)
      Writes a 2 byte short.
      void writeShorts​(short[] array, int offset, int count)
      Writes a short array in bulk.
      void writeString​(java.lang.String value)
      Writes the length and string, or null.
      int writeVarInt​(int value, boolean optimizePositive)
      Writes a 1-5 byte int.
      int writeVarIntFlag​(boolean flag, int value, boolean optimizePositive)
      Writes a 1-5 byte int, encoding the boolean value with a bit flag.
      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 Detail

      • byteBuffer

        protected java.nio.ByteBuffer byteBuffer
    • Constructor Detail

      • ByteBufferOutput

        public ByteBufferOutput()
        Creates an uninitialized Output, setBuffer(ByteBuffer) must be called before the Output is used.
      • ByteBufferOutput

        public ByteBufferOutput​(int bufferSize)
        Creates a new Output for writing to a direct ByteBuffer.
        Parameters:
        bufferSize - The size of the buffer. An exception is thrown if more bytes than this are written and flush() does not empty the buffer.
      • 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 - If flush() does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
      • 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 - If flush() does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
      • ByteBufferOutput

        public ByteBufferOutput​(java.io.OutputStream outputStream)
        See Also:
        Output(OutputStream)
      • ByteBufferOutput

        public ByteBufferOutput​(java.io.OutputStream outputStream,
                                int bufferSize)
        See Also:
        Output(OutputStream, int)
    • Method Detail

      • getOutputStream

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

        public byte[] getBuffer()
        Deprecated.
        Throws UnsupportedOperationException because this output uses a ByteBuffer, not a byte[].
        Overrides:
        getBuffer in class Output
        See Also:
        getByteBuffer()
      • setBuffer

        public void setBuffer​(byte[] buffer)
        Deprecated.
        Throws UnsupportedOperationException because this output uses a ByteBuffer, not a byte[].
        Overrides:
        setBuffer in class Output
        See Also:
        getByteBuffer()
      • setBuffer

        public void setBuffer​(byte[] buffer,
                              int maxBufferSize)
        Deprecated.
        Throws UnsupportedOperationException because this output uses a ByteBuffer, not a byte[].
        Overrides:
        setBuffer in class Output
        maxBufferSize - If Output.flush() does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
        See Also:
        getByteBuffer()
      • setBuffer

        public void setBuffer​(byte[] bytes,
                              int offset,
                              int count)
        Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
        See Also:
        setBuffer(ByteBuffer)
      • setBuffer

        public void setBuffer​(java.nio.ByteBuffer buffer)
        Sets a new buffer to write to. The max size is the buffer's length.
        See Also:
        setBuffer(ByteBuffer, int)
      • setBuffer

        public void setBuffer​(java.nio.ByteBuffer buffer,
                              int maxBufferSize)
        Sets a new buffer to write to. The bytes are not copied, the old buffer is discarded and the new buffer used in its place. The position and capacity are set to match the specified buffer. The total is reset. The OutputStream is set to null.
        Parameters:
        maxBufferSize - If flush() does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
      • 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()
        Description copied from class: Output
        Allocates and returns a new byte[] containing the bytes currently in the buffer between 0 and Output.position().
        Overrides:
        toBytes in class Output
      • setPosition

        public void setPosition​(int position)
        Description copied from class: Output
        Sets the current position in the buffer.
        Overrides:
        setPosition in class Output
      • reset

        public void reset()
        Description copied from class: Output
        Sets the position and total to 0.
        Specified by:
        reset in interface Pool.Poolable
        Overrides:
        reset in class Output
      • require

        protected boolean require​(int required)
                           throws KryoException
        Description copied from class: Output
        Ensures the buffer is large enough to read the specified number of bytes.
        Overrides:
        require in class Output
        Returns:
        true if the buffer has been resized.
        Throws:
        KryoException
      • flush

        public void flush()
                   throws KryoException
        Description copied from class: Output
        Flushes the buffered bytes. The default implementation writes the buffered bytes to the OutputStream, if any, and sets the position to 0. Can be overridden to flush the bytes somewhere else.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class Output
        Throws:
        KryoException
      • close

        public void close()
                   throws KryoException
        Description copied from class: Output
        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)
                   throws KryoException
        Description copied from class: Output
        Writes the bytes. Note the number of bytes is not written.
        Overrides:
        write in class Output
        Throws:
        KryoException
      • write

        public void write​(byte[] bytes,
                          int offset,
                          int length)
                   throws KryoException
        Description copied from class: Output
        Writes the bytes. Note the number of bytes is not written.
        Overrides:
        write in class Output
        Throws:
        KryoException
      • writeBytes

        public void writeBytes​(byte[] bytes,
                               int offset,
                               int count)
                        throws KryoException
        Description copied from class: Output
        Writes the bytes. Note the number of bytes is not written.
        Overrides:
        writeBytes in class Output
        Throws:
        KryoException
      • writeVarInt

        public int writeVarInt​(int value,
                               boolean optimizePositive)
                        throws KryoException
        Description copied from class: Output
        Writes a 1-5 byte int.
        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).
        Returns:
        The number of bytes written.
        Throws:
        KryoException
        See Also:
        Output.varIntLength(int, boolean)
      • writeVarIntFlag

        public int writeVarIntFlag​(boolean flag,
                                   int value,
                                   boolean optimizePositive)
                            throws KryoException
        Description copied from class: Output
        Writes a 1-5 byte int, encoding the boolean value with a bit flag.
        Overrides:
        writeVarIntFlag in class Output
        optimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).
        Returns:
        The number of bytes written.
        Throws:
        KryoException
      • writeVarLong

        public int writeVarLong​(long value,
                                boolean optimizePositive)
                         throws KryoException
        Description copied from class: Output
        Writes a 1-9 byte long.
        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).
        Returns:
        The number of bytes written.
        Throws:
        KryoException
        See Also:
        Output.varLongLength(long, boolean)
      • writeAscii

        public void writeAscii​(java.lang.String value)
                        throws KryoException
        Description copied from class: Output
        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 Output.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
      • writeInts

        public void writeInts​(int[] array,
                              int offset,
                              int count)
                       throws KryoException
        Description copied from class: Output
        Writes an int array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeInts in class Output
        Throws:
        KryoException
      • writeLongs

        public void writeLongs​(long[] array,
                               int offset,
                               int count)
                        throws KryoException
        Description copied from class: Output
        Writes a long array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeLongs in class Output
        Throws:
        KryoException
      • writeFloats

        public void writeFloats​(float[] array,
                                int offset,
                                int count)
                         throws KryoException
        Description copied from class: Output
        Writes a float array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeFloats in class Output
        Throws:
        KryoException
      • writeDoubles

        public void writeDoubles​(double[] array,
                                 int offset,
                                 int count)
                          throws KryoException
        Description copied from class: Output
        Writes a double array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeDoubles in class Output
        Throws:
        KryoException
      • writeShorts

        public void writeShorts​(short[] array,
                                int offset,
                                int count)
                         throws KryoException
        Description copied from class: Output
        Writes a short array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeShorts in class Output
        Throws:
        KryoException
      • writeChars

        public void writeChars​(char[] array,
                               int offset,
                               int count)
                        throws KryoException
        Description copied from class: Output
        Writes a char array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeChars in class Output
        Throws:
        KryoException
      • writeBooleans

        public void writeBooleans​(boolean[] array,
                                  int offset,
                                  int count)
                           throws KryoException
        Description copied from class: Output
        Writes a boolean array in bulk. This may be more efficient than writing them individually.
        Overrides:
        writeBooleans in class Output
        Throws:
        KryoException