Class ByteBufferInput

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ByteBufferInput
    extends Input
    An InputStream that reads data from a byte array and optionally fills the byte array from another InputStream as needed. Utility methods are provided for efficiently reading primitive types and strings.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferInput()
      Creates an uninitialized Input.
      ByteBufferInput​(byte[] buffer)  
      ByteBufferInput​(int bufferSize)
      Creates a new Input for reading from a byte array.
      ByteBufferInput​(java.io.InputStream inputStream)
      Creates a new Input for reading from an InputStream with a buffer size of 4096.
      ByteBufferInput​(java.io.InputStream inputStream, int bufferSize)
      Creates a new Input for reading from an InputStream.
      ByteBufferInput​(java.nio.ByteBuffer buffer)
      Creates a new Input for reading from a ByteBuffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canReadInt()
      Returns true if enough bytes are available to read an int with readInt(boolean).
      boolean canReadLong()
      Returns true if enough bytes are available to read a long with readLong(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.
      java.nio.ByteBuffer getByteBuffer()  
      java.io.InputStream getInputStream()  
      boolean getVarIntsEnabled()
      Return current setting for variable length encoding of integers
      private boolean isNativeOrder()  
      private int optional​(int optional)  
      java.nio.ByteOrder order()  
      void order​(java.nio.ByteOrder byteOrder)  
      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.
      private java.lang.String readAscii()  
      private java.lang.String readAscii_slow()  
      boolean readBoolean()
      Reads a 1 byte boolean.
      byte readByte()
      Reads a single byte.
      void readBytes​(byte[] bytes)
      Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.
      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)
      Bulk input of a char array.
      double readDouble()
      Reads an 8 bytes double.
      double readDouble​(double precision, boolean optimizePositive)
      Reads a 1-9 byte double with reduced precision.
      double[] readDoubles​(int length)
      Bulk input of a double array.
      float readFloat()
      Reads a 4 byte float.
      float readFloat​(float precision, boolean optimizePositive)
      Reads a 1-5 byte float with reduced precision.
      float[] readFloats​(int length)
      Bulk input of a float array.
      int readInt()
      Reads a 4 byte int.
      int readInt​(boolean optimizePositive)
      Reads a 1-5 byte int.
      private int readInt_slow​(boolean optimizePositive)  
      int[] readInts​(int length)
      Bulk input of an int array.
      long readLong()
      Reads an 8 byte long.
      long readLong​(boolean optimizePositive)
      Reads a 1-9 byte long.
      private long readLong_slow​(boolean optimizePositive)  
      long[] readLongs​(int length)
      Bulk input of a long array.
      short readShort()
      Reads a 2 byte short.
      short[] readShorts​(int length)
      Bulk input of a short array.
      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.
      private void readUtf8​(int charCount)  
      private void readUtf8_slow​(int charCount, int charIndex)  
      private int readUtf8Length​(int b)  
      private int readUtf8Length_slow​(int b)  
      int readVarInt​(boolean optimizePositive)
      Reads a 1-5 byte int.
      long readVarLong​(boolean optimizePositive)
      Reads a 1-9 byte long.
      protected int require​(int required)  
      void rewind()
      Sets the position and total to zero.
      void setBuffer​(byte[] bytes)
      Sets a new buffer, discarding any previous buffer.
      void setBuffer​(java.nio.ByteBuffer buffer)
      Sets a new buffer, discarding any previous buffer.
      void setInputStream​(java.io.InputStream inputStream)
      Sets a new InputStream.
      void setLimit​(int limit)
      Sets the limit in the buffer.
      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.
      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, reset
      • 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

      • ByteBufferInput

        public ByteBufferInput()
        Creates an uninitialized Input. A buffer must be set before the Input is used.
        See Also:
        setBuffer(ByteBuffer)
      • ByteBufferInput

        public ByteBufferInput​(int bufferSize)
        Creates a new Input for reading from a byte array.
        Parameters:
        bufferSize - The size of the buffer. An exception is thrown if more bytes than this are read.
      • ByteBufferInput

        public ByteBufferInput​(byte[] buffer)
      • ByteBufferInput

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

        public ByteBufferInput​(java.io.InputStream inputStream)
        Creates a new Input for reading from an InputStream with a buffer size of 4096.
      • ByteBufferInput

        public ByteBufferInput​(java.io.InputStream inputStream,
                               int bufferSize)
        Creates a new Input for reading from an InputStream.
    • Method Detail

      • order

        public java.nio.ByteOrder order()
      • order

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

        public void setBuffer​(byte[] bytes)
        Sets a new buffer, discarding any previous buffer. The position and total are reset.
        Overrides:
        setBuffer in class Input
      • setBuffer

        public void setBuffer​(java.nio.ByteBuffer buffer)
        Sets a new buffer, discarding any previous buffer. The byte order, 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()
      • getInputStream

        public java.io.InputStream getInputStream()
        Overrides:
        getInputStream in class Input
      • setInputStream

        public void setInputStream​(java.io.InputStream inputStream)
        Sets a new InputStream. The position and total are reset, discarding any buffered bytes.
        Overrides:
        setInputStream in class Input
        Parameters:
        inputStream - May be null.
      • rewind

        public void rewind()
        Description copied from class: Input
        Sets the position and total to zero.
        Overrides:
        rewind in class Input
      • fill

        protected int fill​(java.nio.ByteBuffer buffer,
                           int offset,
                           int count)
                    throws KryoException
        Fills the buffer with more bytes. Can be overridden to fill the bytes from a source other than the InputStream.
        Throws:
        KryoException
      • require

        protected final int require​(int required)
                             throws KryoException
        Overrides:
        require in class Input
        Parameters:
        required - Must be > 0. The buffer is filled until it has at least this many bytes.
        Returns:
        the number of bytes remaining.
        Throws:
        KryoException - if EOS is reached before required bytes are read (buffer underflow).
      • optional

        private int optional​(int optional)
                      throws KryoException
        Parameters:
        optional - Try to fill the buffer with this many bytes.
        Returns:
        the number of bytes remaining, but not more than optional, or -1 if the EOS was reached and the buffer is empty.
        Throws:
        KryoException
      • read

        public int read()
                 throws KryoException
        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
        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
        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.
        Overrides:
        setPosition in class Input
      • setLimit

        public void setLimit​(int limit)
        Sets the limit in the buffer.
        Overrides:
        setLimit in class Input
      • close

        public void close()
                   throws KryoException
        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 void readBytes​(byte[] bytes)
                       throws KryoException
        Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.
        Overrides:
        readBytes in class Input
        Throws:
        KryoException
      • readBytes

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

        public int readInt​(boolean optimizePositive)
                    throws KryoException
        Description copied from class: Input
        Reads 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:
        readInt in class Input
        Throws:
        KryoException
      • readVarInt

        public int readVarInt​(boolean optimizePositive)
                       throws KryoException
        Description copied from class: Input
        Reads a 1-5 byte int. It is guaranteed that a varible length encoding will be used.
        Overrides:
        readVarInt in class Input
        Throws:
        KryoException
      • readInt_slow

        private int readInt_slow​(boolean optimizePositive)
      • readUtf8Length

        private int readUtf8Length​(int b)
      • readUtf8Length_slow

        private int readUtf8Length_slow​(int b)
      • readUtf8

        private void readUtf8​(int charCount)
      • readUtf8_slow

        private void readUtf8_slow​(int charCount,
                                   int charIndex)
      • readAscii

        private java.lang.String readAscii()
      • readAscii_slow

        private java.lang.String readAscii_slow()
      • readFloat

        public float readFloat​(float precision,
                               boolean optimizePositive)
                        throws KryoException
        Reads a 1-5 byte float with reduced precision.
        Overrides:
        readFloat in class Input
        Throws:
        KryoException
      • readLong

        public long readLong​(boolean optimizePositive)
                      throws KryoException
        Reads 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:
        readLong in class Input
        Throws:
        KryoException
      • readVarLong

        public long readVarLong​(boolean optimizePositive)
                         throws KryoException
        Reads a 1-9 byte long. It is guaranteed that a varible length encoding will be used.
        Overrides:
        readVarLong in class Input
        Throws:
        KryoException
      • readLong_slow

        private long readLong_slow​(boolean optimizePositive)
      • readDouble

        public double readDouble​(double precision,
                                 boolean optimizePositive)
                          throws KryoException
        Reads a 1-9 byte double with reduced precision.
        Overrides:
        readDouble in class Input
        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 -