Class ByteBufferInput

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

    public class ByteBufferInput
    extends Input
    An Input that uses a ByteBuffer rather than a byte[].

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

    Author:
    Roman Levenstein , Nathan Sweet
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferInput()
      Creates an uninitialized Input, setBuffer(ByteBuffer) must be called before the Input is used.
      ByteBufferInput​(byte[] bytes)
      Creates a new Input for reading from a ByteBuffer which is filled with the specified bytes.
      ByteBufferInput​(byte[] bytes, int offset, int count)
      Creates a new Input for reading from a ByteBuffer which is filled with the specified bytes.
      ByteBufferInput​(int bufferSize)
      Creates a new Input for reading from a direct ByteBuffer.
      ByteBufferInput​(java.io.InputStream inputStream)  
      ByteBufferInput​(java.io.InputStream inputStream, int bufferSize)  
      ByteBufferInput​(java.nio.ByteBuffer buffer)
      Creates a new Input for reading from a ByteBuffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean canReadVarInt()
      Returns true if enough bytes are available to read an int with Input.readVarInt(boolean).
      boolean canReadVarLong()
      Returns true if enough bytes are available to read a long with Input.readVarLong(boolean).
      void close()
      Closes the underlying InputStream, if any.
      protected int fill​(java.nio.ByteBuffer buffer, int offset, int count)
      Fills the buffer with more bytes.
      byte[] getBuffer()
      Deprecated. 
      java.nio.ByteBuffer getByteBuffer()  
      protected int optional​(int optional)
      Fills the buffer with at least the number of bytes specified, if possible.
      int read()
      Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.
      int read​(byte[] bytes)
      Reads bytes.length bytes or less and writes them to the specified byte[], starting at 0, and returns the number of bytes read.
      int read​(byte[] bytes, int offset, int count)
      Reads count bytes or less and writes them to the specified byte[], starting at offset, and returns the number of bytes read or -1 if no more bytes are available.
      boolean readBoolean()
      Reads a 1 byte boolean.
      boolean[] readBooleans​(int length)
      Reads a boolean array in bulk.
      byte readByte()
      Reads a single byte.
      void readBytes​(byte[] bytes, int offset, int count)
      Reads count bytes and writes them to the specified byte[], starting at offset.
      byte[] readBytes​(int length)
      Reads the specified number of bytes into a new byte[].
      int readByteUnsigned()
      Reads a byte as an int from 0 to 255.
      char readChar()
      Reads a 2 byte char.
      char[] readChars​(int length)
      Reads a char array in bulk.
      double readDouble()
      Reads an 8 byte double.
      double[] readDoubles​(int length)
      Reads a double array in bulk.
      float readFloat()
      Reads a 4 byte float.
      float[] readFloats​(int length)
      Reads a float array in bulk.
      int readInt()
      Reads a 4 byte int.
      int[] readInts​(int length)
      Reads an int array in bulk.
      long readLong()
      Reads an 8 byte long.
      long[] readLongs​(int length)
      Reads a long array in bulk.
      short readShort()
      Reads a 2 byte short.
      short[] readShorts​(int length)
      Reads a short array in bulk.
      int readShortUnsigned()
      Reads a 2 byte short as an int from 0 to 65535.
      java.lang.String readString()
      Reads the length and string of UTF8 characters, or null.
      java.lang.StringBuilder readStringBuilder()
      Reads the length and string of UTF8 characters, or null.
      int readVarInt​(boolean optimizePositive)
      Reads a 1-5 byte int.
      boolean readVarIntFlag()
      Reads the boolean part of a varint flag.
      int readVarIntFlag​(boolean optimizePositive)
      Reads the 1-5 byte int part of a varint flag.
      long readVarLong​(boolean optimizePositive)
      Reads a 1-9 byte long.
      protected int require​(int required)
      Fills the buffer with at least the number of bytes specified.
      void reset()
      Sets the position and total to zero.
      void setBuffer​(byte[] bytes)
      Deprecated. 
      void setBuffer​(byte[] bytes, int offset, int count)
      Deprecated. 
      void setBuffer​(java.nio.ByteBuffer buffer)
      Sets a new buffer to read from.
      void setInputStream​(java.io.InputStream inputStream)
      Sets an InputStream to read from when data in the buffer is exhausted.
      void setLimit​(int limit)
      Sets the limit in the buffer which marks the end of the data that can be read.
      void setPosition​(int position)
      Sets the current position in the buffer where the next byte will be read.
      void skip​(int count)
      Discards the specified number of bytes.
      long skip​(long count)
      Discards the specified number of bytes.
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • 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

      • ByteBufferInput

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

        public ByteBufferInput​(int bufferSize)
        Creates a new Input for reading from a direct ByteBuffer.
        Parameters:
        bufferSize - The size of the buffer. An exception is thrown if more bytes than this are read and fill(ByteBuffer, int, int) does not supply more bytes.
      • ByteBufferInput

        public ByteBufferInput​(byte[] bytes)
        Creates a new Input for reading from a ByteBuffer which is filled with the specified bytes.
      • ByteBufferInput

        public ByteBufferInput​(byte[] bytes,
                               int offset,
                               int count)
        Creates a new Input for reading from a ByteBuffer which is filled with the specified bytes.
        See Also:
        setBuffer(byte[], int, int)
      • ByteBufferInput

        public ByteBufferInput​(java.nio.ByteBuffer buffer)
        Creates a new Input for reading from a ByteBuffer.
      • ByteBufferInput

        public ByteBufferInput​(java.io.InputStream inputStream)
        See Also:
        Input(InputStream)
      • ByteBufferInput

        public ByteBufferInput​(java.io.InputStream inputStream,
                               int bufferSize)
        See Also:
        Input(InputStream, int)
    • Method Detail

      • getBuffer

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

        public void setBuffer​(byte[] bytes)
        Deprecated.
        Throws UnsupportedOperationException because this input uses a ByteBuffer, not a byte[].
        Overrides:
        setBuffer in class Input
        See Also:
        setBuffer(ByteBuffer)
      • setBuffer

        public void setBuffer​(byte[] bytes,
                              int offset,
                              int count)
        Deprecated.
        Throws UnsupportedOperationException because this input uses a ByteBuffer, not a byte[].
        Overrides:
        setBuffer in class Input
        See Also:
        setBuffer(ByteBuffer)
      • setBuffer

        public void setBuffer​(java.nio.ByteBuffer buffer)
        Sets a new buffer to read from. The bytes are not copied, the old buffer is discarded and the new buffer used in its place. The position, limit, and capacity are set to match the specified buffer. The total is reset. The InputStream is set to null.
      • getByteBuffer

        public java.nio.ByteBuffer getByteBuffer()
      • setInputStream

        public void setInputStream​(java.io.InputStream inputStream)
        Description copied from class: Input
        Sets an InputStream to read from when data in the buffer is exhausted. The position, limit, and total are reset, discarding any buffered bytes.
        Overrides:
        setInputStream in class Input
        Parameters:
        inputStream - May be null.
      • reset

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

        protected int fill​(java.nio.ByteBuffer buffer,
                           int offset,
                           int count)
                    throws KryoException
        Fills the buffer with more bytes. May leave the buffer position changed. The default implementation reads from the InputStream, if set. Can be overridden to fill the bytes from another source.
        Throws:
        KryoException
      • require

        protected int require​(int required)
                       throws KryoException
        Description copied from class: Input
        Fills the buffer with at least the number of bytes specified.
        Overrides:
        require in class Input
        Parameters:
        required - Must be > 0.
        Returns:
        The number of bytes remaining in the buffer, which will be at least required bytes.
        Throws:
        KryoException - if Input.fill(byte[], int, int) is unable to provide more bytes (buffer underflow).
      • optional

        protected int optional​(int optional)
                        throws KryoException
        Fills the buffer with at least the number of bytes specified, if possible.
        Overrides:
        optional in class Input
        Parameters:
        optional - Must be > 0.
        Returns:
        the number of bytes remaining, but not more than optional, or -1 if fill(ByteBuffer, int, int) is unable to provide more bytes.
        Throws:
        KryoException
      • read

        public int read()
                 throws KryoException
        Description copied from class: Input
        Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.
        Overrides:
        read in class Input
        Throws:
        KryoException
      • read

        public int read​(byte[] bytes)
                 throws KryoException
        Description copied from class: Input
        Reads bytes.length bytes or less and writes them to the specified byte[], starting at 0, and returns the number of bytes read.
        Overrides:
        read in class Input
        Throws:
        KryoException
      • read

        public int read​(byte[] bytes,
                        int offset,
                        int count)
                 throws KryoException
        Description copied from class: Input
        Reads count bytes or less and writes them to the specified byte[], starting at offset, and returns the number of bytes read or -1 if no more bytes are available.
        Overrides:
        read in class Input
        Throws:
        KryoException
      • setPosition

        public void setPosition​(int position)
        Description copied from class: Input
        Sets the current position in the buffer where the next byte will be read.
        Overrides:
        setPosition in class Input
      • setLimit

        public void setLimit​(int limit)
        Description copied from class: Input
        Sets the limit in the buffer which marks the end of the data that can be read.
        Overrides:
        setLimit in class Input
      • close

        public void close()
                   throws KryoException
        Description copied from class: Input
        Closes the underlying InputStream, if any.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class Input
        Throws:
        KryoException
      • readBytes

        public byte[] readBytes​(int length)
                         throws KryoException
        Description copied from class: Input
        Reads the specified number of bytes into a new byte[].
        Overrides:
        readBytes in class Input
        Throws:
        KryoException
      • readBytes

        public void readBytes​(byte[] bytes,
                              int offset,
                              int count)
                       throws KryoException
        Description copied from class: Input
        Reads count bytes and writes them to the specified byte[], starting at offset.
        Overrides:
        readBytes in class Input
        Throws:
        KryoException
      • readVarIntFlag

        public boolean readVarIntFlag()
        Reads the boolean part of a varint flag. The position is not advanced, readVarIntFlag(boolean) should be used to advance the position.
        Overrides:
        readVarIntFlag in class Input
      • readVarIntFlag

        public int readVarIntFlag​(boolean optimizePositive)
        Reads the 1-5 byte int part of a varint flag. The position is advanced so if the boolean part is needed it should be read first with readVarIntFlag().
        Overrides:
        readVarIntFlag in class Input
      • readStringBuilder

        public java.lang.StringBuilder readStringBuilder()
        Description copied from class: Input
        Reads the length and string of UTF8 characters, or null. For non-ASCII strings, this method avoids allocating a string by reading directly to the StringBuilder. This can read strings written by Output.writeString(String) and Output.writeAscii(String).
        Overrides:
        readStringBuilder in class Input
        Returns:
        May be null.
      • readInts

        public int[] readInts​(int length)
                       throws KryoException
        Description copied from class: Input
        Reads an int array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readInts in class Input
        Throws:
        KryoException
      • readLongs

        public long[] readLongs​(int length)
                         throws KryoException
        Description copied from class: Input
        Reads a long array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readLongs in class Input
        Throws:
        KryoException
      • readFloats

        public float[] readFloats​(int length)
                           throws KryoException
        Description copied from class: Input
        Reads a float array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readFloats in class Input
        Throws:
        KryoException
      • readDoubles

        public double[] readDoubles​(int length)
                             throws KryoException
        Description copied from class: Input
        Reads a double array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readDoubles in class Input
        Throws:
        KryoException
      • readShorts

        public short[] readShorts​(int length)
                           throws KryoException
        Description copied from class: Input
        Reads a short array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readShorts in class Input
        Throws:
        KryoException
      • readChars

        public char[] readChars​(int length)
                         throws KryoException
        Description copied from class: Input
        Reads a char array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readChars in class Input
        Throws:
        KryoException
      • readBooleans

        public boolean[] readBooleans​(int length)
                               throws KryoException
        Description copied from class: Input
        Reads a boolean array in bulk. This may be more efficient than reading them individually.
        Overrides:
        readBooleans in class Input
        Throws:
        KryoException