Package com.esotericsoftware.kryo.unsafe
Class UnsafeByteBufferOutput
- java.lang.Object
-
- java.io.OutputStream
-
- com.esotericsoftware.kryo.io.Output
-
- com.esotericsoftware.kryo.io.ByteBufferOutput
-
- com.esotericsoftware.kryo.unsafe.UnsafeByteBufferOutput
-
- All Implemented Interfaces:
Pool.Poolable
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class UnsafeByteBufferOutput extends ByteBufferOutput
AByteBufferOutput
that writes data to a 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
Output.setVariableLengthEncoding(boolean)
is false.- Author:
- Roman Levenstein
, Nathan Sweet
-
-
Field Summary
-
Fields inherited from class com.esotericsoftware.kryo.io.ByteBufferOutput
byteBuffer
-
Fields inherited from class com.esotericsoftware.kryo.io.Output
buffer, capacity, maxCapacity, outputStream, position, total, varEncoding
-
-
Constructor Summary
Constructors Constructor Description UnsafeByteBufferOutput()
Creates an uninitialized Output,ByteBufferOutput.setBuffer(ByteBuffer)
must be called before the Output is used.UnsafeByteBufferOutput(int bufferSize)
Creates a new Output for writing to a directByteBuffer
.UnsafeByteBufferOutput(int bufferSize, int maxBufferSize)
Creates a new Output for writing to a direct ByteBuffer.UnsafeByteBufferOutput(long address, int size)
Creates a new Output for writing to a ByteBuffer representing the memory region at the specified address and size.UnsafeByteBufferOutput(java.io.OutputStream outputStream)
Creates a new Output for writing to an OutputStream.UnsafeByteBufferOutput(java.io.OutputStream outputStream, int bufferSize)
Creates a new Output for writing to an OutputStream with the specified buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
dispose()
Releases the byte buffer immediately, rather than waiting for GC.protected boolean
require(int required)
Ensures the buffer is large enough to read the specified number of bytes.void
setBuffer(java.nio.ByteBuffer buffer, int maxBufferSize)
Sets a new buffer to write to.void
write(int value)
Writes a byte.void
writeBoolean(boolean value)
Writes a 1 byte boolean.void
writeBooleans(boolean[] array, int offset, int count)
Writes a boolean array in bulk.void
writeByte(byte value)
void
writeByte(int value)
void
writeBytes(byte[] array, int offset, int count)
Writes the bytes.void
writeBytes(java.lang.Object from, long offset, int count)
Write count bytes to the byte buffer, reading from the given offset inside the in-memory representation of the object.void
writeChar(char value)
Writes a 2 byte char.void
writeChars(char[] array, int offset, int count)
Writes a char array in bulk.void
writeDouble(double value)
Writes an 8 byte double.void
writeDoubles(double[] array, int offset, int count)
Writes a double array in bulk.void
writeFloat(float value)
Writes a 4 byte float.void
writeFloats(float[] array, int offset, int count)
Writes a float array in bulk.void
writeInt(int value)
Writes a 4 byte int.void
writeInts(int[] array, int offset, int count)
Writes an int array in bulk.void
writeLong(long value)
Writes an 8 byte long.void
writeLongs(long[] array, int offset, int count)
Writes a long array in bulk.void
writeShort(int value)
Writes a 2 byte short.void
writeShorts(short[] array, int offset, int count)
Writes a short array in bulk.-
Methods inherited from class com.esotericsoftware.kryo.io.ByteBufferOutput
close, flush, getBuffer, getByteBuffer, getOutputStream, reset, setBuffer, setBuffer, setBuffer, setBuffer, setPosition, toBytes, write, write, writeAscii, writeBytes, writeString, writeVarInt, writeVarIntFlag, writeVarLong
-
Methods inherited from class com.esotericsoftware.kryo.io.Output
getMaxCapacity, getVariableLengthEncoding, intLength, longLength, position, setOutputStream, setVariableLengthEncoding, total, varIntLength, varLongLength, writeInt, writeInts, writeLong, writeLongs, writeVarDouble, writeVarFloat
-
-
-
-
Constructor Detail
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput()
Creates an uninitialized Output,ByteBufferOutput.setBuffer(ByteBuffer)
must be called before the Output is used.
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput(int bufferSize)
Creates a new Output for writing to a directByteBuffer
.- Parameters:
bufferSize
- The size of the buffer. An exception is thrown if more bytes than this are written andByteBufferOutput.flush()
does not empty the buffer.
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput(int bufferSize, int maxBufferSize)
Creates a new Output for writing to a direct ByteBuffer.- Parameters:
bufferSize
- The initial size of the buffer.maxBufferSize
- IfByteBufferOutput.flush()
does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput(java.io.OutputStream outputStream)
Creates a new Output for writing to an OutputStream. A buffer size of 4096 is used.
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput(java.io.OutputStream outputStream, int bufferSize)
Creates a new Output for writing to an OutputStream with the specified buffer size.
-
UnsafeByteBufferOutput
public UnsafeByteBufferOutput(long address, int size)
Creates a new Output for writing to a ByteBuffer representing the memory region at the specified address and size. @throws UnsupportedOperationException if creating a ByteBuffer this way is not available.
-
-
Method Detail
-
setBuffer
public void setBuffer(java.nio.ByteBuffer buffer, int maxBufferSize)
Description copied from class:ByteBufferOutput
Sets a new buffer to write to. The bytes are not copied, the old buffer is discarded and the new buffer used in its place. The position and capacity are set to match the specified buffer. The total is reset. TheOutputStream
is set to null.- Overrides:
setBuffer
in classByteBufferOutput
maxBufferSize
- IfByteBufferOutput.flush()
does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
-
require
protected boolean require(int required) throws KryoException
Description copied from class:Output
Ensures the buffer is large enough to read the specified number of bytes.- Overrides:
require
in classByteBufferOutput
- Returns:
- true if the buffer has been resized.
- Throws:
KryoException
-
dispose
public void dispose()
Releases the byte buffer immediately, rather than waiting for GC. This output can no longer be used until a new byte buffer is set.
-
write
public void write(int value) throws KryoException
Description copied from class:Output
Writes a byte.- Overrides:
write
in classByteBufferOutput
- Throws:
KryoException
-
writeByte
public void writeByte(byte value) throws KryoException
- Overrides:
writeByte
in classByteBufferOutput
- Throws:
KryoException
-
writeByte
public void writeByte(int value) throws KryoException
- Overrides:
writeByte
in classByteBufferOutput
- Throws:
KryoException
-
writeInt
public void writeInt(int value) throws KryoException
Description copied from class:Output
Writes a 4 byte int.- Overrides:
writeInt
in classByteBufferOutput
- Throws:
KryoException
-
writeLong
public void writeLong(long value) throws KryoException
Description copied from class:Output
Writes an 8 byte long.- Overrides:
writeLong
in classByteBufferOutput
- Throws:
KryoException
-
writeFloat
public void writeFloat(float value) throws KryoException
Description copied from class:Output
Writes a 4 byte float.- Overrides:
writeFloat
in classByteBufferOutput
- Throws:
KryoException
-
writeDouble
public void writeDouble(double value) throws KryoException
Description copied from class:Output
Writes an 8 byte double.- Overrides:
writeDouble
in classByteBufferOutput
- Throws:
KryoException
-
writeShort
public void writeShort(int value) throws KryoException
Description copied from class:Output
Writes a 2 byte short.- Overrides:
writeShort
in classByteBufferOutput
- Throws:
KryoException
-
writeChar
public void writeChar(char value) throws KryoException
Description copied from class:Output
Writes a 2 byte char.- Overrides:
writeChar
in classByteBufferOutput
- Throws:
KryoException
-
writeBoolean
public void writeBoolean(boolean value) throws KryoException
Description copied from class:Output
Writes a 1 byte boolean.- Overrides:
writeBoolean
in classByteBufferOutput
- Throws:
KryoException
-
writeInts
public void writeInts(int[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes an int array in bulk. This may be more efficient than writing them individually.- Overrides:
writeInts
in classByteBufferOutput
- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a long array in bulk. This may be more efficient than writing them individually.- Overrides:
writeLongs
in classByteBufferOutput
- Throws:
KryoException
-
writeFloats
public void writeFloats(float[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a float array in bulk. This may be more efficient than writing them individually.- Overrides:
writeFloats
in classByteBufferOutput
- Throws:
KryoException
-
writeDoubles
public void writeDoubles(double[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a double array in bulk. This may be more efficient than writing them individually.- Overrides:
writeDoubles
in classByteBufferOutput
- Throws:
KryoException
-
writeShorts
public void writeShorts(short[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a short array in bulk. This may be more efficient than writing them individually.- Overrides:
writeShorts
in classByteBufferOutput
- Throws:
KryoException
-
writeChars
public void writeChars(char[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a char array in bulk. This may be more efficient than writing them individually.- Overrides:
writeChars
in classByteBufferOutput
- Throws:
KryoException
-
writeBooleans
public void writeBooleans(boolean[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes a boolean array in bulk. This may be more efficient than writing them individually.- Overrides:
writeBooleans
in classByteBufferOutput
- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] array, int offset, int count) throws KryoException
Description copied from class:Output
Writes the bytes. Note the number of bytes is not written.- Overrides:
writeBytes
in classByteBufferOutput
- Throws:
KryoException
-
writeBytes
public void writeBytes(java.lang.Object from, long offset, int count) throws KryoException
Write count bytes to the byte buffer, reading from the given offset inside the in-memory representation of the object.- Throws:
KryoException
-
-