Package com.esotericsoftware.kryo.io
Class Output
- java.lang.Object
-
- java.io.OutputStream
-
- com.esotericsoftware.kryo.io.Output
-
- All Implemented Interfaces:
Pool.Poolable,java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
- Direct Known Subclasses:
ByteBufferOutput,OutputChunked,UnsafeOutput
public class Output extends java.io.OutputStream implements java.lang.AutoCloseable, Pool.Poolable
An OutputStream that writes data to a byte[] and optionally flushes to another OutputStream. Utility methods are provided for efficiently writing primitive types and strings using big endian.- Author:
- Nathan Sweet
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufferprotected intcapacityprotected intmaxCapacityprotected java.io.OutputStreamoutputStreamprotected intpositionprotected longtotalprotected booleanvarEncoding
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Flushes any buffered bytes and closes the underlying OutputStream, if any.voidflush()Flushes the buffered bytes.byte[]getBuffer()Returns the buffer.intgetMaxCapacity()The maximum buffer size, or -1 for no maximum.java.io.OutputStreamgetOutputStream()booleangetVariableLengthEncoding()intintLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteInt(int, boolean).intlongLength(int 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)Ensures the buffer is large enough to read the specified number of bytes.voidreset()Sets the position and total to 0.voidsetBuffer(byte[] buffer)Sets a new buffer to write to.voidsetBuffer(byte[] buffer, int maxBufferSize)Sets a new buffer to write to.voidsetOutputStream(java.io.OutputStream outputStream)Sets a new OutputStream to flush data to when the buffer is full.voidsetPosition(int position)Sets the current position in the buffer.voidsetVariableLengthEncoding(boolean varEncoding)If false,writeInt(int, boolean),writeLong(long, boolean),writeInts(int[], int, int, boolean), andwriteLongs(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 andposition().longtotal()Returns the total number of bytes written.static intvarIntLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteVarInt(int, boolean).static intvarLongLength(long value, boolean optimizePositive)Returns the number of bytes that would be written withwriteVarLong(long, boolean).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.voidwriteBoolean(boolean value)Writes a 1 byte boolean.voidwriteBooleans(boolean[] array, int offset, int count)Writes a boolean array in bulk.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[] array, int offset, int count)Writes a char array in bulk.voidwriteDouble(double value)Writes an 8 byte double.voidwriteDoubles(double[] array, int offset, int count)Writes a double array in bulk.voidwriteFloat(float value)Writes a 4 byte float.voidwriteFloats(float[] array, int offset, int count)Writes a float array in bulk.voidwriteInt(int value)Writes a 4 byte int.intwriteInt(int value, boolean optimizePositive)Reads an int using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).voidwriteInts(int[] array, int offset, int count)Writes an int array in bulk.voidwriteInts(int[] array, int offset, int count, boolean optimizePositive)Writes an int array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).voidwriteLong(long value)Writes an 8 byte long.intwriteLong(long value, boolean optimizePositive)Reads a long using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).voidwriteLongs(long[] array, int offset, int count)Writes a long array in bulk.voidwriteLongs(long[] array, int offset, int count, boolean optimizePositive)Writes a long array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean).voidwriteShort(int value)Writes a 2 byte short.voidwriteShorts(short[] array, int offset, int count)Writes a short array in bulk.voidwriteString(java.lang.String value)Writes the length and string, or null.intwriteVarDouble(double value, double precision, boolean optimizePositive)Writes a 1-9 byte double with reduced precision.intwriteVarFloat(float value, float precision, boolean optimizePositive)Writes a 1-5 byte float with reduced precision.intwriteVarInt(int value, boolean optimizePositive)Writes a 1-5 byte int.intwriteVarIntFlag(boolean flag, int value, boolean optimizePositive)Writes a 1-5 byte int, encoding the boolean value with a bit flag.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[].- Parameters:
bufferSize- The size of the buffer. An exception is thrown if more bytes than this are written andflush()does not empty the buffer.
-
Output
public Output(int bufferSize, int maxBufferSize)Creates a new Output for writing to a byte[].- Parameters:
bufferSize- The initial size of the buffer.maxBufferSize- Ifflush()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.
-
Output
public Output(byte[] buffer)
Creates a new Output for writing to a byte[].- See Also:
setBuffer(byte[])
-
Output
public Output(byte[] buffer, int maxBufferSize)Creates a new Output for writing to a byte[].- 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 with the specified buffer size.
-
-
Method Detail
-
getOutputStream
public java.io.OutputStream getOutputStream()
-
setOutputStream
public void setOutputStream(java.io.OutputStream outputStream)
Sets a new OutputStream to flush data to when the buffer is full. The position and total are reset, discarding any buffered bytes.- Parameters:
outputStream- May be null.
-
setBuffer
public void setBuffer(byte[] buffer)
Sets a new buffer to write to. The max size is the buffer's length.- See Also:
setBuffer(byte[], int)
-
setBuffer
public void setBuffer(byte[] buffer, int maxBufferSize)Sets a new buffer to write to. The bytes are not copied, the old buffer is discarded and the new buffer used in its place. The position and total are reset. TheOutputStreamis set to null.- Parameters:
maxBufferSize- Ifflush()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.
-
getBuffer
public byte[] getBuffer()
Returns the buffer. The bytes between 0 andposition()are the data that has been written.
-
toBytes
public byte[] toBytes()
Allocates and returns a new byte[] containing the bytes currently in the buffer between 0 andposition().
-
getVariableLengthEncoding
public boolean getVariableLengthEncoding()
-
setVariableLengthEncoding
public void setVariableLengthEncoding(boolean varEncoding)
If false,writeInt(int, boolean),writeLong(long, boolean),writeInts(int[], int, int, boolean), andwriteLongs(long[], int, int, boolean)will use fixed length encoding, which may be faster for some data. Default is true.
-
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.
-
getMaxCapacity
public int getMaxCapacity()
The maximum buffer size, or -1 for no maximum.- See Also:
Output(int, int)
-
reset
public void reset()
Sets the position and total to 0.- Specified by:
resetin interfacePool.Poolable
-
require
protected boolean require(int required) throws KryoExceptionEnsures the buffer is large enough to read the specified number of bytes.- Returns:
- true if the buffer has been resized.
- Throws:
KryoException
-
flush
public void flush() throws KryoExceptionFlushes the buffered bytes. The default implementation writes the buffered bytes to theOutputStream, if any, and sets the position to 0. Can be overridden to flush the bytes somewhere else.- 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 number of bytes 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 number of bytes 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 number of bytes is not written.- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int count) throws KryoExceptionWrites the bytes. Note the number of bytes is not written.- Throws:
KryoException
-
writeInt
public void writeInt(int value) throws KryoExceptionWrites a 4 byte int.- Throws:
KryoException
-
writeInt
public int writeInt(int value, boolean optimizePositive) throws KryoExceptionReads an int using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). UsewriteVarInt(int, boolean)explicitly when writing values that should always use variable length encoding (eg values that appear many times).- Returns:
- The number of bytes written.
- Throws:
KryoException- See Also:
intLength(int, boolean)
-
writeVarInt
public int writeVarInt(int value, boolean optimizePositive) throws KryoExceptionWrites a 1-5 byte int.- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).- Returns:
- The number of bytes written.
- Throws:
KryoException- See Also:
varIntLength(int, boolean)
-
writeVarIntFlag
public int writeVarIntFlag(boolean flag, int value, boolean optimizePositive) throws KryoExceptionWrites a 1-5 byte int, encoding the boolean value with a bit flag.- Parameters:
optimizePositive- If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5 bytes).- Returns:
- The number of bytes written.
- Throws:
KryoException
-
intLength
public int intLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteInt(int, boolean).
-
writeLong
public void writeLong(long value) throws KryoExceptionWrites an 8 byte long.- Throws:
KryoException
-
writeLong
public int writeLong(long value, boolean optimizePositive) throws KryoExceptionReads a long using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). UsewriteVarLong(long, boolean)explicitly when writing values that should always use variable length encoding (eg values that appear many times).- Returns:
- The number of bytes written.
- Throws:
KryoException- See Also:
longLength(int, boolean)
-
writeVarLong
public int writeVarLong(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).- Returns:
- The number of bytes written.
- Throws:
KryoException- See Also:
varLongLength(long, boolean)
-
longLength
public int longLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteLong(long, boolean).
-
writeFloat
public void writeFloat(float value) throws KryoExceptionWrites a 4 byte float.- Throws:
KryoException
-
writeVarFloat
public int writeVarFloat(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).- Returns:
- The number of bytes written.
- Throws:
KryoException
-
writeDouble
public void writeDouble(double value) throws KryoExceptionWrites an 8 byte double.- Throws:
KryoException
-
writeVarDouble
public int writeVarDouble(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).- Returns:
- The number of bytes written.
- Throws:
KryoException
-
writeShort
public void writeShort(int value) throws KryoExceptionWrites a 2 byte short.- Throws:
KryoException
-
writeChar
public void writeChar(char value) throws KryoExceptionWrites a 2 byte char.- Throws:
KryoException
-
writeBoolean
public void writeBoolean(boolean value) throws KryoExceptionWrites a 1 byte boolean.- 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
-
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
-
writeInts
public void writeInts(int[] array, int offset, int count) throws KryoExceptionWrites an int array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeInts
public void writeInts(int[] array, int offset, int count, boolean optimizePositive) throws KryoExceptionWrites an int array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). This may be more efficient than writing them individually.- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] array, int offset, int count) throws KryoExceptionWrites a long array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] array, int offset, int count, boolean optimizePositive) throws KryoExceptionWrites a long array in bulk using fixed or variable length encoding, depending onsetVariableLengthEncoding(boolean). This may be more efficient than writing them individually.- Throws:
KryoException
-
writeFloats
public void writeFloats(float[] array, int offset, int count) throws KryoExceptionWrites a float array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeDoubles
public void writeDoubles(double[] array, int offset, int count) throws KryoExceptionWrites a double array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeShorts
public void writeShorts(short[] array, int offset, int count) throws KryoExceptionWrites a short array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeChars
public void writeChars(char[] array, int offset, int count) throws KryoExceptionWrites a char array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
writeBooleans
public void writeBooleans(boolean[] array, int offset, int count) throws KryoExceptionWrites a boolean array in bulk. This may be more efficient than writing them individually.- Throws:
KryoException
-
varIntLength
public static int varIntLength(int value, boolean optimizePositive)Returns the number of bytes that would be written withwriteVarInt(int, boolean).
-
varLongLength
public static int varLongLength(long value, boolean optimizePositive)Returns the number of bytes that would be written withwriteVarLong(long, boolean).
-
-