Package com.esotericsoftware.kryo.io
Class ByteBufferInput
- java.lang.Object
-
- java.io.InputStream
-
- com.esotericsoftware.kryo.io.Input
-
- com.esotericsoftware.kryo.io.ByteBufferInput
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.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 Summary
Fields Modifier and Type Field Description (package private) java.nio.ByteOrderbyteOrderprotected static java.nio.ByteOrdernativeOrderprotected java.nio.ByteBufferniobufferprotected booleanvarIntsEnabled
-
Constructor Summary
Constructors Constructor Description ByteBufferInput()Creates an uninitialized Input.ByteBufferInput(byte[] buffer)ByteBufferInput(int bufferSize)Creates a new Input for reading from a byte array.ByteBufferInput(java.io.InputStream inputStream)Creates a new Input for reading from an InputStream with a buffer size of 4096.ByteBufferInput(java.io.InputStream inputStream, int bufferSize)Creates a new Input for reading from an InputStream.ByteBufferInput(java.nio.ByteBuffer buffer)Creates a new Input for reading from a ByteBuffer.
-
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(java.nio.ByteBuffer buffer, int offset, int count)Fills the buffer with more bytes.java.nio.ByteBuffergetByteBuffer()java.io.InputStreamgetInputStream()booleangetVarIntsEnabled()Return current setting for variable length encoding of integersprivate booleanisNativeOrder()private intoptional(int optional)java.nio.ByteOrderorder()voidorder(java.nio.ByteOrder byteOrder)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 java.lang.StringreadAscii()private java.lang.StringreadAscii_slow()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.char[]readChars(int length)Bulk input of a char array.doublereadDouble()Reads 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.floatreadFloat()Reads 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.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.shortreadShort()Reads a 2 byte short.short[]readShorts(int length)Bulk input of a short array.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.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, discarding any previous buffer.voidsetBuffer(java.nio.ByteBuffer buffer)Sets a new buffer, discarding any previous 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.voidsetVarIntsEnabled(boolean varIntsEnabled)Controls if a variable length encoding for integer types should be used when serializers suggest it.voidskip(int count)Discards the specified number of bytes.longskip(long count)Discards the specified number of bytes.
-
-
-
Constructor Detail
-
ByteBufferInput
public ByteBufferInput()
Creates an uninitialized Input. A buffer must be set before the Input is used.- See Also:
setBuffer(ByteBuffer)
-
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(java.nio.ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
ByteBufferInput
public ByteBufferInput(java.io.InputStream inputStream)
Creates a new Input for reading from an InputStream with a buffer size of 4096.
-
ByteBufferInput
public ByteBufferInput(java.io.InputStream inputStream, int bufferSize)Creates a new Input for reading from an InputStream.
-
-
Method Detail
-
order
public java.nio.ByteOrder order()
-
order
public void order(java.nio.ByteOrder byteOrder)
-
setBuffer
public void setBuffer(byte[] bytes)
Sets a new buffer, discarding any previous buffer. The position and total are reset.
-
setBuffer
public void setBuffer(java.nio.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. TheInputStreamis set to null.
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
-
getInputStream
public java.io.InputStream getInputStream()
- Overrides:
getInputStreamin classInput
-
setInputStream
public void setInputStream(java.io.InputStream inputStream)
Sets a new InputStream. The position and total are reset, discarding any buffered bytes.- Overrides:
setInputStreamin classInput- Parameters:
inputStream- May be null.
-
rewind
public void rewind()
Description copied from class:InputSets the position and total to zero.
-
fill
protected int fill(java.nio.ByteBuffer 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
-
require
protected final int require(int required) throws KryoException- Overrides:
requirein classInput- 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 KryoExceptionReads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.- Overrides:
readin classInput- 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 classInput- 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 classInput- Throws:
KryoException
-
setPosition
public void setPosition(int position)
Description copied from class:InputSets the current position in the buffer.- Overrides:
setPositionin classInput
-
setLimit
public void setLimit(int limit)
Sets the limit in the buffer.
-
skip
public void skip(int count) throws KryoExceptionDescription copied from class:InputDiscards the specified number of bytes.- Overrides:
skipin classInput- Throws:
KryoException
-
skip
public long skip(long count) throws KryoExceptionDiscards the specified number of bytes.- Overrides:
skipin classInput- 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 classInput- Throws:
KryoException
-
readByte
public byte readByte() throws KryoExceptionReads a single byte.- Overrides:
readBytein classInput- Throws:
KryoException
-
readByteUnsigned
public int readByteUnsigned() throws KryoExceptionReads a byte as an int from 0 to 255.- Overrides:
readByteUnsignedin classInput- Throws:
KryoException
-
readBytes
public byte[] readBytes(int length) throws KryoExceptionReads the specified number of bytes into a new byte[].- Overrides:
readBytesin classInput- Throws:
KryoException
-
readBytes
public void readBytes(byte[] bytes) throws KryoExceptionReads bytes.length bytes and writes them to the specified byte[], starting at index 0.- Overrides:
readBytesin classInput- 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.- Overrides:
readBytesin classInput- Throws:
KryoException
-
readInt
public int readInt() throws KryoExceptionDescription copied from class:InputReads a 4 byte int.- Overrides:
readIntin classInput- Throws:
KryoException
-
readInt
public int readInt(boolean optimizePositive) throws KryoExceptionDescription copied from class:InputReads 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:
readIntin classInput- Throws:
KryoException
-
readVarInt
public int readVarInt(boolean optimizePositive) throws KryoExceptionDescription copied from class:InputReads a 1-5 byte int. It is guaranteed that a varible length encoding will be used.- Overrides:
readVarIntin classInput- Throws:
KryoException
-
readInt_slow
private int readInt_slow(boolean optimizePositive)
-
canReadInt
public boolean canReadInt() throws KryoExceptionReturns true if enough bytes are available to read an int withreadInt(boolean).- Overrides:
canReadIntin classInput- Throws:
KryoException
-
canReadLong
public boolean canReadLong() throws KryoExceptionReturns true if enough bytes are available to read a long withreadLong(boolean).- Overrides:
canReadLongin classInput- 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).- Overrides:
readStringin classInput- 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 java.lang.String readAscii()
-
readAscii_slow
private java.lang.String readAscii_slow()
-
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).- Overrides:
readStringBuilderin classInput- Returns:
- May be null.
-
readFloat
public float readFloat() throws KryoExceptionReads a 4 byte float.- Overrides:
readFloatin classInput- Throws:
KryoException
-
readFloat
public float readFloat(float precision, boolean optimizePositive) throws KryoExceptionReads a 1-5 byte float with reduced precision.- Overrides:
readFloatin classInput- Throws:
KryoException
-
readShort
public short readShort() throws KryoExceptionReads a 2 byte short.- Overrides:
readShortin classInput- Throws:
KryoException
-
readShortUnsigned
public int readShortUnsigned() throws KryoExceptionReads a 2 byte short as an int from 0 to 65535.- Overrides:
readShortUnsignedin classInput- Throws:
KryoException
-
readLong
public long readLong() throws KryoExceptionReads an 8 byte long.- Overrides:
readLongin classInput- Throws:
KryoException
-
readLong
public long readLong(boolean optimizePositive) throws KryoExceptionReads 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:
readLongin classInput- Throws:
KryoException
-
readVarLong
public long readVarLong(boolean optimizePositive) throws KryoExceptionReads a 1-9 byte long. It is guaranteed that a varible length encoding will be used.- Overrides:
readVarLongin classInput- Throws:
KryoException
-
readLong_slow
private long readLong_slow(boolean optimizePositive)
-
readBoolean
public boolean readBoolean() throws KryoExceptionReads a 1 byte boolean.- Overrides:
readBooleanin classInput- Throws:
KryoException
-
readChar
public char readChar() throws KryoExceptionReads a 2 byte char.- Overrides:
readCharin classInput- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoExceptionReads an 8 bytes double.- Overrides:
readDoublein classInput- Throws:
KryoException
-
readDouble
public double readDouble(double precision, boolean optimizePositive) throws KryoExceptionReads a 1-9 byte double with reduced precision.- Overrides:
readDoublein classInput- Throws:
KryoException
-
readInts
public int[] readInts(int length) throws KryoExceptionBulk input of an int array.- Overrides:
readIntsin classInput- Throws:
KryoException
-
readLongs
public long[] readLongs(int length) throws KryoExceptionBulk input of a long array.- Overrides:
readLongsin classInput- Throws:
KryoException
-
readFloats
public float[] readFloats(int length) throws KryoExceptionBulk input of a float array.- Overrides:
readFloatsin classInput- Throws:
KryoException
-
readShorts
public short[] readShorts(int length) throws KryoExceptionBulk input of a short array.- Overrides:
readShortsin classInput- Throws:
KryoException
-
readChars
public char[] readChars(int length) throws KryoExceptionBulk input of a char array.- Overrides:
readCharsin classInput- Throws:
KryoException
-
readDoubles
public double[] readDoubles(int length) throws KryoExceptionBulk input of a double array.- Overrides:
readDoublesin classInput- 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-
-
-