public class ByteBufferOutput extends Output
Modifier and Type | Field and Description |
---|---|
protected static ByteOrder |
nativeOrder |
protected ByteBuffer |
niobuffer |
protected boolean |
varIntsEnabled |
buffer, capacity, maxCapacity, outputStream, position, total
Constructor and Description |
---|
ByteBufferOutput()
Creates an uninitialized Output.
|
ByteBufferOutput(ByteBuffer buffer)
Creates a new Output for writing to a ByteBuffer.
|
ByteBufferOutput(ByteBuffer buffer,
int maxBufferSize)
Creates a new Output for writing to a ByteBuffer.
|
ByteBufferOutput(int bufferSize)
Creates a new Output for writing to a direct ByteBuffer.
|
ByteBufferOutput(int bufferSize,
int maxBufferSize)
Creates a new Output for writing to a direct ByteBuffer.
|
ByteBufferOutput(long address,
int maxBufferSize)
Creates a direct ByteBuffer of a given size at a given address.
|
ByteBufferOutput(OutputStream outputStream)
Creates a new Output for writing to an OutputStream.
|
ByteBufferOutput(OutputStream outputStream,
int bufferSize)
Creates a new Output for writing to an OutputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Sets the position and total to zero.
|
void |
close()
Flushes any buffered bytes and closes the underlying OutputStream, if any.
|
void |
flush()
Writes the buffered bytes to the underlying OutputStream, if any.
|
ByteBuffer |
getByteBuffer()
Returns the buffer.
|
OutputStream |
getOutputStream() |
boolean |
getVarIntsEnabled()
Return current setting for variable length encoding of integers
|
ByteOrder |
order() |
void |
order(ByteOrder byteOrder) |
void |
release()
Release a direct buffer.
|
protected boolean |
require(int required) |
void |
setBuffer(ByteBuffer buffer)
Sets the buffer that will be written to.
|
void |
setBuffer(ByteBuffer buffer,
int maxBufferSize)
Sets the buffer that will be written to.
|
void |
setOutputStream(OutputStream outputStream)
Sets a new OutputStream.
|
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.
|
byte[] |
toBytes()
Returns a new byte array containing the bytes currently in the buffer between zero and
Output.position() . |
void |
write(byte[] bytes)
Writes the bytes.
|
void |
write(byte[] bytes,
int offset,
int length)
Writes the bytes.
|
void |
write(int value)
Writes a byte.
|
void |
writeAscii(String value)
Writes a string that is known to contain only ASCII characters.
|
void |
writeBoolean(boolean value)
Writes a 1 byte boolean.
|
void |
writeByte(byte value) |
void |
writeByte(int value) |
void |
writeBytes(byte[] bytes)
Writes the bytes.
|
void |
writeBytes(byte[] bytes,
int offset,
int count)
Writes the bytes.
|
void |
writeChar(char value)
Writes a 2 byte char.
|
void |
writeChars(char[] object)
Bulk output of a char array.
|
void |
writeDouble(double value)
Writes an 8 byte double.
|
int |
writeDouble(double value,
double precision,
boolean optimizePositive)
Writes a 1-9 byte double with reduced precision.
|
void |
writeDoubles(double[] object)
Bulk output of a double array.
|
void |
writeFloat(float value)
Writes a 4 byte float.
|
int |
writeFloat(float value,
float precision,
boolean optimizePositive)
Writes a 1-5 byte float with reduced precision.
|
void |
writeFloats(float[] object)
Bulk output of a float array.
|
void |
writeInt(int value)
Writes a 4 byte int.
|
int |
writeInt(int value,
boolean optimizePositive)
Writes a 1-5 byte int.
|
void |
writeInts(int[] object)
Bulk output of an int array.
|
void |
writeLong(long value)
Writes an 8 byte long.
|
int |
writeLong(long value,
boolean optimizePositive)
Writes a 1-9 byte long.
|
void |
writeLongs(long[] object)
Bulk output of an long array.
|
int |
writeLongS(long value,
boolean optimizePositive)
Writes a 1-9 byte long.
|
void |
writeShort(int value)
Writes a 2 byte short.
|
void |
writeShorts(short[] object)
Bulk output of a short array.
|
void |
writeString(CharSequence value)
Writes the length and CharSequence as UTF8, or null.
|
void |
writeString(String value)
Writes the length and string, or null.
|
int |
writeVarInt(int val,
boolean optimizePositive)
Writes a 1-5 byte int.
|
int |
writeVarLong(long value,
boolean optimizePositive)
Writes a 1-9 byte long.
|
getBuffer, intLength, longLength, position, setBuffer, setBuffer, total, writeInts, writeLongs
protected ByteBuffer niobuffer
protected boolean varIntsEnabled
protected static final ByteOrder nativeOrder
public ByteBufferOutput()
setBuffer(ByteBuffer, int)
public ByteBufferOutput(int bufferSize)
bufferSize
- The initial and maximum size of the buffer. An exception is thrown if this size is exceeded.public ByteBufferOutput(int bufferSize, int maxBufferSize)
bufferSize
- The initial size of the buffer.maxBufferSize
- The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown.public ByteBufferOutput(OutputStream outputStream)
public ByteBufferOutput(OutputStream outputStream, int bufferSize)
public ByteBufferOutput(ByteBuffer buffer)
public ByteBufferOutput(ByteBuffer buffer, int maxBufferSize)
maxBufferSize
- The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.public ByteBufferOutput(long address, int maxBufferSize)
Typical usage could look like this snippet:
// Explicitly allocate memory long bufAddress = UnsafeUtil.unsafe().allocateMemory(4096); // Create a ByteBufferOutput using the allocated memory region ByteBufferOutput buffer = new ByteBufferOutput(bufAddress, 4096); // Do some operations on this buffer here // Say that ByteBuffer won't be used anymore buffer.release(); // Release the allocated region UnsafeUtil.unsafe().freeMemory(bufAddress);
address
- starting address of a memory region pre-allocated using Unsafe.allocateMemory()maxBufferSize
- public void release()
setBuffer(ByteBuffer, int)
should be called before next write operations can be called.
NOTE: If Cleaner is not accessible due to SecurityManager restrictions, reflection could be used to obtain the "clean"
method and then invoke it.public ByteOrder order()
public void order(ByteOrder byteOrder)
public OutputStream getOutputStream()
getOutputStream
in class Output
public void setOutputStream(OutputStream outputStream)
setOutputStream
in class Output
outputStream
- May be null.public void setBuffer(ByteBuffer buffer)
setBuffer(ByteBuffer, int)
public void setBuffer(ByteBuffer buffer, int maxBufferSize)
OutputStream
is set to null.maxBufferSize
- The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.public ByteBuffer getByteBuffer()
Output.position()
are the data that has been written.public byte[] toBytes()
Output.position()
.public void setPosition(int position)
setPosition
in class Output
protected boolean require(int required) throws KryoException
require
in class Output
KryoException
public void flush() throws KryoException
flush
in interface Flushable
flush
in class Output
KryoException
public void close() throws KryoException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Output
KryoException
public void write(int value) throws KryoException
write
in class Output
KryoException
public void write(byte[] bytes) throws KryoException
write
in class Output
KryoException
public void write(byte[] bytes, int offset, int length) throws KryoException
write
in class Output
KryoException
public void writeByte(byte value) throws KryoException
writeByte
in class Output
KryoException
public void writeByte(int value) throws KryoException
writeByte
in class Output
KryoException
public void writeBytes(byte[] bytes) throws KryoException
writeBytes
in class Output
KryoException
public void writeBytes(byte[] bytes, int offset, int count) throws KryoException
writeBytes
in class Output
KryoException
public void writeInt(int value) throws KryoException
writeInt
in class Output
KryoException
public int writeInt(int value, boolean optimizePositive) throws KryoException
Output
writeInt
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
public int writeVarInt(int val, boolean optimizePositive) throws KryoException
Output
writeVarInt
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
public void writeString(String value) throws KryoException
writeAscii(String)
may be used. The string can be read using
Input.readString()
or Input.readStringBuilder()
.writeString
in class Output
value
- May be null.KryoException
public void writeString(CharSequence value) throws KryoException
Input.readString()
or
Input.readStringBuilder()
.writeString
in class Output
value
- May be null.KryoException
public void writeAscii(String value) throws KryoException
writeString(String)
. The string can be read using Input.readString()
or
Input.readStringBuilder()
.writeAscii
in class Output
value
- May be null.KryoException
public void writeFloat(float value) throws KryoException
writeFloat
in class Output
KryoException
public int writeFloat(float value, float precision, boolean optimizePositive) throws KryoException
writeFloat
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
public void writeShort(int value) throws KryoException
writeShort
in class Output
KryoException
public void writeLong(long value) throws KryoException
writeLong
in class Output
KryoException
public int writeLong(long value, boolean optimizePositive) throws KryoException
Output
writeLong
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoException
public int writeVarLong(long value, boolean optimizePositive) throws KryoException
Output
writeVarLong
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoException
public int writeLongS(long value, boolean optimizePositive) throws KryoException
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoException
public void writeBoolean(boolean value) throws KryoException
writeBoolean
in class Output
KryoException
public void writeChar(char value) throws KryoException
writeChar
in class Output
KryoException
public void writeDouble(double value) throws KryoException
writeDouble
in class Output
KryoException
public int writeDouble(double value, double precision, boolean optimizePositive) throws KryoException
writeDouble
in class Output
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoException
public void writeInts(int[] object) throws KryoException
writeInts
in class Output
KryoException
public void writeLongs(long[] object) throws KryoException
writeLongs
in class Output
KryoException
public void writeFloats(float[] object) throws KryoException
writeFloats
in class Output
KryoException
public void writeShorts(short[] object) throws KryoException
writeShorts
in class Output
KryoException
public void writeChars(char[] object) throws KryoException
writeChars
in class Output
KryoException
public void writeDoubles(double[] object) throws KryoException
writeDoubles
in class Output
KryoException
public boolean getVarIntsEnabled()
public void setVarIntsEnabled(boolean varIntsEnabled)
varIntsEnabled
- Copyright © 2018. All rights reserved.