Package com.esotericsoftware.kryo.io
Class Input
- java.lang.Object
-
- java.io.InputStream
-
- com.esotericsoftware.kryo.io.Input
-
- All Implemented Interfaces:
Pool.Poolable,java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
ByteBufferInput,InputChunked,UnsafeInput
public class Input extends java.io.InputStream implements Pool.Poolable
An InputStream that reads data from a byte[] and optionally fills the byte[] from another InputStream as needed. Utility methods are provided for efficiently reading primitive types and strings.- Author:
- Nathan Sweet
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufferprotected intcapacityprotected char[]charsprotected java.io.InputStreaminputStreamprotected intlimitprotected intpositionprotected longtotalprotected booleanvarEncoding
-
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(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 with the specified buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()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).booleancanReadVarInt()Returns true if enough bytes are available to read an int withreadVarInt(boolean).booleancanReadVarLong()Returns true if enough bytes are available to read a long withreadVarLong(boolean).voidclose()Closes the underlying InputStream, if any.booleanend()Returns true if thelimit()has been reached andfill(byte[], int, int)is unable to provide more bytes.protected intfill(byte[] buffer, int offset, int count)Fills the buffer with more bytes.byte[]getBuffer()Returns the buffer.java.io.InputStreamgetInputStream()booleangetVariableLengthEncoding()intlimit()Returns the limit for the buffer.protected intoptional(int optional)Fills the buffer with at least the number of bytes specified, if possible.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.boolean[]readBooleans(int length)Reads a boolean array in bulk.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.char[]readChars(int length)Reads a char array in bulk.doublereadDouble()Reads an 8 byte double.double[]readDoubles(int length)Reads a double array in bulk.floatreadFloat()Reads a 4 byte float.float[]readFloats(int length)Reads a float array in bulk.intreadInt()Reads a 4 byte int.intreadInt(boolean optimizePositive)Reads an int using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).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 onsetVariableLengthEncoding(boolean).longreadLong()Reads an 8 byte long.longreadLong(boolean optimizePositive)Reads a long using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).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 onsetVariableLengthEncoding(boolean).shortreadShort()Reads a 2 byte short.short[]readShorts(int length)Reads a short array in bulk.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.doublereadVarDouble(double precision, boolean optimizePositive)Reads a 1-9 byte double with reduced precision.floatreadVarFloat(float precision, boolean optimizePositive)Reads a 1-5 byte float with reduced precision.intreadVarInt(boolean optimizePositive)Reads a 1-5 byte int.booleanreadVarIntFlag()Reads the boolean part of a varint flag.intreadVarIntFlag(boolean optimizePositive)Reads the 1-5 byte int part of a varint flag.longreadVarLong(boolean optimizePositive)Reads a 1-9 byte long.protected intrequire(int required)Fills the buffer with at least the number of bytes specified.voidreset()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 to read from.voidsetInputStream(java.io.InputStream inputStream)Sets an InputStream to read from when data in the buffer is exhausted.voidsetLimit(int limit)Sets the limit in the buffer which marks the end of the data that can be read.voidsetPosition(int position)Sets the current position in the buffer where the next byte will be read.voidsetTotal(long total)Sets the total number of bytes read.voidsetVariableLengthEncoding(boolean varEncoding)If false,readInt(boolean),readLong(boolean),readInts(int, boolean), andreadLongs(int, boolean)will use fixed length encoding, which may be faster for some data.voidskip(int count)Discards the specified number of bytes.longskip(long count)Discards the specified number of bytes.longtotal()Returns the total 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[] buffer.- Parameters:
bufferSize- The size of the buffer. An exception is thrown if more bytes than this are read andfill(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 andfill(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 andfill(byte[], int, int)does not supply more bytes.
-
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 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. TheInputStreamis set to null.
-
getBuffer
public byte[] getBuffer()
Returns the buffer. The bytes between 0 andposition()are the data that can be read.
-
getInputStream
public java.io.InputStream getInputStream()
-
setInputStream
public void setInputStream(java.io.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()
-
setVariableLengthEncoding
public void setVariableLengthEncoding(boolean varEncoding)
If false,readInt(boolean),readLong(boolean),readInts(int, boolean), andreadLongs(int, boolean)will use fixed length encoding, which may be faster for some data. Default is true.
-
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.
-
reset
public void reset()
Sets the position and total to zero.- Specified by:
resetin interfacePool.Poolable- Overrides:
resetin classjava.io.InputStream
-
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. The default implementation reads from theInputStream, 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 KryoExceptionFills 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
requiredbytes. - Throws:
KryoException- iffill(byte[], int, int)is unable to provide more bytes (buffer underflow).
-
optional
protected int optional(int optional) throws KryoExceptionFills 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
-
end
public boolean end()
Returns true if thelimit()has been reached andfill(byte[], int, int)is unable to provide more bytes.
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
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 an int using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). UsereadVarInt(boolean)explicitly when reading values that should always use variable length encoding (eg values that appear many times).- Throws:
KryoException- See Also:
canReadInt()
-
canReadInt
public boolean canReadInt() throws KryoExceptionReturns true if enough bytes are available to read an int withreadInt(boolean).- Throws:
KryoException
-
readVarInt
public int readVarInt(boolean optimizePositive) throws KryoExceptionReads a 1-5 byte int.- Throws:
KryoException- See Also:
canReadVarInt()
-
canReadVarInt
public boolean canReadVarInt() throws KryoExceptionReturns true if enough bytes are available to read an int withreadVarInt(boolean).- Throws:
KryoException
-
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 withreadVarIntFlag().
-
readLong
public long readLong() throws KryoExceptionReads an 8 byte long.- Throws:
KryoException
-
readLong
public long readLong(boolean optimizePositive) throws KryoExceptionReads a long using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). UsereadVarLong(boolean)explicitly when reading values that should always use variable length encoding (eg values that appear many times).- Throws:
KryoException- See Also:
canReadLong()
-
readVarLong
public long readVarLong(boolean optimizePositive) throws KryoExceptionReads a 1-9 byte long.- Throws:
KryoException- See Also:
canReadLong()
-
canReadLong
public boolean canReadLong() throws KryoExceptionReturns true if enough bytes are available to read a long withreadLong(boolean).- Throws:
KryoException
-
canReadVarLong
public boolean canReadVarLong() throws KryoExceptionReturns true if enough bytes are available to read a long withreadVarLong(boolean).- Throws:
KryoException
-
readFloat
public float readFloat() throws KryoExceptionReads a 4 byte float.- Throws:
KryoException
-
readVarFloat
public float readVarFloat(float precision, boolean optimizePositive) throws KryoExceptionReads a 1-5 byte float with reduced precision.- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoExceptionReads an 8 byte double.- Throws:
KryoException
-
readVarDouble
public double readVarDouble(double precision, boolean optimizePositive) throws KryoExceptionReads a 1-9 byte double 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
-
readChar
public char readChar() throws KryoExceptionReads a 2 byte char.- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoExceptionReads a 1 byte 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)andOutput.writeAscii(String).- Returns:
- May be null.
-
readStringBuilder
public java.lang.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 byOutput.writeString(String)andOutput.writeAscii(String).- Returns:
- May be null.
-
readInts
public int[] readInts(int length) throws KryoExceptionReads an int array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readInts
public int[] readInts(int length, boolean optimizePositive) throws KryoExceptionReads an int array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). This may be more efficient than reading them individually.- Throws:
KryoException
-
readLongs
public long[] readLongs(int length) throws KryoExceptionReads a long array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readLongs
public long[] readLongs(int length, boolean optimizePositive) throws KryoExceptionReads an int array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). This may be more efficient than reading them individually.- Throws:
KryoException
-
readFloats
public float[] readFloats(int length) throws KryoExceptionReads a float array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readDoubles
public double[] readDoubles(int length) throws KryoExceptionReads a double array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readShorts
public short[] readShorts(int length) throws KryoExceptionReads a short array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readChars
public char[] readChars(int length) throws KryoExceptionReads a char array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
readBooleans
public boolean[] readBooleans(int length) throws KryoExceptionReads a boolean array in bulk. This may be more efficient than reading them individually.- Throws:
KryoException
-
-