Class Input

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    ByteBufferInput, InputChunked

    public class Input
    extends java.io.InputStream
    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.

    The byte[] buffer may be modified and then returned to its original state during some read operations, so the same byte[] should not be used concurrently in separate threads.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] buffer  
      protected int capacity  
      protected char[] chars  
      protected java.io.InputStream inputStream  
      protected int limit  
      protected int position  
      protected long total  
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      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.
      boolean eof()  
      protected int fill​(byte[] buffer, int offset, int count)
      Fills the buffer with more bytes.
      byte[] getBuffer()  
      java.io.InputStream getInputStream()  
      int limit()
      Returns the limit for the buffer.
      private int optional​(int optional)  
      int position()
      Returns the current position in the buffer.
      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.
      int[] readInts​(int length, boolean optimizePositive)
      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.
      long[] readLongs​(int length, boolean optimizePositive)
      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.
      void setBuffer​(byte[] bytes, int offset, int count)
      Sets a new 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 setTotal​(long total)
      Sets the number of bytes read.
      void skip​(int count)
      Discards the specified number of bytes.
      long skip​(long count)
      Discards the specified number of bytes.
      long total()
      Returns the number of bytes read.
      • 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

      • buffer

        protected byte[] buffer
      • position

        protected int position
      • capacity

        protected int capacity
      • limit

        protected int limit
      • total

        protected long total
      • chars

        protected char[] chars
      • inputStream

        protected java.io.InputStream inputStream
    • Constructor Detail

      • Input

        public Input()
        Creates an uninitialized Input. setBuffer(byte[]) must be called before the Input is used.
      • Input

        public Input​(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.
      • Input

        public Input​(byte[] buffer)
        Creates a new Input for reading from a byte array.
        Parameters:
        buffer - An exception is thrown if more bytes than this are read.
      • Input

        public Input​(byte[] buffer,
                     int offset,
                     int count)
        Creates a new Input for reading from a byte array.
        Parameters:
        buffer - An exception is thrown if more bytes than this are read.
      • Input

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

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

      • setBuffer

        public void setBuffer​(byte[] bytes)
        Sets a new buffer. The position and total are reset, discarding any buffered bytes.
      • setBuffer

        public void setBuffer​(byte[] bytes,
                              int offset,
                              int count)
        Sets a new buffer. The position and total are reset, discarding any buffered bytes.
      • getBuffer

        public byte[] getBuffer()
      • getInputStream

        public java.io.InputStream getInputStream()
      • setInputStream

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

        public long total()
        Returns the number of bytes read.
      • setTotal

        public void setTotal​(long total)
        Sets the number of bytes read.
      • position

        public int position()
        Returns the current position in the buffer.
      • setPosition

        public void setPosition​(int position)
        Sets the current position in the buffer.
      • limit

        public int limit()
        Returns the limit for the buffer.
      • setLimit

        public void setLimit​(int limit)
        Sets the limit in the buffer.
      • rewind

        public void rewind()
        Sets the position and total to zero.
      • fill

        protected int fill​(byte[] 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.
        Returns:
        -1 if there are no more bytes.
        Throws:
        KryoException
      • require

        protected int require​(int required)
                       throws KryoException
        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
      • eof

        public boolean eof()
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • 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.
        Specified by:
        read in class java.io.InputStream
        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 java.io.InputStream
        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 java.io.InputStream
        Throws:
        KryoException
      • skip

        public long skip​(long count)
                  throws KryoException
        Discards the specified number of bytes.
        Overrides:
        skip in class java.io.InputStream
        Throws:
        KryoException
      • 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 java.io.InputStream
        Throws:
        KryoException
      • readByteUnsigned

        public int readByteUnsigned()
                             throws KryoException
        Reads a byte as an int from 0 to 255.
        Throws:
        KryoException
      • readBytes

        public byte[] readBytes​(int length)
                         throws KryoException
        Reads the specified number of bytes into a new byte[].
        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.
        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.
        Throws:
        KryoException
      • readInt

        public int readInt​(boolean optimizePositive)
                    throws KryoException
        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.
        Throws:
        KryoException
      • readVarInt

        public int readVarInt​(boolean optimizePositive)
                       throws KryoException
        Reads a 1-5 byte int. It is guaranteed that a varible length encoding will be used.
        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.
        Throws:
        KryoException
      • readShortUnsigned

        public int readShortUnsigned()
                              throws KryoException
        Reads a 2 byte short as an int from 0 to 65535.
        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.
        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.
        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.
        Throws:
        KryoException
      • readInts

        public int[] readInts​(int length,
                              boolean optimizePositive)
                       throws KryoException
        Bulk input of an int array.
        Throws:
        KryoException
      • readLongs

        public long[] readLongs​(int length,
                                boolean optimizePositive)
                         throws KryoException
        Bulk input of a long array.
        Throws:
        KryoException
      • readDoubles

        public double[] readDoubles​(int length)
                             throws KryoException
        Bulk input of a double array.
        Throws:
        KryoException