Package com.esotericsoftware.kryo.io
Class Output
- java.lang.Object
-
- java.io.OutputStream
-
- com.esotericsoftware.kryo.io.Output
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
- Direct Known Subclasses:
ByteBufferOutput,OutputChunked
public class Output extends java.io.OutputStreamAn 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. Encoding of integers: BIG_ENDIAN is used for storing fixed native size integer values LITTLE_ENDIAN is used for a variable length encoding of integer values
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufferprotected intcapacityprotected intmaxCapacityprotected java.io.OutputStreamoutputStreamprotected intpositionprotected longtotal
-
Constructor Summary
Constructors Constructor Description Output()Creates an uninitialized Output.Output(byte[] buffer)Creates a new Output for writing to a byte array.Output(byte[] buffer, int maxBufferSize)Creates a new Output for writing to a byte array.Output(int bufferSize)Creates a new Output for writing to a byte array.Output(int bufferSize, int maxBufferSize)Creates a new Output for writing to a byte array.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.
-
Method Summary
All Methods Static 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.byte[]getBuffer()Returns the buffer.java.io.OutputStreamgetOutputStream()static intintLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteInt(int, boolean).static intlongLength(long value, boolean optimizePositive)Returns the number of bytes that would be written withwriteLong(long, boolean).intposition()Returns the current position in the buffer.protected booleanrequire(int required)voidsetBuffer(byte[] buffer)Sets the buffer that will be written to.voidsetBuffer(byte[] 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.byte[]toBytes()Returns a new byte array containing the bytes currently in the buffer between zero andposition().longtotal()Returns the total number of bytes written.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.voidwriteInts(int[] object, boolean optimizePositive)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.voidwriteLongs(long[] object, boolean optimizePositive)Bulk output of an long array.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 value, boolean optimizePositive)Writes a 1-5 byte int.intwriteVarLong(long value, boolean optimizePositive)Writes a 1-9 byte long.
-
-
-
Constructor Detail
-
Output
public Output()
Creates an uninitialized Output.setBuffer(byte[], int)must be called before the Output is used.
-
Output
public Output(int bufferSize)
Creates a new Output for writing to a byte array.- Parameters:
bufferSize- The initial and maximum size of the buffer. An exception is thrown if this size is exceeded.
-
Output
public Output(int bufferSize, int maxBufferSize)Creates a new Output for writing to a byte array.- Parameters:
bufferSize- The initial size of the buffer.maxBufferSize- The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown. Can be -1 for no maximum.
-
Output
public Output(byte[] buffer)
Creates a new Output for writing to a byte array.- See Also:
setBuffer(byte[])
-
Output
public Output(byte[] buffer, int maxBufferSize)Creates a new Output for writing to a byte array.- See Also:
setBuffer(byte[], int)
-
Output
public Output(java.io.OutputStream outputStream)
Creates a new Output for writing to an OutputStream. A buffer size of 4096 is used.
-
Output
public Output(java.io.OutputStream outputStream, int bufferSize)Creates a new Output for writing to an OutputStream.
-
-
Method Detail
-
getOutputStream
public java.io.OutputStream getOutputStream()
-
setOutputStream
public void setOutputStream(java.io.OutputStream outputStream)
Sets a new OutputStream. The position and total are reset, discarding any buffered bytes.- Parameters:
outputStream- May be null.
-
setBuffer
public void setBuffer(byte[] buffer)
Sets the buffer that will be written to.setBuffer(byte[], int)is called with the specified buffer's length as the maxBufferSize.
-
setBuffer
public void setBuffer(byte[] buffer, int maxBufferSize)Sets the buffer that will be written to. The position and total are reset, discarding any buffered bytes. TheOutputStreamis set to null.- Parameters:
maxBufferSize- The buffer is doubled as needed until it exceeds maxBufferSize and an exception is thrown.
-
getBuffer
public byte[] getBuffer()
Returns the buffer. The bytes between zero andposition()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 andposition().
-
position
public int position()
Returns the current position in the buffer. This is the number of bytes that have not been flushed.
-
setPosition
public void setPosition(int position)
Sets the current position in the buffer.
-
total
public long total()
Returns the total number of bytes written. This may include bytes that have not been flushed.
-
clear
public void clear()
Sets the position and total to zero.
-
require
protected boolean require(int required) throws KryoException- 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 classjava.io.OutputStream- 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 classjava.io.OutputStream- Throws:
KryoException
-
write
public void write(int value) throws KryoExceptionWrites a byte.- Specified by:
writein classjava.io.OutputStream- Throws:
KryoException
-
write
public void write(byte[] bytes) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Overrides:
writein classjava.io.OutputStream- 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 classjava.io.OutputStream- Throws:
KryoException
-
writeByte
public void writeByte(byte value) throws KryoException- Throws:
KryoException
-
writeByte
public void writeByte(int value) throws KryoException- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int count) throws KryoExceptionWrites the bytes. Note the byte[] length is not written.- Throws:
KryoException
-
writeInt
public void writeInt(int value) throws KryoExceptionWrites a 4 byte int. Uses BIG_ENDIAN byte order.- Throws:
KryoException
-
writeInt
public int writeInt(int value, boolean optimizePositive) throws KryoExceptionWrites 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.- Parameters:
optimizePositive- 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 value, boolean optimizePositive) throws KryoExceptionWrites a 1-5 byte int. It is guaranteed that a varible length encoding will be used.- Parameters:
optimizePositive- 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().- 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().- 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().- 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.- Throws:
KryoException
-
writeFloat
public int writeFloat(float value, float precision, boolean optimizePositive) throws KryoExceptionWrites a 1-5 byte float with reduced precision.- 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. Uses BIG_ENDIAN byte order.- Throws:
KryoException
-
writeLong
public void writeLong(long value) throws KryoExceptionWrites an 8 byte long. Uses BIG_ENDIAN byte order.- Throws:
KryoException
-
writeLong
public int writeLong(long value, boolean optimizePositive) throws KryoExceptionWrites 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.- Parameters:
optimizePositive- 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 KryoExceptionWrites a 1-9 byte long. It is guaranteed that a varible length encoding will be used.- 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.- Throws:
KryoException
-
writeChar
public void writeChar(char value) throws KryoExceptionWrites a 2 byte char. Uses BIG_ENDIAN byte order.- Throws:
KryoException
-
writeDouble
public void writeDouble(double value) throws KryoExceptionWrites an 8 byte double.- Throws:
KryoException
-
writeDouble
public int writeDouble(double value, double precision, boolean optimizePositive) throws KryoExceptionWrites a 1-9 byte double with reduced precision.- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9 bytes).- Throws:
KryoException
-
intLength
public static int intLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteInt(int, boolean).
-
longLength
public static int longLength(long value, boolean optimizePositive)Returns the number of bytes that would be written withwriteLong(long, boolean).
-
writeInts
public void writeInts(int[] object, boolean optimizePositive) throws KryoExceptionBulk output of an int array.- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] object, boolean optimizePositive) throws KryoExceptionBulk output of an long array.- Throws:
KryoException
-
writeInts
public void writeInts(int[] object) throws KryoExceptionBulk output of an int array.- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] object) throws KryoExceptionBulk output of an long array.- Throws:
KryoException
-
writeFloats
public void writeFloats(float[] object) throws KryoExceptionBulk output of a float array.- Throws:
KryoException
-
writeShorts
public void writeShorts(short[] object) throws KryoExceptionBulk output of a short array.- Throws:
KryoException
-
writeChars
public void writeChars(char[] object) throws KryoExceptionBulk output of a char array.- Throws:
KryoException
-
writeDoubles
public void writeDoubles(double[] object) throws KryoExceptionBulk output of a double array.- Throws:
KryoException
-
-