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:
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.- Author:
- Roman Levenstein
-
-
Field Summary
Fields Modifier and Type Field Description protected static ByteOrder
nativeOrder
protected ByteBuffer
niobuffer
protected boolean
varIntsEnabled
-
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(InputStream inputStream)
Creates a new Input for reading from an InputStream with a buffer size of 4096.ByteBufferInput(InputStream inputStream, int bufferSize)
Creates a new Input for reading from an InputStream.ByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canReadInt()
Returns true if enough bytes are available to read an int withreadInt(boolean)
.boolean
canReadLong()
Returns true if enough bytes are available to read a long withreadLong(boolean)
.void
close()
Closes the underlying InputStream, if any.protected int
fill(ByteBuffer buffer, int offset, int count)
Fills the buffer with more bytes.ByteBuffer
getByteBuffer()
InputStream
getInputStream()
boolean
getVarIntsEnabled()
Return current setting for variable length encoding of integersByteOrder
order()
void
order(ByteOrder byteOrder)
int
read()
Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.int
read(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.int
read(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.boolean
readBoolean()
Reads a 1 byte boolean.byte
readByte()
Reads a single byte.void
readBytes(byte[] bytes)
Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.void
readBytes(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[].int
readByteUnsigned()
Reads a byte as an int from 0 to 255.char
readChar()
Reads a 2 byte char.char[]
readChars(int length)
Bulk input of a char array.double
readDouble()
Reads an 8 bytes double.double
readDouble(double precision, boolean optimizePositive)
Reads a 1-9 byte double with reduced precision.double[]
readDoubles(int length)
Bulk input of a double array.float
readFloat()
Reads a 4 byte float.float
readFloat(float precision, boolean optimizePositive)
Reads a 1-5 byte float with reduced precision.float[]
readFloats(int length)
Bulk input of a float array.int
readInt()
Reads a 4 byte int.int
readInt(boolean optimizePositive)
Reads a 1-5 byte int.int[]
readInts(int length)
Bulk input of an int array.long
readLong()
Reads an 8 byte long.long
readLong(boolean optimizePositive)
Reads a 1-9 byte long.long[]
readLongs(int length)
Bulk input of a long array.short
readShort()
Reads a 2 byte short.short[]
readShorts(int length)
Bulk input of a short array.int
readShortUnsigned()
Reads a 2 byte short as an int from 0 to 65535.String
readString()
Reads the length and string of UTF8 characters, or null.StringBuilder
readStringBuilder()
Reads the length and string of UTF8 characters, or null.int
readVarInt(boolean optimizePositive)
Reads a 1-5 byte int.long
readVarLong(boolean optimizePositive)
Reads a 1-9 byte long.protected int
require(int required)
void
rewind()
Sets the position and total to zero.void
setBuffer(byte[] bytes)
Sets a new buffer, discarding any previous buffer.void
setBuffer(ByteBuffer buffer)
Sets a new buffer, discarding any previous buffer.void
setInputStream(InputStream inputStream)
Sets a new InputStream.void
setLimit(int limit)
Sets the limit in the buffer.void
setPosition(int position)
Sets the current position in the buffer.void
setVarIntsEnabled(boolean varIntsEnabled)
Controls if a variable length encoding for integer types should be used when serializers suggest it.void
skip(int count)
Discards the specified number of bytes.long
skip(long count)
Discards the specified number of bytes.-
Methods inherited from class com.esotericsoftware.kryo.io.Input
available, eof, fill, getBuffer, limit, position, readInts, readLongs, setBuffer, setTotal, total
-
Methods inherited from class java.io.InputStream
mark, markSupported, reset
-
-
-
-
Field Detail
-
niobuffer
protected ByteBuffer niobuffer
-
varIntsEnabled
protected boolean varIntsEnabled
-
nativeOrder
protected static final ByteOrder nativeOrder
-
-
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(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 Detail
-
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.
-
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. TheInputStream
is set to null.
-
getByteBuffer
public ByteBuffer getByteBuffer()
-
getInputStream
public InputStream getInputStream()
- Overrides:
getInputStream
in classInput
-
setInputStream
public void setInputStream(InputStream inputStream)
Sets a new InputStream. The position and total are reset, discarding any buffered bytes.- Overrides:
setInputStream
in classInput
- Parameters:
inputStream
- May be null.
-
rewind
public void rewind()
Description copied from class:Input
Sets the position and total to zero.
-
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 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).
-
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 classInput
- 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 classInput
- 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 classInput
- Throws:
KryoException
-
setPosition
public void setPosition(int position)
Description copied from class:Input
Sets the current position in the buffer.- Overrides:
setPosition
in classInput
-
setLimit
public void setLimit(int limit)
Sets the limit in the buffer.
-
skip
public void skip(int count) throws KryoException
Description copied from class:Input
Discards the specified number of bytes.- Overrides:
skip
in classInput
- Throws:
KryoException
-
skip
public long skip(long count) throws KryoException
Discards the specified number of bytes.- Overrides:
skip
in classInput
- Throws:
KryoException
-
close
public void close() throws KryoException
Closes the underlying InputStream, if any.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInput
- Throws:
KryoException
-
readByte
public byte readByte() throws KryoException
Reads a single byte.- Overrides:
readByte
in classInput
- Throws:
KryoException
-
readByteUnsigned
public int readByteUnsigned() throws KryoException
Reads a byte as an int from 0 to 255.- Overrides:
readByteUnsigned
in classInput
- Throws:
KryoException
-
readBytes
public byte[] readBytes(int length) throws KryoException
Reads the specified number of bytes into a new byte[].- Overrides:
readBytes
in classInput
- 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 classInput
- 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 classInput
- Throws:
KryoException
-
readInt
public int readInt() throws KryoException
Description copied from class:Input
Reads a 4 byte int.- Overrides:
readInt
in classInput
- 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 classInput
- 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 classInput
- Throws:
KryoException
-
canReadInt
public boolean canReadInt() throws KryoException
Returns true if enough bytes are available to read an int withreadInt(boolean)
.- Overrides:
canReadInt
in classInput
- Throws:
KryoException
-
canReadLong
public boolean canReadLong() throws KryoException
Returns true if enough bytes are available to read a long withreadLong(boolean)
.- Overrides:
canReadLong
in classInput
- Throws:
KryoException
-
readString
public 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:
readString
in classInput
- Returns:
- May be null.
-
readStringBuilder
public 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:
readStringBuilder
in classInput
- Returns:
- May be null.
-
readFloat
public float readFloat() throws KryoException
Reads a 4 byte float.- Overrides:
readFloat
in classInput
- Throws:
KryoException
-
readFloat
public float readFloat(float precision, boolean optimizePositive) throws KryoException
Reads a 1-5 byte float with reduced precision.- Overrides:
readFloat
in classInput
- Throws:
KryoException
-
readShort
public short readShort() throws KryoException
Reads a 2 byte short.- Overrides:
readShort
in classInput
- Throws:
KryoException
-
readShortUnsigned
public int readShortUnsigned() throws KryoException
Reads a 2 byte short as an int from 0 to 65535.- Overrides:
readShortUnsigned
in classInput
- Throws:
KryoException
-
readLong
public long readLong() throws KryoException
Reads an 8 byte long.- Overrides:
readLong
in classInput
- 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 classInput
- 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 classInput
- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoException
Reads a 1 byte boolean.- Overrides:
readBoolean
in classInput
- Throws:
KryoException
-
readChar
public char readChar() throws KryoException
Reads a 2 byte char.- Overrides:
readChar
in classInput
- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoException
Reads an 8 bytes double.- Overrides:
readDouble
in classInput
- Throws:
KryoException
-
readDouble
public double readDouble(double precision, boolean optimizePositive) throws KryoException
Reads a 1-9 byte double with reduced precision.- Overrides:
readDouble
in classInput
- Throws:
KryoException
-
readInts
public int[] readInts(int length) throws KryoException
Bulk input of an int array.- Overrides:
readInts
in classInput
- Throws:
KryoException
-
readLongs
public long[] readLongs(int length) throws KryoException
Bulk input of a long array.- Overrides:
readLongs
in classInput
- Throws:
KryoException
-
readFloats
public float[] readFloats(int length) throws KryoException
Bulk input of a float array.- Overrides:
readFloats
in classInput
- Throws:
KryoException
-
readShorts
public short[] readShorts(int length) throws KryoException
Bulk input of a short array.- Overrides:
readShorts
in classInput
- Throws:
KryoException
-
readChars
public char[] readChars(int length) throws KryoException
Bulk input of a char array.- Overrides:
readChars
in classInput
- Throws:
KryoException
-
readDoubles
public double[] readDoubles(int length) throws KryoException
Bulk input of a double array.- Overrides:
readDoubles
in classInput
- Throws:
KryoException
-
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
-
-
-