Class Input

    • Constructor Summary

      Constructors 
      Constructor Description
      Input()
      Creates an uninitialized Input, setBuffer(byte[]) must be called before the Input is used.
      Input​(byte[] buffer)
      Creates a new Input for reading from a byte[] buffer.
      Input​(byte[] buffer, int offset, int count)
      Creates a new Input for reading from a byte[] buffer.
      Input​(int bufferSize)
      Creates a new Input for reading from a byte[] buffer.
      Input​(InputStream inputStream)
      Creates a new Input for reading from an InputStream with a buffer size of 4096.
      Input​(InputStream inputStream, int bufferSize)
      Creates a new Input for reading from an InputStream with the specified buffer size.
    • 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).
      boolean canReadVarInt()
      Returns true if enough bytes are available to read an int with readVarInt(boolean).
      boolean canReadVarLong()
      Returns true if enough bytes are available to read a long with readVarLong(boolean).
      void close()
      Closes the underlying InputStream, if any.
      boolean end()
      Returns true if the limit() has been reached and fill(byte[], int, int) is unable to provide more bytes.
      protected int fill​(byte[] buffer, int offset, int count)
      Fills the buffer with more bytes.
      byte[] getBuffer()
      Returns the buffer.
      InputStream getInputStream()  
      boolean getVariableLengthEncoding()  
      int limit()
      Returns the limit for the buffer.
      protected int optional​(int optional)
      Fills the buffer with at least the number of bytes specified, if possible.
      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 or -1 if no more bytes are available.
      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)
      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)
      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 readInt​(boolean optimizePositive)
      Reads an int using fixed or variable length encoding, depending on setVariableLengthEncoding(boolean).
      int readInt​(int count)
      Reads count bytes and returns them as int, the last byte read will be the lowest byte in the int.
      int[] readInts​(int length)
      Reads an int array in bulk.
      int[] readInts​(int length, boolean optimizePositive)
      Reads an int array in bulk using fixed or variable length encoding, depending on setVariableLengthEncoding(boolean).
      long readLong()
      Reads an 8 byte long.
      long readLong​(boolean optimizePositive)
      Reads a long using fixed or variable length encoding, depending on setVariableLengthEncoding(boolean).
      long readLong​(int count)
      Reads count bytes and returns them as long, the last byte read will be the lowest byte in the long.
      long[] readLongs​(int length)
      Reads a long array in bulk.
      long[] readLongs​(int length, boolean optimizePositive)
      Reads an int array in bulk using fixed or variable length encoding, depending on setVariableLengthEncoding(boolean).
      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.
      String readString()
      Reads the length and string of UTF8 characters, or null.
      StringBuilder readStringBuilder()
      Reads the length and string of UTF8 characters, or null.
      double readVarDouble​(double precision, boolean optimizePositive)
      Reads a 1-9 byte double with reduced precision.
      float readVarFloat​(float precision, boolean optimizePositive)
      Reads a 1-5 byte float with reduced precision.
      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)
      Sets a new buffer.
      void setBuffer​(byte[] bytes, int offset, int count)
      Sets a new buffer to read from.
      void setInputStream​(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 setTotal​(long total)
      Sets the total number of bytes read.
      void setVariableLengthEncoding​(boolean varEncoding)
      If false, readInt(boolean), readLong(boolean), readInts(int, boolean), and readLongs(int, boolean) will use fixed length encoding, which may be faster for some data.
      void skip​(int count)
      Discards the specified number of bytes.
      long skip​(long count)
      Discards the specified number of bytes.
      long total()
      Returns the total number of bytes read.
    • 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
      • varEncoding

        protected boolean varEncoding
    • 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[] buffer.
        Parameters:
        bufferSize - The size of the buffer. An exception is thrown if more bytes than this are read and fill(byte[], int, int) does not supply more bytes.
      • Input

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

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

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

        public Input​(InputStream inputStream,
                     int bufferSize)
        Creates a new Input for reading from an InputStream with the specified buffer size.
    • Method Detail

      • setBuffer

        public void setBuffer​(byte[] bytes)
        Sets a new buffer. The offset is 0 and the count is the buffer's length.
        See Also:
        setBuffer(byte[], int, int)
      • setBuffer

        public void setBuffer​(byte[] bytes,
                              int offset,
                              int count)
        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 and total are reset. The InputStream is set to null.
      • getBuffer

        public byte[] getBuffer()
        Returns the buffer. The bytes between 0 and position() are the data that can be read.
      • setInputStream

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

        public boolean getVariableLengthEncoding()
      • total

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

        public void setTotal​(long total)
        Sets the total 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 where the next byte will be read.
      • limit

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

        public void setLimit​(int limit)
        Sets the limit in the buffer which marks the end of the data that can be read.
      • fill

        protected int fill​(byte[] buffer,
                           int offset,
                           int count)
                    throws KryoException
        Fills the buffer with more bytes. The default implementation reads from the InputStream, if set. Can be overridden to fill the bytes from another source.
        Returns:
        -1 if there are no more bytes.
        Throws:
        KryoException
      • require

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

        protected int optional​(int optional)
                        throws KryoException
        Fills the buffer with at least the number of bytes specified, if possible.
        Parameters:
        optional - Must be > 0.
        Returns:
        the number of bytes remaining, but not more than optional, or -1 if fill(byte[], int, int) is unable to provide more bytes.
        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 or -1 if no more bytes are available.
        Overrides:
        read in class 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 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​(int count)
        Reads count bytes and returns them as int, the last byte read will be the lowest byte in the int.
      • readLong

        public long readLong​(int count)
        Reads count bytes and returns them as long, the last byte read will be the lowest byte in the long.
      • 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.
      • 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().
      • readVarFloat

        public float readVarFloat​(float precision,
                                  boolean optimizePositive)
                           throws KryoException
        Reads a 1-5 byte float with reduced precision.
        Throws:
        KryoException
      • readVarDouble

        public double readVarDouble​(double precision,
                                    boolean optimizePositive)
                             throws KryoException
        Reads a 1-9 byte double 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
      • readStringBuilder

        public StringBuilder readStringBuilder()
        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).
        Returns:
        May be null.
      • readInts

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

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

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

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

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

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

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