Package com.esotericsoftware.kryo.unsafe
Class UnsafeByteBufferInput
- java.lang.Object
-
- java.io.InputStream
-
- com.esotericsoftware.kryo.io.Input
-
- com.esotericsoftware.kryo.io.ByteBufferInput
-
- com.esotericsoftware.kryo.unsafe.UnsafeByteBufferInput
-
- All Implemented Interfaces:
Pool.Poolable
,Closeable
,AutoCloseable
public class UnsafeByteBufferInput extends ByteBufferInput
AByteBufferInput
that reads data from direct ByteBuffer (off-heap memory) using sun.misc.Unsafe. Multi-byte primitive types use native byte order, so the native byte order on different computers which read and write the data must be the same.Not available on all JVMs.
Util.unsafe
can be checked before using this class.This class may be much faster when
Input.setVariableLengthEncoding(boolean)
is false.- Author:
- Roman Levenstein
, Nathan Sweet
-
-
Field Summary
-
Fields inherited from class com.esotericsoftware.kryo.io.ByteBufferInput
byteBuffer
-
Fields inherited from class com.esotericsoftware.kryo.io.Input
buffer, capacity, chars, inputStream, limit, position, total, varEncoding
-
-
Constructor Summary
Constructors Constructor Description UnsafeByteBufferInput()
Creates an uninitialized Input,setBuffer(ByteBuffer)
must be called before the Input is used.UnsafeByteBufferInput(byte[] bytes)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.UnsafeByteBufferInput(byte[] bytes, int offset, int count)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.UnsafeByteBufferInput(int bufferSize)
Creates a new Input for reading from a directByteBuffer
.UnsafeByteBufferInput(long address, int size)
Creates a new Input for reading from a ByteBuffer representing the memory region at the specified address and size.UnsafeByteBufferInput(InputStream inputStream)
Creates a new Input for reading from an InputStream with a buffer size of 4096.UnsafeByteBufferInput(InputStream inputStream, int bufferSize)
Creates a new Input for reading from an InputStream with the specified buffer size.UnsafeByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
read()
Reads a single byte as an int from 0 to 255, or -1 if there are 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.void
readBytes(Object to, long offset, int count)
Read count bytes and write them to the object at the given offset inside the in-memory representation of the object.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.void
setBuffer(ByteBuffer buffer)
Sets a new buffer to read from.-
Methods inherited from class com.esotericsoftware.kryo.io.ByteBufferInput
canReadVarInt, canReadVarLong, close, fill, getBuffer, getByteBuffer, optional, read, read, readBytes, readShortUnsigned, readString, readStringBuilder, readVarInt, readVarIntFlag, readVarIntFlag, readVarLong, require, reset, setBuffer, setBuffer, setInputStream, setLimit, setPosition, skip, skip
-
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
-
-
-
-
Constructor Detail
-
UnsafeByteBufferInput
public UnsafeByteBufferInput()
Creates an uninitialized Input,setBuffer(ByteBuffer)
must be called before the Input is used.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(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 andByteBufferInput.fill(ByteBuffer, int, int)
does not supply more bytes.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(byte[] bytes)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(byte[] bytes, int offset, int count)
Creates a new Input for reading from aByteBuffer
which is filled with the specified bytes.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(long address, int size)
Creates a new Input for reading from a ByteBuffer representing the memory region at the specified address and size. @throws UnsupportedOperationException if creating a ByteBuffer this way is not available.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(InputStream inputStream)
Creates a new Input for reading from an InputStream with a buffer size of 4096.
-
UnsafeByteBufferInput
public UnsafeByteBufferInput(InputStream inputStream, int bufferSize)
Creates a new Input for reading from an InputStream with the specified buffer size.
-
-
Method Detail
-
setBuffer
public void setBuffer(ByteBuffer buffer)
Description copied from class:ByteBufferInput
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.- Overrides:
setBuffer
in classByteBufferInput
-
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 classByteBufferInput
- Throws:
KryoException
-
readByte
public byte readByte() throws KryoException
Description copied from class:Input
Reads a single byte.- Overrides:
readByte
in classByteBufferInput
- 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 classByteBufferInput
- Throws:
KryoException
-
readInt
public int readInt() throws KryoException
Description copied from class:Input
Reads a 4 byte int.- Overrides:
readInt
in classByteBufferInput
- Throws:
KryoException
-
readLong
public long readLong() throws KryoException
Description copied from class:Input
Reads an 8 byte long.- Overrides:
readLong
in classByteBufferInput
- Throws:
KryoException
-
readFloat
public float readFloat() throws KryoException
Description copied from class:Input
Reads a 4 byte float.- Overrides:
readFloat
in classByteBufferInput
- Throws:
KryoException
-
readDouble
public double readDouble() throws KryoException
Description copied from class:Input
Reads an 8 byte double.- Overrides:
readDouble
in classByteBufferInput
- Throws:
KryoException
-
readShort
public short readShort() throws KryoException
Description copied from class:Input
Reads a 2 byte short.- Overrides:
readShort
in classByteBufferInput
- Throws:
KryoException
-
readChar
public char readChar() throws KryoException
Description copied from class:Input
Reads a 2 byte char.- Overrides:
readChar
in classByteBufferInput
- Throws:
KryoException
-
readBoolean
public boolean readBoolean() throws KryoException
Description copied from class:Input
Reads a 1 byte boolean.- Overrides:
readBoolean
in classByteBufferInput
- Throws:
KryoException
-
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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- 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 classByteBufferInput
- Throws:
KryoException
-
readBytes
public void readBytes(Object to, long offset, int count) throws KryoException
Read count bytes and write them to the object at the given offset inside the in-memory representation of the object.- Throws:
KryoException
-
-