Class Input
java.lang.Object
java.io.InputStream
com.esotericsoftware.kryo.io.Input
- All Implemented Interfaces:
Closeable, AutoCloseable
- Direct Known Subclasses:
ByteBufferInput, InputChunked
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
FieldsModifier and TypeFieldDescriptionprotected byte[]protected intprotected char[]protected InputStreamprotected intprotected intprotected long -
Constructor Summary
ConstructorsConstructorDescriptionInput()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(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. -
Method Summary
Modifier and TypeMethodDescriptionintbooleanReturns true if enough bytes are available to read an int withreadInt(boolean).booleanReturns true if enough bytes are available to read a long withreadLong(boolean).voidclose()Closes the underlying InputStream, if any.booleaneof()protected intfill(byte[] buffer, int offset, int count) Fills the buffer with more bytes.byte[]intlimit()Returns the limit for the buffer.private intoptional(int optional) intposition()Returns the current position in the buffer.intread()Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.intread(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.intread(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 Stringprivate StringbooleanReads a 1 byte boolean.bytereadByte()Reads a single byte.voidreadBytes(byte[] bytes) Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.voidreadBytes(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[].intReads a byte as an int from 0 to 255.charreadChar()Reads a 2 byte char.char[]readChars(int length) Bulk input of a char array.doubleReads an 8 bytes double.doublereadDouble(double precision, boolean optimizePositive) Reads a 1-9 byte double with reduced precision.double[]readDoubles(int length) Bulk input of a double array.floatReads a 4 byte float.floatreadFloat(float precision, boolean optimizePositive) Reads a 1-5 byte float with reduced precision.float[]readFloats(int length) Bulk input of a float array.intreadInt()Reads a 4 byte int.intreadInt(boolean optimizePositive) Reads a 1-5 byte int.private intreadInt_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.longreadLong()Reads an 8 byte long.longreadLong(boolean optimizePositive) Reads a 1-9 byte long.private longreadLong_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.shortReads a 2 byte short.short[]readShorts(int length) Bulk input of a short array.intReads 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.private voidreadUtf8(int charCount) private voidreadUtf8_slow(int charCount, int charIndex) private intreadUtf8Length(int b) private intreadUtf8Length_slow(int b) intreadVarInt(boolean optimizePositive) Reads a 1-5 byte int.longreadVarLong(boolean optimizePositive) Reads a 1-9 byte long.protected intrequire(int required) voidrewind()Sets the position and total to zero.voidsetBuffer(byte[] bytes) Sets a new buffer.voidsetBuffer(byte[] bytes, int offset, int count) Sets a new buffer.voidsetInputStream(InputStream inputStream) Sets a new InputStream.voidsetLimit(int limit) Sets the limit in the buffer.voidsetPosition(int position) Sets the current position in the buffer.voidsetTotal(long total) Sets the number of bytes read.voidskip(int count) Discards the specified number of bytes.longskip(long count) Discards the specified number of bytes.longtotal()Returns the number of bytes read.Methods inherited from class InputStream
mark, markSupported, reset
-
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
-
-
Constructor Details
-
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
Creates a new Input for reading from an InputStream with a buffer size of 4096. -
Input
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
-
setInputStream
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. -
skip
Discards the specified number of bytes.- Throws:
KryoException
-
fill
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
- 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
- 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
- Overrides:
availablein classInputStream- Throws:
IOException
-
read
Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.- Specified by:
readin classInputStream- Throws:
KryoException
-
read
Reads bytes.length bytes or less and writes them to the specified byte[], starting at 0, and returns the number of bytes read.- Overrides:
readin classInputStream- Throws:
KryoException
-
read
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:
readin classInputStream- Throws:
KryoException
-
skip
Discards the specified number of bytes.- Overrides:
skipin classInputStream- Throws:
KryoException
-
close
Closes the underlying InputStream, if any.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
KryoException
-
readByte
-
readByteUnsigned
Reads a byte as an int from 0 to 255.- Throws:
KryoException
-
readBytes
Reads the specified number of bytes into a new byte[].- Throws:
KryoException
-
readBytes
Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.- Throws:
KryoException
-
readBytes
Reads count bytes and writes them to the specified byte[], starting at offset.- Throws:
KryoException
-
readInt
-
readInt
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
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) -
canReadInt
Returns true if enough bytes are available to read an int withreadInt(boolean).- Throws:
KryoException
-
canReadLong
Returns true if enough bytes are available to read a long withreadLong(boolean).- Throws:
KryoException
-
readString
Reads the length and string of UTF8 characters, or null. This can read strings written byOutput.writeString(String),Output.writeString(CharSequence), andOutput.writeAscii(String).- 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
-
readAscii_slow
-
readStringBuilder
Reads the length and string of UTF8 characters, or null. This can read strings written byOutput.writeString(String),Output.writeString(CharSequence), andOutput.writeAscii(String).- Returns:
- May be null.
-
readFloat
-
readFloat
Reads a 1-5 byte float with reduced precision.- Throws:
KryoException
-
readShort
-
readShortUnsigned
Reads a 2 byte short as an int from 0 to 65535.- Throws:
KryoException
-
readLong
-
readLong
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
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) -
readBoolean
-
readChar
-
readDouble
-
readDouble
Reads a 1-9 byte double with reduced precision.- Throws:
KryoException
-
readInts
Bulk input of an int array.- Throws:
KryoException
-
readLongs
Bulk input of a long array.- Throws:
KryoException
-
readInts
Bulk input of an int array.- Throws:
KryoException
-
readLongs
Bulk input of a long array.- Throws:
KryoException
-
readFloats
Bulk input of a float array.- Throws:
KryoException
-
readShorts
Bulk input of a short array.- Throws:
KryoException
-
readChars
Bulk input of a char array.- Throws:
KryoException
-
readDoubles
Bulk input of a double array.- Throws:
KryoException
-