public class Output extends java.io.OutputStream implements java.lang.AutoCloseable, Pool.Poolable
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer |
protected int |
capacity |
protected int |
maxCapacity |
protected java.io.OutputStream |
outputStream |
protected int |
position |
protected long |
total |
protected boolean |
varEncoding |
Constructor and Description |
---|
Output()
Creates an uninitialized Output,
setBuffer(byte[], int) must be called before the Output is used. |
Output(byte[] buffer)
Creates a new Output for writing to a byte[].
|
Output(byte[] buffer,
int maxBufferSize)
Creates a new Output for writing to a byte[].
|
Output(int bufferSize)
Creates a new Output for writing to a byte[].
|
Output(int bufferSize,
int maxBufferSize)
Creates a new Output for writing to a byte[].
|
Output(java.io.OutputStream outputStream)
Creates a new Output for writing to an OutputStream.
|
Output(java.io.OutputStream outputStream,
int bufferSize)
Creates a new Output for writing to an OutputStream with the specified buffer size.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Flushes any buffered bytes and closes the underlying OutputStream, if any.
|
void |
flush()
Flushes the buffered bytes.
|
byte[] |
getBuffer()
Returns the buffer.
|
int |
getMaxCapacity()
The maximum buffer size, or -1 for no maximum.
|
java.io.OutputStream |
getOutputStream() |
boolean |
getVariableLengthEncoding() |
int |
intLength(int value,
boolean optimizePositive)
Returns the number of bytes that would be written with
writeInt(int, boolean) . |
int |
longLength(int value,
boolean optimizePositive)
Returns the number of bytes that would be written with
writeLong(long, boolean) . |
int |
position()
Returns the current position in the buffer.
|
protected boolean |
require(int required)
Ensures the buffer is large enough to read the specified number of bytes.
|
void |
reset()
Sets the position and total to 0.
|
void |
setBuffer(byte[] buffer)
Sets a new buffer to write to.
|
void |
setBuffer(byte[] buffer,
int maxBufferSize)
Sets a new buffer to write to.
|
void |
setOutputStream(java.io.OutputStream outputStream)
Sets a new OutputStream to flush data to when the buffer is full.
|
void |
setPosition(int position)
Sets the current position in the buffer.
|
void |
setVariableLengthEncoding(boolean varEncoding)
If false,
writeInt(int, boolean) , writeLong(long, boolean) , writeInts(int[], int, int, boolean) ,
and writeLongs(long[], int, int, boolean) will use fixed length encoding, which may be faster for some data. |
byte[] |
toBytes()
Allocates and returns a new byte[] containing the bytes currently in the buffer between 0 and
position() . |
long |
total()
Returns the total number of bytes written.
|
static int |
varIntLength(int value,
boolean optimizePositive)
Returns the number of bytes that would be written with
writeVarInt(int, boolean) . |
static int |
varLongLength(long value,
boolean optimizePositive)
Returns the number of bytes that would be written with
writeVarLong(long, boolean) . |
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(java.lang.String value)
Writes a string that is known to contain only ASCII characters.
|
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[] 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[] 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.
|
int |
writeInt(int value,
boolean optimizePositive)
Reads an int using fixed or variable length encoding, depending on
setVariableLengthEncoding(boolean) . |
void |
writeInts(int[] array,
int offset,
int count)
Writes an int array in bulk.
|
void |
writeInts(int[] array,
int offset,
int count,
boolean optimizePositive)
Writes an int array in bulk using fixed or variable length encoding, depending on
setVariableLengthEncoding(boolean) . |
void |
writeLong(long value)
Writes an 8 byte long.
|
int |
writeLong(long value,
boolean optimizePositive)
Reads a long using fixed or variable length encoding, depending on
setVariableLengthEncoding(boolean) . |
void |
writeLongs(long[] array,
int offset,
int count)
Writes a long array in bulk.
|
void |
writeLongs(long[] array,
int offset,
int count,
boolean optimizePositive)
Writes a long array in bulk using fixed or variable length encoding, depending on
setVariableLengthEncoding(boolean) . |
void |
writeShort(int value)
Writes a 2 byte short.
|
void |
writeShorts(short[] array,
int offset,
int count)
Writes a short array in bulk.
|
void |
writeString(java.lang.String value)
Writes the length and string, or null.
|
int |
writeVarDouble(double value,
double precision,
boolean optimizePositive)
Writes a 1-9 byte double with reduced precision.
|
int |
writeVarFloat(float value,
float precision,
boolean optimizePositive)
Writes a 1-5 byte float with reduced precision.
|
int |
writeVarInt(int value,
boolean optimizePositive)
Writes a 1-5 byte int.
|
int |
writeVarIntFlag(boolean flag,
int value,
boolean optimizePositive)
Writes a 1-5 byte int, encoding the boolean value with a bit flag.
|
int |
writeVarLong(long value,
boolean optimizePositive)
Writes a 1-9 byte long.
|
protected int maxCapacity
protected long total
protected int position
protected int capacity
protected byte[] buffer
protected java.io.OutputStream outputStream
protected boolean varEncoding
public Output()
setBuffer(byte[], int)
must be called before the Output is used.public Output(int bufferSize)
bufferSize
- The size of the buffer. An exception is thrown if more bytes than this are written and flush()
does not empty the buffer.public Output(int bufferSize, int maxBufferSize)
bufferSize
- The initial size of the buffer.maxBufferSize
- If 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.public Output(byte[] buffer)
setBuffer(byte[])
public Output(byte[] buffer, int maxBufferSize)
setBuffer(byte[], int)
public Output(java.io.OutputStream outputStream)
public Output(java.io.OutputStream outputStream, int bufferSize)
public java.io.OutputStream getOutputStream()
public void setOutputStream(java.io.OutputStream outputStream)
outputStream
- May be null.public void setBuffer(byte[] buffer)
setBuffer(byte[], int)
public void setBuffer(byte[] buffer, int maxBufferSize)
OutputStream
is set to null.maxBufferSize
- If 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.public byte[] getBuffer()
position()
are the data that has been written.public byte[] toBytes()
position()
.public boolean getVariableLengthEncoding()
public void setVariableLengthEncoding(boolean varEncoding)
writeInt(int, boolean)
, writeLong(long, boolean)
, writeInts(int[], int, int, boolean)
,
and writeLongs(long[], int, int, boolean)
will use fixed length encoding, which may be faster for some data.
Default is true.public int position()
public void setPosition(int position)
public long total()
public int getMaxCapacity()
Output(int, int)
public void reset()
reset
in interface Pool.Poolable
protected boolean require(int required) throws KryoException
KryoException
public void flush() throws KryoException
OutputStream
, if any, and sets the position to 0. Can be overridden to flush the bytes somewhere else.flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
KryoException
public void close() throws KryoException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
KryoException
public void write(int value) throws KryoException
write
in class java.io.OutputStream
KryoException
public void write(byte[] bytes) throws KryoException
write
in class java.io.OutputStream
KryoException
public void write(byte[] bytes, int offset, int length) throws KryoException
write
in class java.io.OutputStream
KryoException
public void writeByte(byte value) throws KryoException
KryoException
public void writeByte(int value) throws KryoException
KryoException
public void writeBytes(byte[] bytes) throws KryoException
KryoException
public void writeBytes(byte[] bytes, int offset, int count) throws KryoException
KryoException
public void writeInt(int value) throws KryoException
KryoException
public int writeInt(int value, boolean optimizePositive) throws KryoException
setVariableLengthEncoding(boolean)
. Use
writeVarInt(int, boolean)
explicitly when writing values that should always use variable length encoding (eg values
that appear many times).KryoException
intLength(int, boolean)
public int writeVarInt(int value, boolean optimizePositive) throws KryoException
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
varIntLength(int, boolean)
public int writeVarIntFlag(boolean flag, int value, boolean optimizePositive) throws KryoException
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
public int intLength(int value, boolean optimizePositive)
writeInt(int, boolean)
.public void writeLong(long value) throws KryoException
KryoException
public int writeLong(long value, boolean optimizePositive) throws KryoException
setVariableLengthEncoding(boolean)
. Use
writeVarLong(long, boolean)
explicitly when writing values that should always use variable length encoding (eg
values that appear many times).KryoException
longLength(int, boolean)
public int writeVarLong(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
varLongLength(long, boolean)
public int longLength(int value, boolean optimizePositive)
writeLong(long, boolean)
.public void writeFloat(float value) throws KryoException
KryoException
public int writeVarFloat(float value, float precision, boolean optimizePositive) throws KryoException
optimizePositive
- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
inefficient (5 bytes).KryoException
public void writeDouble(double value) throws KryoException
KryoException
public int writeVarDouble(double value, double precision, 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 writeShort(int value) throws KryoException
KryoException
public void writeChar(char value) throws KryoException
KryoException
public void writeBoolean(boolean value) throws KryoException
KryoException
public void writeString(java.lang.String value) throws KryoException
writeAscii(String)
may be used. The string can be read using
Input.readString()
or Input.readStringBuilder()
.value
- May be null.KryoException
public void writeAscii(java.lang.String value) throws KryoException
writeString(String)
. The string can be read using Input.readString()
or
Input.readStringBuilder()
.value
- May be null.KryoException
public void writeInts(int[] array, int offset, int count) throws KryoException
KryoException
public void writeInts(int[] array, int offset, int count, boolean optimizePositive) throws KryoException
setVariableLengthEncoding(boolean)
. This may be more efficient than writing them individually.KryoException
public void writeLongs(long[] array, int offset, int count) throws KryoException
KryoException
public void writeLongs(long[] array, int offset, int count, boolean optimizePositive) throws KryoException
setVariableLengthEncoding(boolean)
. This may be more efficient than writing them individually.KryoException
public void writeFloats(float[] array, int offset, int count) throws KryoException
KryoException
public void writeDoubles(double[] array, int offset, int count) throws KryoException
KryoException
public void writeShorts(short[] array, int offset, int count) throws KryoException
KryoException
public void writeChars(char[] array, int offset, int count) throws KryoException
KryoException
public void writeBooleans(boolean[] array, int offset, int count) throws KryoException
KryoException
public static int varIntLength(int value, boolean optimizePositive)
writeVarInt(int, boolean)
.public static int varLongLength(long value, boolean optimizePositive)
writeVarLong(long, boolean)
.Copyright © 2019. All Rights Reserved.