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, writeLongsprotected 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 Outputpublic void setOutputStream(OutputStream outputStream)
setOutputStream in class OutputoutputStream - 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 Outputprotected boolean require(int required)
throws KryoException
require in class OutputKryoExceptionpublic void flush()
throws KryoException
flush in interface Flushableflush in class OutputKryoExceptionpublic void close()
throws KryoException
close in interface Closeableclose in interface AutoCloseableclose in class OutputKryoExceptionpublic void write(int value)
throws KryoException
write in class OutputKryoExceptionpublic void write(byte[] bytes)
throws KryoException
write in class OutputKryoExceptionpublic void write(byte[] bytes,
int offset,
int length)
throws KryoException
write in class OutputKryoExceptionpublic void writeByte(byte value)
throws KryoException
writeByte in class OutputKryoExceptionpublic void writeByte(int value)
throws KryoException
writeByte in class OutputKryoExceptionpublic void writeBytes(byte[] bytes)
throws KryoException
writeBytes in class OutputKryoExceptionpublic void writeBytes(byte[] bytes,
int offset,
int count)
throws KryoException
writeBytes in class OutputKryoExceptionpublic void writeInt(int value)
throws KryoException
writeInt in class OutputKryoExceptionpublic int writeInt(int value,
boolean optimizePositive)
throws KryoException
OutputwriteInt in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoExceptionpublic int writeVarInt(int val,
boolean optimizePositive)
throws KryoException
OutputwriteVarInt in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoExceptionpublic 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 Outputvalue - May be null.KryoExceptionpublic void writeString(CharSequence value) throws KryoException
Input.readString() or
Input.readStringBuilder().writeString in class Outputvalue - May be null.KryoExceptionpublic void writeAscii(String value) throws KryoException
writeString(String). The string can be read using Input.readString() or
Input.readStringBuilder().writeAscii in class Outputvalue - May be null.KryoExceptionpublic void writeFloat(float value)
throws KryoException
writeFloat in class OutputKryoExceptionpublic int writeFloat(float value,
float precision,
boolean optimizePositive)
throws KryoException
writeFloat in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoExceptionpublic void writeShort(int value)
throws KryoException
writeShort in class OutputKryoExceptionpublic void writeLong(long value)
throws KryoException
writeLong in class OutputKryoExceptionpublic int writeLong(long value,
boolean optimizePositive)
throws KryoException
OutputwriteLong in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoExceptionpublic int writeVarLong(long value,
boolean optimizePositive)
throws KryoException
OutputwriteVarLong in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoExceptionpublic 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).KryoExceptionpublic void writeBoolean(boolean value)
throws KryoException
writeBoolean in class OutputKryoExceptionpublic void writeChar(char value)
throws KryoException
writeChar in class OutputKryoExceptionpublic void writeDouble(double value)
throws KryoException
writeDouble in class OutputKryoExceptionpublic int writeDouble(double value,
double precision,
boolean optimizePositive)
throws KryoException
writeDouble in class OutputoptimizePositive - If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (9 bytes).KryoExceptionpublic void writeInts(int[] object)
throws KryoException
writeInts in class OutputKryoExceptionpublic void writeLongs(long[] object)
throws KryoException
writeLongs in class OutputKryoExceptionpublic void writeFloats(float[] object)
throws KryoException
writeFloats in class OutputKryoExceptionpublic void writeShorts(short[] object)
throws KryoException
writeShorts in class OutputKryoExceptionpublic void writeChars(char[] object)
throws KryoException
writeChars in class OutputKryoExceptionpublic void writeDoubles(double[] object)
throws KryoException
writeDoubles in class OutputKryoExceptionpublic boolean getVarIntsEnabled()
public void setVarIntsEnabled(boolean varIntsEnabled)
varIntsEnabled - Copyright © 2023. All rights reserved.