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,java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
UnsafeByteBufferInput
public class ByteBufferInput extends Input
AnInputthat uses a ByteBuffer rather than a byte[].Note that the byte[]
bufferis 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 java.nio.ByteBufferbyteBuffer-
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 aByteBufferwhich is filled with the specified bytes.ByteBufferInput(byte[] bytes, int offset, int count)Creates a new Input for reading from aByteBufferwhich is filled with the specified bytes.ByteBufferInput(int bufferSize)Creates a new Input for reading from a directByteBuffer.ByteBufferInput(java.io.InputStream inputStream)ByteBufferInput(java.io.InputStream inputStream, int bufferSize)ByteBufferInput(java.nio.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 booleancanReadVarInt()Returns true if enough bytes are available to read an int withInput.readVarInt(boolean).booleancanReadVarLong()Returns true if enough bytes are available to read a long withInput.readVarLong(boolean).voidclose()Closes the underlying InputStream, if any.protected intfill(java.nio.ByteBuffer buffer, int offset, int count)Fills the buffer with more bytes.byte[]getBuffer()Deprecated.java.nio.ByteBuffergetByteBuffer()protected intoptional(int optional)Fills the buffer with at least the number of bytes specified, if possible.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, 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.int[]readInts(int length)Reads an int array in bulk.longreadLong()Reads an 8 byte long.long[]readLongs(int length)Reads a long array in bulk.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.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)Deprecated.voidsetBuffer(byte[] bytes, int offset, int count)Deprecated.voidsetBuffer(java.nio.ByteBuffer buffer)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.voidskip(int count)Discards the specified number of bytes.longskip(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
-
-
-
-
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 aByteBufferwhich is filled with the specified bytes.
-
ByteBufferInput
public ByteBufferInput(byte[] bytes, int offset, int count)Creates a new Input for reading from aByteBufferwhich is filled with the specified bytes.- See Also:
setBuffer(byte[], int, int)
-
ByteBufferInput
public ByteBufferInput(java.nio.ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
ByteBufferInput
public ByteBufferInput(java.io.InputStream inputStream)
- See Also:
Input(InputStream)
-
ByteBufferInput
public ByteBufferInput(java.io.InputStream inputStream, int bufferSize)- See Also:
Input(InputStream, int)
-
-
Method Detail
-
getBuffer
public byte[] getBuffer()
Deprecated.ThrowsUnsupportedOperationExceptionbecause this input uses a ByteBuffer, not a byte[].- Overrides:
getBufferin classInput- See Also:
getByteBuffer()
-
setBuffer
public void setBuffer(byte[] bytes)
Deprecated.ThrowsUnsupportedOperationExceptionbecause this input uses a ByteBuffer, not a byte[].- Overrides:
setBufferin classInput- See Also:
setBuffer(ByteBuffer)
-
setBuffer
public void setBuffer(byte[] bytes, int offset, int count)Deprecated.ThrowsUnsupportedOperationExceptionbecause this input uses a ByteBuffer, not a byte[].- Overrides:
setBufferin classInput- See Also:
setBuffer(ByteBuffer)
-
setBuffer
public void setBuffer(java.nio.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. TheInputStreamis set to null.
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
-
setInputStream
public void setInputStream(java.io.InputStream inputStream)
Description copied from class:InputSets an InputStream to read from when data in the buffer is exhausted. The position, limit, and total are reset, discarding any buffered bytes.- Overrides:
setInputStreamin classInput- Parameters:
inputStream- May be null.
-
reset
public void reset()
Description copied from class:InputSets the position and total to zero.- Specified by:
resetin interfacePool.Poolable- Overrides:
resetin classInput
-
fill
protected int fill(java.nio.ByteBuffer buffer, int offset, int count) throws KryoExceptionFills 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 KryoExceptionDescription copied from class:InputFills the buffer with at least the number of bytes specified.- Overrides:
requirein classInput- Parameters:
required- Must be > 0.- Returns:
- The number of bytes remaining in the buffer, which will be at least
requiredbytes. - Throws:
KryoException- ifInput.fill(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.- Overrides:
optionalin 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 KryoExceptionDescription copied from class:InputReads 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 KryoExceptionDescription copied from class:InputReads 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 KryoExceptionDescription copied from class:InputReads 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 where the next byte will be read.- Overrides:
setPositionin classInput
-
setLimit
public void setLimit(int limit)
Description copied from class:InputSets the limit in the buffer which marks the end of the data that can be read.
-
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 KryoExceptionDescription copied from class:InputDiscards the specified number of bytes.- Overrides:
skipin classInput- Throws:
KryoException
-
close
public void close() throws KryoExceptionDescription copied from class:InputCloses 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 KryoExceptionDescription copied from class:InputReads a single byte.- Overrides:
readBytein classInput- Throws:
KryoException
-
readByteUnsigned
public int readByteUnsigned() throws KryoExceptionDescription copied from class:InputReads a byte as an int from 0 to 255.- Overrides:
readByteUnsignedin classInput- Throws:
KryoException
-
readBytes
public byte[] readBytes(int length) throws KryoExceptionDescription copied from class:InputReads the specified number of bytes into a new byte[].- Overrides:
readBytesin classInput- Throws:
KryoException
-
readBytes
public void readBytes(byte[] bytes, int offset, int count) throws KryoExceptionDescription copied from class:InputReads 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
-
readVarInt
public int readVarInt(boolean optimizePositive) throws KryoExceptionDescription copied from class:InputReads a 1-5 byte int.- Overrides:
readVarIntin classInput- Throws:
KryoException- See Also:
Input.canReadVarInt()
-
canReadVarInt
public boolean canReadVarInt() throws KryoExceptionDescription copied from class:InputReturns true if enough bytes are available to read an int withInput.readVarInt(boolean).- Overrides:
canReadVarIntin 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:
readVarIntFlagin 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:
readVarIntFlagin classInput
-
readLong
public long readLong() throws KryoExceptionDescription copied from class:InputReads an 8 byte long.- Overrides:
readLongin classInput- Throws:
KryoException
-
readVarLong
public long readVarLong(boolean optimizePositive) throws KryoExceptionDescription copied from class:InputReads a 1-9 byte long.- Overrides:
readVarLongin classInput- Throws:
KryoException- See Also:
Input.canReadLong()
-
canReadVarLong
public boolean canReadVarLong() throws KryoExceptionDescription copied from class:InputReturns true if enough bytes are available to read a long withInput.readVarLong(boolean).- Overrides:
canReadVarLongin classInput- Throws:
KryoException
-
readFloat
public float readFloat() throws KryoExceptionDescription copied from class:InputReads a 4 byte float.- Overrides:
readFloatin classInput- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoExceptionDescription copied from class:InputReads an 8 byte double.- Overrides:
readDoublein classInput- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoExceptionDescription copied from class:InputReads a 1 byte boolean.- Overrides:
readBooleanin classInput- Throws:
KryoException
-
readShort
public short readShort() throws KryoExceptionDescription copied from class:InputReads a 2 byte short.- Overrides:
readShortin classInput- Throws:
KryoException
-
readShortUnsigned
public int readShortUnsigned() throws KryoExceptionDescription copied from class:InputReads a 2 byte short as an int from 0 to 65535.- Overrides:
readShortUnsignedin classInput- Throws:
KryoException
-
readChar
public char readChar() throws KryoExceptionDescription copied from class:InputReads a 2 byte char.- Overrides:
readCharin classInput- Throws:
KryoException
-
readString
public java.lang.String readString()
Description copied from class:InputReads the length and string of UTF8 characters, or null. This can read strings written byOutput.writeString(String)andOutput.writeAscii(String).- Overrides:
readStringin classInput- Returns:
- May be null.
-
readStringBuilder
public java.lang.StringBuilder readStringBuilder()
Description copied from class:InputReads 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:
readStringBuilderin classInput- Returns:
- May be null.
-
readInts
public int[] readInts(int length) throws KryoExceptionDescription copied from class:InputReads an int array in bulk. This may be more efficient than reading them individually.- Overrides:
readIntsin classInput- Throws:
KryoException
-
readLongs
public long[] readLongs(int length) throws KryoExceptionDescription copied from class:InputReads a long array in bulk. This may be more efficient than reading them individually.- Overrides:
readLongsin classInput- Throws:
KryoException
-
readFloats
public float[] readFloats(int length) throws KryoExceptionDescription copied from class:InputReads a float array in bulk. This may be more efficient than reading them individually.- Overrides:
readFloatsin classInput- Throws:
KryoException
-
readDoubles
public double[] readDoubles(int length) throws KryoExceptionDescription copied from class:InputReads a double array in bulk. This may be more efficient than reading them individually.- Overrides:
readDoublesin classInput- Throws:
KryoException
-
readShorts
public short[] readShorts(int length) throws KryoExceptionDescription copied from class:InputReads a short array in bulk. This may be more efficient than reading them individually.- Overrides:
readShortsin classInput- Throws:
KryoException
-
readChars
public char[] readChars(int length) throws KryoExceptionDescription copied from class:InputReads a char array in bulk. This may be more efficient than reading them individually.- Overrides:
readCharsin classInput- Throws:
KryoException
-
readBooleans
public boolean[] readBooleans(int length) throws KryoExceptionDescription copied from class:InputReads a boolean array in bulk. This may be more efficient than reading them individually.- Overrides:
readBooleansin classInput- Throws:
KryoException
-
-