Package com.esotericsoftware.kryo.io
Class Input
- java.lang.Object
-
- java.io.InputStream
-
- com.esotericsoftware.kryo.io.Input
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
InputChunked
public class Input extends java.io.InputStreamAn InputStream that reads data from a byte array and optionally fills the byte array from another OutputStream as needed. Utility methods are provided for efficiently reading primitive types and strings.
-
-
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 booleancanReadInt()Returns true if enough bytes are available to read an int withreadInt(boolean).booleancanReadLong()Returns true if enough bytes are available to read a long withreadLong(boolean).voidclose()Closes the underlying InputStream, if any.protected intfill(byte[] buffer, int offset, int count)Fills the buffer with more bytes.byte[]getBuffer()java.io.InputStreamgetInputStream()intlimit()Returns the limit for the buffer.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.booleanreadBoolean()Reads 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[].intreadByteUnsigned()Reads a byte as an int from 0 to 255.charreadChar()Reads a 2 byte char.doublereadDouble()Reads an 8 bytes double.doublereadDouble(double precision, boolean optimizePositive)Reads a 1-9 byte double with reduced precision.floatreadFloat()Reads a 4 byte float.floatreadFloat(float precision, boolean optimizePositive)Reads a 1-5 byte float with reduced precision.intreadInt()Reads a 4 byte int.intreadInt(boolean optimizePositive)Reads a 1-5 byte int.longreadLong()Reads an 8 byte long.longreadLong(boolean optimizePositive)Reads a 1-9 byte long.shortreadShort()Reads a 2 byte short.intreadShortUnsigned()Reads a 2 byte short as an int from 0 to 65535.java.lang.StringreadString()Reads the length and string of UTF8 characters, or null.java.lang.StringBuilderreadStringBuilder()Reads the length and string of UTF8 characters, or null.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(java.io.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(int 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.inttotal()Returns the number of bytes read.
-
-
-
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 int total()
Returns the number of bytes read.
-
setTotal
public void setTotal(int 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
public void skip(int count) throws KryoExceptionDiscards the specified number of bytes.- Throws:
KryoException
-
fill
protected int fill(byte[] buffer, int offset, int count) throws KryoExceptionFills the buffer with more bytes. Can be overridden to fill the bytes from a source other than the InputStream.- Throws:
KryoException
-
read
public int read() throws KryoExceptionReads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.- Specified by:
readin classjava.io.InputStream- Throws:
KryoException
-
read
public int read(byte[] bytes) throws KryoExceptionReads bytes.length bytes or less and writes them to the specified byte[], starting at 0, and returns the number of bytes read.- Overrides:
readin classjava.io.InputStream- Throws:
KryoException
-
read
public int read(byte[] bytes, int offset, int count) throws KryoExceptionReads 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 classjava.io.InputStream- Throws:
KryoException
-
skip
public long skip(long count) throws KryoExceptionDiscards the specified number of bytes.- Overrides:
skipin classjava.io.InputStream- Throws:
KryoException
-
close
public void close() throws KryoExceptionCloses the underlying InputStream, if any.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
KryoException
-
readByte
public byte readByte() throws KryoExceptionReads a single byte.- Throws:
KryoException
-
readByteUnsigned
public int readByteUnsigned() throws KryoExceptionReads a byte as an int from 0 to 255.- Throws:
KryoException
-
readBytes
public byte[] readBytes(int length) throws KryoExceptionReads the specified number of bytes into a new byte[].- Throws:
KryoException
-
readBytes
public void readBytes(byte[] bytes) throws KryoExceptionReads 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 KryoExceptionReads count bytes and writes them to the specified byte[], starting at offset.- Throws:
KryoException
-
readInt
public int readInt() throws KryoExceptionReads a 4 byte int.- Throws:
KryoException
-
readInt
public int readInt(boolean optimizePositive) throws KryoExceptionReads a 1-5 byte int.- Throws:
KryoException
-
canReadInt
public boolean canReadInt() throws KryoExceptionReturns true if enough bytes are available to read an int withreadInt(boolean).- Throws:
KryoException
-
canReadLong
public boolean canReadLong() throws KryoExceptionReturns true if enough bytes are available to read a long withreadLong(boolean).- Throws:
KryoException
-
readString
public java.lang.String 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.
-
readStringBuilder
public java.lang.StringBuilder 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
public float readFloat() throws KryoExceptionReads a 4 byte float.- Throws:
KryoException
-
readFloat
public float readFloat(float precision, boolean optimizePositive) throws KryoExceptionReads a 1-5 byte float with reduced precision.- Throws:
KryoException
-
readShort
public short readShort() throws KryoExceptionReads a 2 byte short.- Throws:
KryoException
-
readShortUnsigned
public int readShortUnsigned() throws KryoExceptionReads a 2 byte short as an int from 0 to 65535.- Throws:
KryoException
-
readLong
public long readLong() throws KryoExceptionReads an 8 byte long.- Throws:
KryoException
-
readLong
public long readLong(boolean optimizePositive) throws KryoExceptionReads a 1-9 byte long.- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoExceptionReads a 1 byte boolean.- Throws:
KryoException
-
readChar
public char readChar() throws KryoExceptionReads a 2 byte char.- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoExceptionReads an 8 bytes double.- Throws:
KryoException
-
readDouble
public double readDouble(double precision, boolean optimizePositive) throws KryoExceptionReads a 1-9 byte double with reduced precision.- Throws:
KryoException
-
-