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:
Pool.Poolable,Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
UnsafeByteBufferOutput
public class ByteBufferOutput extends Output
AnOutputthat uses a ByteBuffer rather than a byte[].Note that the byte[]
bufferis not used. Code taking an Output and expecting the byte[] to be used may not work correctly.- Author:
- Roman Levenstein
, Nathan Sweet
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteBufferbyteBuffer-
Fields inherited from class com.esotericsoftware.kryo.io.Output
buffer, capacity, maxCapacity, outputStream, position, total, varEncoding
-
-
Constructor Summary
Constructors Constructor Description ByteBufferOutput()Creates an uninitialized Output,setBuffer(ByteBuffer)must be called before the Output is used.ByteBufferOutput(int bufferSize)Creates a new Output for writing to a directByteBuffer.ByteBufferOutput(int bufferSize, int maxBufferSize)Creates a new Output for writing to a direct ByteBuffer.ByteBufferOutput(OutputStream outputStream)ByteBufferOutput(OutputStream outputStream, int bufferSize)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.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated 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()Deprecated.ByteBuffergetByteBuffer()Returns the buffer.OutputStreamgetOutputStream()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)Deprecated.voidsetBuffer(byte[] buffer, int maxBufferSize)Deprecated.voidsetBuffer(byte[] bytes, int offset, int count)Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.voidsetBuffer(ByteBuffer buffer)Sets a new buffer to write to.voidsetBuffer(ByteBuffer buffer, int maxBufferSize)Sets a new buffer to write to.voidsetPosition(int position)Sets the current position in the buffer.byte[]toBytes()Allocates and returns a new byte[] containing the bytes currently in the buffer between 0 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(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.voidwriteInt(int bytes, int count)Writes count bytes from long, the last byte written is the lowest byte from the long.voidwriteInts(int[] array, int offset, int count)Writes an int array in bulk.voidwriteLong(long value)Writes an 8 byte long.voidwriteLongs(long[] array, int offset, int count)Writes a long array in bulk.voidwriteShort(int value)Writes a 2 byte short.voidwriteShorts(short[] array, int offset, int count)Writes a short array in bulk.voidwriteString(String value)Writes the length and string, or null.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.-
Methods inherited from class com.esotericsoftware.kryo.io.Output
getMaxCapacity, getVariableLengthEncoding, intLength, longLength, position, setOutputStream, setVariableLengthEncoding, total, varIntLength, varLongLength, writeInt, writeInts, writeLong, writeLong, writeLongs, writeVarDouble, writeVarFloat
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
byteBuffer
protected ByteBuffer byteBuffer
-
-
Constructor Detail
-
ByteBufferOutput
public ByteBufferOutput()
Creates an uninitialized Output,setBuffer(ByteBuffer)must be called before the Output is used.
-
ByteBufferOutput
public ByteBufferOutput(int bufferSize)
Creates a new Output for writing to a directByteBuffer.- Parameters:
bufferSize- The size of the buffer. An exception is thrown if more bytes than this are written andflush()does not empty the buffer.
-
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- 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.
-
ByteBufferOutput
public ByteBufferOutput(ByteBuffer buffer)
Creates a new Output for writing to a ByteBuffer.
-
ByteBufferOutput
public ByteBufferOutput(ByteBuffer buffer, int maxBufferSize)
Creates a new Output for writing to a ByteBuffer.- 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.
-
ByteBufferOutput
public ByteBufferOutput(OutputStream outputStream)
- See Also:
Output(OutputStream)
-
ByteBufferOutput
public ByteBufferOutput(OutputStream outputStream, int bufferSize)
- See Also:
Output(OutputStream, int)
-
-
Method Detail
-
getOutputStream
public OutputStream getOutputStream()
- Overrides:
getOutputStreamin classOutput
-
getBuffer
public byte[] getBuffer()
Deprecated.ThrowsUnsupportedOperationExceptionbecause this output uses a ByteBuffer, not a byte[].- Overrides:
getBufferin classOutput- See Also:
getByteBuffer()
-
setBuffer
public void setBuffer(byte[] buffer)
Deprecated.ThrowsUnsupportedOperationExceptionbecause this output uses a ByteBuffer, not a byte[].- Overrides:
setBufferin classOutput- See Also:
getByteBuffer()
-
setBuffer
public void setBuffer(byte[] buffer, int maxBufferSize)Deprecated.ThrowsUnsupportedOperationExceptionbecause this output uses a ByteBuffer, not a byte[].- Overrides:
setBufferin classOutputmaxBufferSize- IfOutput.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.- See Also:
getByteBuffer()
-
setBuffer
public void setBuffer(byte[] bytes, int offset, int count)Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.- See Also:
setBuffer(ByteBuffer)
-
setBuffer
public void setBuffer(ByteBuffer buffer)
Sets a new buffer to write to. The max size is the buffer's length.- See Also:
setBuffer(ByteBuffer, int)
-
setBuffer
public void setBuffer(ByteBuffer 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 capacity are set to match the specified buffer. The total is 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.
-
getByteBuffer
public ByteBuffer getByteBuffer()
Returns the buffer. The bytes between zero andOutput.position()are the data that has been written.
-
toBytes
public byte[] toBytes()
Description copied from class:OutputAllocates and returns a new byte[] containing the bytes currently in the buffer between 0 andOutput.position().
-
setPosition
public void setPosition(int position)
Description copied from class:OutputSets the current position in the buffer.- Overrides:
setPositionin classOutput
-
reset
public void reset()
Description copied from class:OutputSets the position and total to 0.- Specified by:
resetin interfacePool.Poolable- Overrides:
resetin classOutput
-
require
protected boolean require(int required) throws KryoExceptionDescription copied from class:OutputEnsures the buffer is large enough to read the specified number of bytes.- Overrides:
requirein classOutput- Returns:
- true if the buffer has been resized.
- Throws:
KryoException
-
flush
public void flush() throws KryoExceptionDescription copied from class:OutputFlushes 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 interfaceFlushable- Overrides:
flushin classOutput- Throws:
KryoException
-
close
public void close() throws KryoExceptionDescription copied from class:OutputFlushes any buffered bytes and closes the underlying OutputStream, if any.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutput- Throws:
KryoException
-
write
public void write(int value) throws KryoExceptionDescription copied from class:OutputWrites a byte.- Overrides:
writein classOutput- Throws:
KryoException
-
write
public void write(byte[] bytes) throws KryoExceptionDescription copied from class:OutputWrites the bytes. Note the number of bytes is not written.- Overrides:
writein classOutput- Throws:
KryoException
-
write
public void write(byte[] bytes, int offset, int length) throws KryoExceptionDescription copied from class:OutputWrites the bytes. Note the number of bytes 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 KryoExceptionDescription copied from class:OutputWrites the bytes. Note the number of bytes is not written.- Overrides:
writeBytesin classOutput- Throws:
KryoException
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites the bytes. Note the number of bytes is not written.- Overrides:
writeBytesin classOutput- Throws:
KryoException
-
writeInt
public void writeInt(int bytes, int count)Description copied from class:OutputWrites count bytes from long, the last byte written is the lowest byte from the long. Note the number of bytes is not written.
-
writeInt
public void writeInt(int value) throws KryoExceptionDescription copied from class:OutputWrites a 4 byte int.- Overrides:
writeIntin classOutput- Throws:
KryoException
-
writeVarInt
public int writeVarInt(int value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-5 byte int.- Overrides:
writeVarIntin classOutputoptimizePositive- 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:
Output.varIntLength(int, boolean)
-
writeVarIntFlag
public int writeVarIntFlag(boolean flag, int value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-5 byte int, encoding the boolean value with a bit flag.- Overrides:
writeVarIntFlagin classOutputoptimizePositive- 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
-
writeLong
public void writeLong(long value) throws KryoExceptionDescription copied from class:OutputWrites an 8 byte long.- Overrides:
writeLongin classOutput- Throws:
KryoException
-
writeVarLong
public int writeVarLong(long value, boolean optimizePositive) throws KryoExceptionDescription copied from class:OutputWrites a 1-9 byte long.- Overrides:
writeVarLongin classOutputoptimizePositive- 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:
Output.varLongLength(long, boolean)
-
writeFloat
public void writeFloat(float value) throws KryoExceptionDescription copied from class:OutputWrites a 4 byte float.- Overrides:
writeFloatin classOutput- Throws:
KryoException
-
writeDouble
public void writeDouble(double value) throws KryoExceptionDescription copied from class:OutputWrites an 8 byte double.- Overrides:
writeDoublein classOutput- Throws:
KryoException
-
writeShort
public void writeShort(int value) throws KryoExceptionDescription copied from class:OutputWrites a 2 byte short.- Overrides:
writeShortin classOutput- Throws:
KryoException
-
writeChar
public void writeChar(char value) throws KryoExceptionDescription copied from class:OutputWrites a 2 byte char.- Overrides:
writeCharin classOutput- Throws:
KryoException
-
writeBoolean
public void writeBoolean(boolean value) throws KryoExceptionDescription copied from class:OutputWrites a 1 byte boolean.- Overrides:
writeBooleanin classOutput- Throws:
KryoException
-
writeString
public void writeString(String value) throws KryoException
Description copied from class:OutputWrites 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,Output.writeAscii(String)may be used. The string can be read usingInput.readString()orInput.readStringBuilder().- Overrides:
writeStringin classOutput- Parameters:
value- May be null.- Throws:
KryoException
-
writeAscii
public void writeAscii(String value) throws KryoException
Description copied from class:OutputWrites 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 thanOutput.writeString(String). The string can be read usingInput.readString()orInput.readStringBuilder().- Overrides:
writeAsciiin classOutput- Parameters:
value- May be null.- Throws:
KryoException
-
writeInts
public void writeInts(int[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites an int array in bulk. This may be more efficient than writing them individually.- Overrides:
writeIntsin classOutput- Throws:
KryoException
-
writeLongs
public void writeLongs(long[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a long array in bulk. This may be more efficient than writing them individually.- Overrides:
writeLongsin classOutput- Throws:
KryoException
-
writeFloats
public void writeFloats(float[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a float array in bulk. This may be more efficient than writing them individually.- Overrides:
writeFloatsin classOutput- Throws:
KryoException
-
writeDoubles
public void writeDoubles(double[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a double array in bulk. This may be more efficient than writing them individually.- Overrides:
writeDoublesin classOutput- Throws:
KryoException
-
writeShorts
public void writeShorts(short[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a short array in bulk. This may be more efficient than writing them individually.- Overrides:
writeShortsin classOutput- Throws:
KryoException
-
writeChars
public void writeChars(char[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a char array in bulk. This may be more efficient than writing them individually.- Overrides:
writeCharsin classOutput- Throws:
KryoException
-
writeBooleans
public void writeBooleans(boolean[] array, int offset, int count) throws KryoExceptionDescription copied from class:OutputWrites a boolean array in bulk. This may be more efficient than writing them individually.- Overrides:
writeBooleansin classOutput- Throws:
KryoException
-
-