Class ByteBufferInput

java.lang.Object
java.io.InputStream
com.esotericsoftware.kryo.io.Input
com.esotericsoftware.kryo.io.ByteBufferInput
All Implemented Interfaces:
Closeable, 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.
  • Field Details

    • niobuffer

      protected ByteBuffer niobuffer
    • varIntsEnabled

      protected boolean varIntsEnabled
    • byteOrder

      ByteOrder byteOrder
    • nativeOrder

      protected static final ByteOrder nativeOrder
  • Constructor Details

    • ByteBufferInput

      public ByteBufferInput()
      Creates an uninitialized Input. A buffer must be set before the Input is used.
      See Also:
    • 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(ByteBuffer buffer)
      Creates a new Input for reading from a ByteBuffer.
    • ByteBufferInput

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

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

    • order

      public ByteOrder order()
    • order

      public void order(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(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 ByteBuffer getByteBuffer()
    • getInputStream

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

      public void setInputStream(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(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
    • skip

      public void skip(int count) throws KryoException
      Description copied from class: Input
      Discards the specified number of bytes.
      Overrides:
      skip in class Input
      Throws:
      KryoException
    • skip

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

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

      public byte readByte() throws KryoException
      Reads a single byte.
      Overrides:
      readByte in class Input
      Throws:
      KryoException
    • readByteUnsigned

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

      public byte[] readBytes(int length) throws KryoException
      Reads the specified number of bytes into a new byte[].
      Overrides:
      readBytes 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() throws KryoException
      Description copied from class: Input
      Reads a 4 byte int.
      Overrides:
      readInt 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)
    • canReadInt

      public boolean canReadInt() throws KryoException
      Returns true if enough bytes are available to read an int with readInt(boolean).
      Overrides:
      canReadInt in class Input
      Throws:
      KryoException
    • canReadLong

      public boolean canReadLong() throws KryoException
      Returns true if enough bytes are available to read a long with readLong(boolean).
      Overrides:
      canReadLong in class Input
      Throws:
      KryoException
    • readString

      public String readString()
      Reads the length and string of UTF8 characters, or null. This can read strings written by Output.writeString(String) , Output.writeString(CharSequence), and Output.writeAscii(String).
      Overrides:
      readString in class Input
      Returns:
      May be null.
    • 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 String readAscii()
    • readAscii_slow

      private String readAscii_slow()
    • readStringBuilder

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

      public float readFloat() throws KryoException
      Reads a 4 byte float.
      Overrides:
      readFloat in class Input
      Throws:
      KryoException
    • 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
    • readShort

      public short readShort() throws KryoException
      Reads a 2 byte short.
      Overrides:
      readShort in class Input
      Throws:
      KryoException
    • readShortUnsigned

      public int readShortUnsigned() throws KryoException
      Reads a 2 byte short as an int from 0 to 65535.
      Overrides:
      readShortUnsigned in class Input
      Throws:
      KryoException
    • readLong

      public long readLong() throws KryoException
      Reads an 8 byte long.
      Overrides:
      readLong 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)
    • readBoolean

      public boolean readBoolean() throws KryoException
      Reads a 1 byte boolean.
      Overrides:
      readBoolean in class Input
      Throws:
      KryoException
    • readChar

      public char readChar() throws KryoException
      Reads a 2 byte char.
      Overrides:
      readChar in class Input
      Throws:
      KryoException
    • readDouble

      public double readDouble() throws KryoException
      Reads an 8 bytes double.
      Overrides:
      readDouble in class Input
      Throws:
      KryoException
    • 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
    • readInts

      public int[] readInts(int length) throws KryoException
      Bulk input of an int array.
      Overrides:
      readInts in class Input
      Throws:
      KryoException
    • readLongs

      public long[] readLongs(int length) throws KryoException
      Bulk input of a long array.
      Overrides:
      readLongs in class Input
      Throws:
      KryoException
    • readFloats

      public float[] readFloats(int length) throws KryoException
      Bulk input of a float array.
      Overrides:
      readFloats in class Input
      Throws:
      KryoException
    • readShorts

      public short[] readShorts(int length) throws KryoException
      Bulk input of a short array.
      Overrides:
      readShorts in class Input
      Throws:
      KryoException
    • readChars

      public char[] readChars(int length) throws KryoException
      Bulk input of a char array.
      Overrides:
      readChars in class Input
      Throws:
      KryoException
    • readDoubles

      public double[] readDoubles(int length) throws KryoException
      Bulk input of a double array.
      Overrides:
      readDoubles 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 -