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:
Pool.Poolable
,Closeable
,AutoCloseable
- Direct Known Subclasses:
UnsafeByteBufferInput
public class ByteBufferInput extends Input
AnInput
that uses a ByteBuffer rather than a byte[].Note that the byte[]
buffer
is not used. Code taking an Input and expecting the byte[] to be used may not work correctly.- Author:
- Roman Levenstein
, Nathan Sweet
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteBuffer
byteBuffer
-
Fields inherited from class com.esotericsoftware.kryo.io.Input
buffer, capacity, chars, inputStream, limit, position, total, varEncoding
-
-
Constructor Summary
Constructors Constructor Description ByteBufferInput()
Creates an uninitialized Input,setBuffer(ByteBuffer)
must be called before the Input is used.ByteBufferInput(byte[] bytes)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.ByteBufferInput(byte[] bytes, int offset, int count)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.ByteBufferInput(int bufferSize)
Creates a new Input for reading from a directByteBuffer
.ByteBufferInput(InputStream inputStream)
ByteBufferInput(InputStream inputStream, int bufferSize)
ByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
canReadVarInt()
Returns true if enough bytes are available to read an int withInput.readVarInt(boolean)
.boolean
canReadVarLong()
Returns true if enough bytes are available to read a long withInput.readVarLong(boolean)
.void
close()
Closes the underlying InputStream, if any.protected int
fill(ByteBuffer buffer, int offset, int count)
Fills the buffer with more bytes.byte[]
getBuffer()
Deprecated.ByteBuffer
getByteBuffer()
protected int
optional(int optional)
Fills the buffer with at least the number of bytes specified, if possible.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.boolean[]
readBooleans(int length)
Reads a boolean array in bulk.byte
readByte()
Reads a single byte.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)
Reads a char array in bulk.double
readDouble()
Reads an 8 byte double.double[]
readDoubles(int length)
Reads a double array in bulk.float
readFloat()
Reads a 4 byte float.float[]
readFloats(int length)
Reads a float array in bulk.int
readInt()
Reads a 4 byte int.int[]
readInts(int length)
Reads an int array in bulk.long
readLong()
Reads an 8 byte long.long[]
readLongs(int length)
Reads a long array in bulk.short
readShort()
Reads a 2 byte short.short[]
readShorts(int length)
Reads a short array in bulk.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.boolean
readVarIntFlag()
Reads the boolean part of a varint flag.int
readVarIntFlag(boolean optimizePositive)
Reads the 1-5 byte int part of a varint flag.long
readVarLong(boolean optimizePositive)
Reads a 1-9 byte long.protected int
require(int required)
Fills the buffer with at least the number of bytes specified.void
reset()
Sets the position and total to zero.void
setBuffer(byte[] bytes)
Deprecated.void
setBuffer(byte[] bytes, int offset, int count)
Deprecated.void
setBuffer(ByteBuffer buffer)
Sets a new buffer to read from.void
setInputStream(InputStream inputStream)
Sets an InputStream to read from when data in the buffer is exhausted.void
setLimit(int limit)
Sets the limit in the buffer which marks the end of the data that can be read.void
setPosition(int position)
Sets the current position in the buffer where the next byte will be read.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, canReadInt, canReadLong, end, fill, getInputStream, getVariableLengthEncoding, limit, position, readBytes, readInt, readInts, readLong, readLongs, readVarDouble, readVarFloat, setTotal, setVariableLengthEncoding, total
-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
byteBuffer
protected ByteBuffer byteBuffer
-
-
Constructor Detail
-
ByteBufferInput
public ByteBufferInput()
Creates an uninitialized Input,setBuffer(ByteBuffer)
must be called before the Input is used.
-
ByteBufferInput
public ByteBufferInput(int bufferSize)
Creates a new Input for reading from a directByteBuffer
.- Parameters:
bufferSize
- The size of the buffer. An exception is thrown if more bytes than this are read andfill(ByteBuffer, int, int)
does not supply more bytes.
-
ByteBufferInput
public ByteBufferInput(byte[] bytes)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.
-
ByteBufferInput
public ByteBufferInput(byte[] bytes, int offset, int count)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.- See Also:
setBuffer(byte[], int, int)
-
ByteBufferInput
public ByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
ByteBufferInput
public ByteBufferInput(InputStream inputStream)
- See Also:
Input(InputStream)
-
ByteBufferInput
public ByteBufferInput(InputStream inputStream, int bufferSize)
- See Also:
Input(InputStream, int)
-
-
Method Detail
-
getBuffer
public byte[] getBuffer()
Deprecated.ThrowsUnsupportedOperationException
because this input uses a ByteBuffer, not a byte[].- Overrides:
getBuffer
in classInput
- See Also:
getByteBuffer()
-
setBuffer
public void setBuffer(byte[] bytes)
Deprecated.ThrowsUnsupportedOperationException
because this input uses a ByteBuffer, not a byte[].- Overrides:
setBuffer
in classInput
- See Also:
setBuffer(ByteBuffer)
-
setBuffer
public void setBuffer(byte[] bytes, int offset, int count)
Deprecated.ThrowsUnsupportedOperationException
because this input uses a ByteBuffer, not a byte[].- Overrides:
setBuffer
in classInput
- See Also:
setBuffer(ByteBuffer)
-
setBuffer
public void setBuffer(ByteBuffer buffer)
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, limit, and capacity are set to match the specified buffer. The total is reset. TheInputStream
is set to null.
-
getByteBuffer
public ByteBuffer getByteBuffer()
-
setInputStream
public void setInputStream(InputStream inputStream)
Description copied from class:Input
Sets an InputStream to read from when data in the buffer is exhausted. The position, limit, and total are reset, discarding any buffered bytes.- Overrides:
setInputStream
in classInput
- Parameters:
inputStream
- May be null.
-
reset
public void reset()
Description copied from class:Input
Sets the position and total to zero.- Specified by:
reset
in interfacePool.Poolable
- Overrides:
reset
in classInput
-
fill
protected int fill(ByteBuffer buffer, int offset, int count) throws KryoException
Fills the buffer with more bytes. May leave the buffer position changed. The default implementation reads from theInputStream
, if set. Can be overridden to fill the bytes from another source.- Throws:
KryoException
-
require
protected int require(int required) throws KryoException
Description copied from class:Input
Fills the buffer with at least the number of bytes specified.- Overrides:
require
in classInput
- Parameters:
required
- Must be > 0.- Returns:
- The number of bytes remaining in the buffer, which will be at least
required
bytes. - Throws:
KryoException
- ifInput.fill(byte[], int, int)
is unable to provide more bytes (buffer underflow).
-
optional
protected int optional(int optional) throws KryoException
Fills the buffer with at least the number of bytes specified, if possible.- Overrides:
optional
in classInput
- Parameters:
optional
- Must be > 0.- Returns:
- the number of bytes remaining, but not more than optional, or -1 if
fill(ByteBuffer, int, int)
is unable to provide more bytes. - Throws:
KryoException
-
read
public int read() throws KryoException
Description copied from class:Input
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
Description copied from class:Input
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
Description copied from class:Input
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 where the next byte will be read.- Overrides:
setPosition
in classInput
-
setLimit
public void setLimit(int limit)
Description copied from class:Input
Sets the limit in the buffer which marks the end of the data that can be read.
-
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
Description copied from class:Input
Discards the specified number of bytes.- Overrides:
skip
in classInput
- Throws:
KryoException
-
close
public void close() throws KryoException
Description copied from class:Input
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
Description copied from class:Input
Reads a single byte.- Overrides:
readByte
in classInput
- Throws:
KryoException
-
readByteUnsigned
public int readByteUnsigned() throws KryoException
Description copied from class:Input
Reads a byte as an int from 0 to 255.- Overrides:
readByteUnsigned
in classInput
- Throws:
KryoException
-
readBytes
public byte[] readBytes(int length) throws KryoException
Description copied from class:Input
Reads the specified number of bytes into a new byte[].- Overrides:
readBytes
in classInput
- Throws:
KryoException
-
readBytes
public void readBytes(byte[] bytes, int offset, int count) throws KryoException
Description copied from class:Input
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
-
readVarInt
public int readVarInt(boolean optimizePositive) throws KryoException
Description copied from class:Input
Reads a 1-5 byte int.- Overrides:
readVarInt
in classInput
- Throws:
KryoException
- See Also:
Input.canReadVarInt()
-
canReadVarInt
public boolean canReadVarInt() throws KryoException
Description copied from class:Input
Returns true if enough bytes are available to read an int withInput.readVarInt(boolean)
.- Overrides:
canReadVarInt
in classInput
- 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.- Overrides:
readVarIntFlag
in classInput
-
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()
.- Overrides:
readVarIntFlag
in classInput
-
readLong
public long readLong() throws KryoException
Description copied from class:Input
Reads an 8 byte long.- Overrides:
readLong
in classInput
- Throws:
KryoException
-
readVarLong
public long readVarLong(boolean optimizePositive) throws KryoException
Description copied from class:Input
Reads a 1-9 byte long.- Overrides:
readVarLong
in classInput
- Throws:
KryoException
- See Also:
Input.canReadLong()
-
canReadVarLong
public boolean canReadVarLong() throws KryoException
Description copied from class:Input
Returns true if enough bytes are available to read a long withInput.readVarLong(boolean)
.- Overrides:
canReadVarLong
in classInput
- Throws:
KryoException
-
readFloat
public float readFloat() throws KryoException
Description copied from class:Input
Reads a 4 byte float.- Overrides:
readFloat
in classInput
- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoException
Description copied from class:Input
Reads an 8 byte double.- Overrides:
readDouble
in classInput
- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoException
Description copied from class:Input
Reads a 1 byte boolean.- Overrides:
readBoolean
in classInput
- Throws:
KryoException
-
readShort
public short readShort() throws KryoException
Description copied from class:Input
Reads a 2 byte short.- Overrides:
readShort
in classInput
- Throws:
KryoException
-
readShortUnsigned
public int readShortUnsigned() throws KryoException
Description copied from class:Input
Reads a 2 byte short as an int from 0 to 65535.- Overrides:
readShortUnsigned
in classInput
- Throws:
KryoException
-
readChar
public char readChar() throws KryoException
Description copied from class:Input
Reads a 2 byte char.- Overrides:
readChar
in classInput
- Throws:
KryoException
-
readString
public String readString()
Description copied from class:Input
Reads the length and string of UTF8 characters, or null. This can read strings written byOutput.writeString(String)
andOutput.writeAscii(String)
.- Overrides:
readString
in classInput
- Returns:
- May be null.
-
readStringBuilder
public StringBuilder readStringBuilder()
Description copied from class:Input
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)
.- Overrides:
readStringBuilder
in classInput
- Returns:
- May be null.
-
readInts
public int[] readInts(int length) throws KryoException
Description copied from class:Input
Reads an int array in bulk. This may be more efficient than reading them individually.- Overrides:
readInts
in classInput
- Throws:
KryoException
-
readLongs
public long[] readLongs(int length) throws KryoException
Description copied from class:Input
Reads a long array in bulk. This may be more efficient than reading them individually.- Overrides:
readLongs
in classInput
- Throws:
KryoException
-
readFloats
public float[] readFloats(int length) throws KryoException
Description copied from class:Input
Reads a float array in bulk. This may be more efficient than reading them individually.- Overrides:
readFloats
in classInput
- Throws:
KryoException
-
readDoubles
public double[] readDoubles(int length) throws KryoException
Description copied from class:Input
Reads a double array in bulk. This may be more efficient than reading them individually.- Overrides:
readDoubles
in classInput
- Throws:
KryoException
-
readShorts
public short[] readShorts(int length) throws KryoException
Description copied from class:Input
Reads a short array in bulk. This may be more efficient than reading them individually.- Overrides:
readShorts
in classInput
- Throws:
KryoException
-
readChars
public char[] readChars(int length) throws KryoException
Description copied from class:Input
Reads a char array in bulk. This may be more efficient than reading them individually.- Overrides:
readChars
in classInput
- Throws:
KryoException
-
readBooleans
public boolean[] readBooleans(int length) throws KryoException
Description copied from class:Input
Reads a boolean array in bulk. This may be more efficient than reading them individually.- Overrides:
readBooleans
in classInput
- Throws:
KryoException
-
-