Package com.esotericsoftware.kryo.io
Class ByteBufferOutput
- java.lang.Object
-
- java.io.OutputStream
-
- com.esotericsoftware.kryo.io.Output
-
- com.esotericsoftware.kryo.io.ByteBufferOutput
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class ByteBufferOutput extends Output
An OutputStream that buffers data in a byte array and optionally flushes to another OutputStream. Utility methods are provided for efficiently writing primitive types and strings.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.nio.ByteOrderbyteOrderprotected static java.nio.ByteOrdernativeOrderprotected java.nio.ByteBufferniobufferprotected booleanvarIntsEnabled-
Fields inherited from class com.esotericsoftware.kryo.io.Output
buffer, capacity, maxCapacity, outputStream, position, total
-
-
Constructor Summary
Constructors Constructor Description ByteBufferOutput()Creates an uninitialized Output.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(java.io.OutputStream outputStream)Creates a new Output for writing to an OutputStream.ByteBufferOutput(java.io.OutputStream outputStream, int bufferSize)Creates a new Output for writing to an OutputStream.ByteBufferOutput(java.nio.ByteBuffer buffer)Creates a new Output for writing to a ByteBuffer.ByteBufferOutput(java.nio.ByteBuffer buffer, int maxBufferSize)Creates a new Output for writing to a ByteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Sets the position and total to zero.voidclose()Flushes any buffered bytes and closes the underlying OutputStream, if any.voidflush()Writes the buffered bytes to the underlying OutputStream, if any.java.nio.ByteBuffergetByteBuffer()Returns the buffer.java.io.OutputStreamgetOutputStream()booleangetVarIntsEnabled()Return current setting for variable length encoding of integersprivate booleanisNativeOrder()java.nio.ByteOrderorder()voidorder(java.nio.ByteOrder byteOrder)protected booleanrequire(int required)voidsetBuffer(java.nio.ByteBuffer buffer)Sets the buffer that will be written to.voidsetBuffer(java.nio.ByteBuffer buffer, int maxBufferSize)Sets the buffer that will be written to.voidsetOutputStream(java.io.OutputStream outputStream)Sets a new OutputStream.voidsetPosition(int position)Sets the current position in the buffer.voidsetVarIntsEnabled(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 andOutput.position().voidwrite(byte[] bytes)Writes the bytes.voidwrite(byte[] bytes, int offset, int length)Writes the bytes.voidwrite(int value)Writes a byte.voidwriteAscii(java.lang.String value)Writes a string that is known to contain only ASCII characters.private voidwriteAscii_slow(java.lang.String value, int charCount)voidwriteBoolean(boolean value)Writes a 1 byte boolean.voidwriteByte(byte value)voidwriteByte(int value)voidwriteBytes(byte[] bytes)Writes the bytes.voidwriteBytes(byte[] bytes, int offset, int count)Writes the bytes.voidwriteChar(char value)Writes a 2 byte char.voidwriteChars(char[] object)Bulk output of a char array.voidwriteDouble(double value)Writes an 8 byte double.intwriteDouble(double value, double precision, boolean optimizePositive)Writes a 1-9 byte double with reduced precision.voidwriteDoubles(double[] object)Bulk output of a double array.voidwriteFloat(float value)Writes a 4 byte float.intwriteFloat(float value, float precision, boolean optimizePositive)Writes a 1-5 byte float with reduced precision.voidwriteFloats(float[] object)Bulk output of a float array.voidwriteInt(int value)Writes a 4 byte int.intwriteInt(int value, boolean optimizePositive)Writes a 1-5 byte int.voidwriteInts(int[] object)Bulk output of an int array.voidwriteLong(long value)Writes an 8 byte long.intwriteLong(long value, boolean optimizePositive)Writes a 1-9 byte long.voidwriteLongs(long[] object)Bulk output of an long array.intwriteLongS(long value, boolean optimizePositive)Writes a 1-9 byte long.voidwriteShort(int value)Writes a 2 byte short.voidwriteShorts(short[] object)Bulk output of a short array.voidwriteString(java.lang.CharSequence value)Writes the length and CharSequence as UTF8, or null.voidwriteString(java.lang.String value)Writes the length and string, or null.private voidwriteString_slow(java.lang.CharSequence value, int charCount, int charIndex)private voidwriteUtf8Length(int value)Writes the length of a string, which is a variable length encoded int except the first byte uses bit 8 to denote UTF8 and bit 7 to denote if another byte is present.intwriteVarInt(int val, boolean optimizePositive)Writes a 1-5 byte int.intwriteVarLong(long value, boolean optimizePositive)Writes a 1-9 byte long.-
Methods inherited from class com.esotericsoftware.kryo.io.Output
getBuffer, intLength, longLength, position, setBuffer, setBuffer, total, writeInts, writeLongs
-
-
-
-
Constructor Detail
-
ByteBufferOutput
public ByteBufferOutput()
Creates an uninitialized Output. A buffer must be set before the Output is used.- See Also:
setBuffer(ByteBuffer, int)
-
ByteBufferOutput
public ByteBufferOutput(int bufferSize)
Creates a new Output for writing to a direct ByteBuffer.- Parameters:
bufferSize- The initial and maximum size of the buffer. An exception is thrown if this size is exceeded.
-
ByteBufferOutput
public ByteBufferOutput(int bufferSize, int maxBufferSize)Creates a new Output for writing to a direct ByteBuffer.- Parameters:
bufferSize- The initial size of the buffer.maxBufferSize- The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown.
-
ByteBufferOutput
public ByteBufferOutput(java.io.OutputStream outputStream)
Creates a new Output for writing to an OutputStream. A buffer size of 4096 is used.
-
ByteBufferOutput
public ByteBufferOutput(java.io.OutputStream outputStream, int bufferSize)Creates a new Output for writing to an OutputStream.
-
ByteBufferOutput
public ByteBufferOutput(java.nio.ByteBuffer buffer)
Creates a new Output for writing to a ByteBuffer.
-
ByteBufferOutput
public ByteBufferOutput(java.nio.ByteBuffer buffer, int maxBufferSize)Creates a new Output for writing to a ByteBuffer.- Parameters:
maxBufferSize- The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.
-
-
Method Detail
-
order
public java.nio.ByteOrder order()
-
order
public void order(java.nio.ByteOrder byteOrder)
-
getOutputStream
public java.io.OutputStream getOutputStream()
- Overrides:
getOutputStreamin classOutput
-
setOutputStream
public void setOutputStream(java.io.OutputStream outputStream)
Sets a new OutputStream. The position and total are reset, discarding any buffered bytes.- Overrides:
setOutputStreamin classOutput- Parameters:
outputStream- May be null.
-
setBuffer
public void setBuffer(java.nio.ByteBuffer buffer)
Sets the buffer that will be written to. maxCapacity is set to the specified buffer's capacity.- See Also:
setBuffer(ByteBuffer, int)
-
setBuffer
public void setBuffer(java.nio.ByteBuffer buffer, int maxBufferSize)Sets the buffer that will be written to. The byte order, position and capacity are set to match the specified buffer. The total is set to 0. TheOutputStreamis set to null.- Parameters:
maxBufferSize- The buffer is doubled as needed until it exceeds maxCapacity and an exception is thrown.
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
Returns the buffer. The bytes between zero andOutput.position()are the data that has been written.
-
toBytes
public byte[] toBytes()
Returns a new byte array containing the bytes currently in the buffer between zero andOutput.position().
-
setPosition
public void setPosition(int position)
Sets the current position in the buffer.- Overrides:
setPositionin classOutput
-
require
protected boolean require(int required) throws KryoException- Overrides:
requirein classOutput- Returns:
- true if the buffer has been resized.
- Throws:
KryoException
-
flush
public void flush() throws KryoExceptionWrites the buffered bytes to the underlying OutputStream, if any.- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classOutput- Throws:
KryoException
-
close
public void close() throws KryoExceptionFlushes any buffered bytes and closes the underlying OutputStream, if any.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classOutput- Throws:
KryoException
-
write
public void write(int value) throws KryoExceptionWrites a byte.- Overrides:
writein classOutput- Throws:
KryoException
-
write
public void write(byte[] bytes) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Overrides:
writein classOutput- Throws:
KryoException
-
write
public void write(byte[] bytes, int offset, int length) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Overrides:
writein classOutput- Throws:
KryoException
-
writeByte
public void writeByte(byte value) throws KryoException- Overrides:
writeBytein classOutput- Throws:
KryoException
-
writeByte
public void writeByte(int value) throws KryoException- Overrides:
writeBytein classOutput- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Overrides:
writeBytesin classOutput- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int count) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Overrides:
writeBytesin classOutput- Throws:
KryoException
-
writeInt
public void writeInt(int value) throws KryoExceptionWrites a 4 byte int.- Overrides:
writeIntin classOutput- Throws:
KryoException
-
writeInt
public int writeInt(int value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-5 byte int. This stream may consider such a variable length encoding request as a hint. It is not guaranteed that a variable length encoding will be really used. The stream may decide to use native-sized integer representation for efficiency reasons.- Overrides:
writeIntin classOutputoptimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).- Throws:
KryoException
-
writeVarInt
public int writeVarInt(int val, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-5 byte int. It is guaranteed that a varible length encoding will be used.- Overrides:
writeVarIntin classOutputoptimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).- Throws:
KryoException
-
writeString
public void writeString(java.lang.String value) throws KryoExceptionWrites the length and string, or null. Short strings are checked and if ASCII they are written more efficiently, else they are written as UTF8. If a string is known to be ASCII,writeAscii(String)may be used. The string can be read usingInput.readString()orInput.readStringBuilder().- Overrides:
writeStringin classOutput- Parameters:
value- May be null.- Throws:
KryoException
-
writeString
public void writeString(java.lang.CharSequence value) throws KryoExceptionWrites the length and CharSequence as UTF8, or null. The string can be read usingInput.readString()orInput.readStringBuilder().- Overrides:
writeStringin classOutput- Parameters:
value- May be null.- Throws:
KryoException
-
writeAscii
public void writeAscii(java.lang.String value) throws KryoExceptionWrites a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted. Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more efficient thanwriteString(String). The string can be read usingInput.readString()orInput.readStringBuilder().- Overrides:
writeAsciiin classOutput- Parameters:
value- May be null.- Throws:
KryoException
-
writeUtf8Length
private void writeUtf8Length(int value)
Writes the length of a string, which is a variable length encoded int except the first byte uses bit 8 to denote UTF8 and bit 7 to denote if another byte is present.
-
writeString_slow
private void writeString_slow(java.lang.CharSequence value, int charCount, int charIndex)
-
writeAscii_slow
private void writeAscii_slow(java.lang.String value, int charCount) throws KryoException- Throws:
KryoException
-
writeFloat
public void writeFloat(float value) throws KryoExceptionWrites a 4 byte float.- Overrides:
writeFloatin classOutput- Throws:
KryoException
-
writeFloat
public int writeFloat(float value, float precision, boolean optimizePositive) throws KryoExceptionWrites a 1-5 byte float with reduced precision.- Overrides:
writeFloatin classOutput- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).- Throws:
KryoException
-
writeShort
public void writeShort(int value) throws KryoExceptionWrites a 2 byte short.- Overrides:
writeShortin classOutput- Throws:
KryoException
-
writeLong
public void writeLong(long value) throws KryoExceptionWrites an 8 byte long.- Overrides:
writeLongin classOutput- Throws:
KryoException
-
writeLong
public int writeLong(long value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-9 byte long. This stream may consider such a variable length encoding request as a hint. It is not guaranteed that a variable length encoding will be really used. The stream may decide to use native-sized integer representation for efficiency reasons.- Overrides:
writeLongin classOutputoptimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).- Throws:
KryoException
-
writeVarLong
public int writeVarLong(long value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-9 byte long. It is guaranteed that a varible length encoding will be used.- Overrides:
writeVarLongin classOutputoptimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).- Throws:
KryoException
-
writeLongS
public int writeLongS(long value, boolean optimizePositive) throws KryoExceptionWrites a 1-9 byte long.- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).- Throws:
KryoException
-
writeBoolean
public void writeBoolean(boolean value) throws KryoExceptionWrites a 1 byte boolean.- Overrides:
writeBooleanin classOutput- Throws:
KryoException
-
writeChar
public void writeChar(char value) throws KryoExceptionWrites a 2 byte char.- Overrides:
writeCharin classOutput- Throws:
KryoException
-
writeDouble
public void writeDouble(double value) throws KryoExceptionWrites an 8 byte double.- Overrides:
writeDoublein classOutput- Throws:
KryoException
-
writeDouble
public int writeDouble(double value, double precision, boolean optimizePositive) throws KryoExceptionWrites a 1-9 byte double with reduced precision.- Overrides:
writeDoublein classOutput- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).- Throws:
KryoException
-
writeInts
public void writeInts(int[] object) throws KryoExceptionBulk output of an int array.- Overrides:
writeIntsin classOutput- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] object) throws KryoExceptionBulk output of an long array.- Overrides:
writeLongsin classOutput- Throws:
KryoException
-
writeFloats
public void writeFloats(float[] object) throws KryoExceptionBulk output of a float array.- Overrides:
writeFloatsin classOutput- Throws:
KryoException
-
writeShorts
public void writeShorts(short[] object) throws KryoExceptionBulk output of a short array.- Overrides:
writeShortsin classOutput- Throws:
KryoException
-
writeChars
public void writeChars(char[] object) throws KryoExceptionBulk output of a char array.- Overrides:
writeCharsin classOutput- Throws:
KryoException
-
writeDoubles
public void writeDoubles(double[] object) throws KryoExceptionBulk output of a double array.- Overrides:
writeDoublesin classOutput- Throws:
KryoException
-
isNativeOrder
private boolean isNativeOrder()
-
getVarIntsEnabled
public boolean getVarIntsEnabled()
Return current setting for variable length encoding of integers- Returns:
- current setting for variable length encoding of integers
-
setVarIntsEnabled
public void setVarIntsEnabled(boolean varIntsEnabled)
Controls if a variable length encoding for integer types should be used when serializers suggest it.- Parameters:
varIntsEnabled-
-
-