Class FixedInput

java.lang.Object
java.io.InputStream
com.prineside.kryo.FixedInput
All Implemented Interfaces:
Closeable, AutoCloseable

public class FixedInput extends 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[]
     
    protected int
     
    protected char[]
     
    protected InputStream
     
    protected int
     
    protected int
     
    protected long
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    int
     
    boolean
    Returns true if enough bytes are available to read an int with readInt(boolean).
    boolean
    Returns true if enough bytes are available to read a long with readLong(boolean).
    void
    Closes the underlying InputStream, if any.
    boolean
    eof()
     
    protected int
    fill(byte[] buffer, int offset, int count)
    Fills the buffer with more bytes.
    byte[]
     
     
    final int
    Returns the limit for the buffer.
    final int
    Returns the current position in the buffer.
    int
    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
    Reads a 1 byte boolean.
    byte
    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
    Reads a byte as an int from 0 to 255.
    char
    Reads a 2 byte char.
    char[]
    readChars(int length)
    Bulk input of a char array.
    double
    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
    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
    Reads a 4 byte int.
    int
    readInt(boolean optimizePositive)
    Reads a 1-5 byte int.
    int[]
    readInts(int length)
    Bulk input of an int array.
    int[]
    readInts(int length, boolean optimizePositive)
    Bulk input of an int array.
    long
    Reads an 8 byte long.
    long
    readLong(boolean optimizePositive)
    Reads a 1-9 byte long.
    long[]
    readLongs(int length)
    Bulk input of a long array.
    long[]
    readLongs(int length, boolean optimizePositive)
    Bulk input of a long array.
    short
    Reads a 2 byte short.
    short[]
    readShorts(int length)
    Bulk input of a short array.
    int
    Reads a 2 byte short as an int from 0 to 65535.
    Reads the length and string of UTF8 characters, or null.
    Reads the length and string of UTF8 characters, or null.
    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
    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
    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(int 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
    Returns the number of bytes read.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 InputStream inputStream
  • Constructor Details

    • FixedInput

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

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

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

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

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

      public FixedInput(InputStream inputStream, int bufferSize)
      Creates a new Input for reading from an InputStream.
  • Method Details

    • 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 InputStream getInputStream()
    • setInputStream

      public void setInputStream(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(int total)
      Sets the number of bytes read.
    • position

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

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

      public final 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.
    • skip

      public void skip(int count) throws com.esotericsoftware.kryo.KryoException
      Discards the specified number of bytes.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • fill

      protected int fill(byte[] buffer, int offset, int count) throws com.esotericsoftware.kryo.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:
      com.esotericsoftware.kryo.KryoException
    • require

      protected int require(int required) throws com.esotericsoftware.kryo.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:
      com.esotericsoftware.kryo.KryoException - if EOS is reached before required bytes are read (buffer underflow).
    • eof

      public boolean eof()
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • read

      public int read() throws com.esotericsoftware.kryo.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 InputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • read

      public int read(byte[] bytes) throws com.esotericsoftware.kryo.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 InputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • read

      public int read(byte[] bytes, int offset, int count) throws com.esotericsoftware.kryo.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:
      com.esotericsoftware.kryo.KryoException
    • skip

      public long skip(long count) throws com.esotericsoftware.kryo.KryoException
      Discards the specified number of bytes.
      Overrides:
      skip in class InputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • close

      public void close() throws com.esotericsoftware.kryo.KryoException
      Closes the underlying InputStream, if any.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readByte

      public byte readByte() throws com.esotericsoftware.kryo.KryoException
      Reads a single byte.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readByteUnsigned

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

      public byte[] readBytes(int length) throws com.esotericsoftware.kryo.KryoException
      Reads the specified number of bytes into a new byte[].
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readBytes

      public void readBytes(byte[] bytes) throws com.esotericsoftware.kryo.KryoException
      Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readBytes

      public void readBytes(byte[] bytes, int offset, int count) throws com.esotericsoftware.kryo.KryoException
      Reads count bytes and writes them to the specified byte[], starting at offset.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readInt

      public int readInt() throws com.esotericsoftware.kryo.KryoException
      Reads a 4 byte int.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readInt

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

      public int readVarInt(boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Reads a 1-5 byte int. It is guaranteed that a varible length encoding will be used.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • canReadInt

      public boolean canReadInt() throws com.esotericsoftware.kryo.KryoException
      Returns true if enough bytes are available to read an int with readInt(boolean).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • canReadLong

      public boolean canReadLong() throws com.esotericsoftware.kryo.KryoException
      Returns true if enough bytes are available to read a long with readLong(boolean).
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readString

      public String readString()
      Reads the length and string of UTF8 characters, or null. This can read strings written by FixedOutput.writeString(String) , FixedOutput.writeString(CharSequence), and FixedOutput.writeAscii(String).
      Returns:
      May be null.
    • readStringBuilder

      public StringBuilder readStringBuilder()
      Reads the length and string of UTF8 characters, or null. This can read strings written by FixedOutput.writeString(String) , FixedOutput.writeString(CharSequence), and FixedOutput.writeAscii(String).
      Returns:
      May be null.
    • readFloat

      public float readFloat() throws com.esotericsoftware.kryo.KryoException
      Reads a 4 byte float.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readFloat

      public float readFloat(float precision, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Reads a 1-5 byte float with reduced precision.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readShort

      public short readShort() throws com.esotericsoftware.kryo.KryoException
      Reads a 2 byte short.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readShortUnsigned

      public int readShortUnsigned() throws com.esotericsoftware.kryo.KryoException
      Reads a 2 byte short as an int from 0 to 65535.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readLong

      public long readLong() throws com.esotericsoftware.kryo.KryoException
      Reads an 8 byte long.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readLong

      public long readLong(boolean optimizePositive) throws com.esotericsoftware.kryo.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:
      com.esotericsoftware.kryo.KryoException
    • readVarLong

      public long readVarLong(boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Reads a 1-9 byte long. It is guaranteed that a varible length encoding will be used.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readBoolean

      public boolean readBoolean() throws com.esotericsoftware.kryo.KryoException
      Reads a 1 byte boolean.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readChar

      public char readChar() throws com.esotericsoftware.kryo.KryoException
      Reads a 2 byte char.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readDouble

      public double readDouble() throws com.esotericsoftware.kryo.KryoException
      Reads an 8 bytes double.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readDouble

      public double readDouble(double precision, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Reads a 1-9 byte double with reduced precision.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readInts

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

      public long[] readLongs(int length, boolean optimizePositive) throws com.esotericsoftware.kryo.KryoException
      Bulk input of a long array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readInts

      public int[] readInts(int length) throws com.esotericsoftware.kryo.KryoException
      Bulk input of an int array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readLongs

      public long[] readLongs(int length) throws com.esotericsoftware.kryo.KryoException
      Bulk input of a long array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readFloats

      public float[] readFloats(int length) throws com.esotericsoftware.kryo.KryoException
      Bulk input of a float array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readShorts

      public short[] readShorts(int length) throws com.esotericsoftware.kryo.KryoException
      Bulk input of a short array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readChars

      public char[] readChars(int length) throws com.esotericsoftware.kryo.KryoException
      Bulk input of a char array.
      Throws:
      com.esotericsoftware.kryo.KryoException
    • readDoubles

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