Class BufferedStreamOutput
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
OutputStream into a rich StreamOutput for use with Writeable instances, using a buffer.
Similar to OutputStreamStreamOutput in function, but with different performance characteristics because it requires a buffer to
be acquired or allocated up-front. Apart from the costs of the buffer creation & release a BufferedStreamOutput is likely
more performant than an OutputStreamStreamOutput because it writes all fields directly to its local buffer and only copies data
to the underlying stream when the buffer fills up.
Wrapping a ByteArrayOutputStream
AByteArrayOutputStream collects data in an underlying byte[] collector which typically doubles in size each time
it receives a write operation which exhausts the remaining space in the existing collector. This works well if wrapped with a
BufferedStreamOutput especially if the object is expected to be small enough to fit entirely into the buffer, because then
there's only one slightly-oversized byte[] allocation to create the collector, plus another right-sized byte[] allocation
to extract the result, assuming the buffer is not allocated afresh each time. However, subsequent flushes may need to allocate a larger
collector, copying over the existing data to the new collector, so this can perform badly for larger objects. For objects larger than
half the G1 region size (8MiB on a 32GiB heap) each reallocation will require a
humongous allocation which can be stressful for the
garbage collector, so it's better to split the bytes into several smaller pages using utilities such as BytesStreamOutput,
ReleasableBytesStreamOutput or RecyclerBytesStreamOutput in those cases.
A BufferedStreamOutput around a ByteArrayOutputStream is also good if the in-memory serialized representation
will have a long lifetime, because the resulting byte[] is exactly the correct size. When using other approaches such as a
BytesStreamOutput, ReleasableBytesStreamOutput or RecyclerBytesStreamOutput there will be some
amount of unused overhead bytes which may be particularly undesirable for long-lived objects.
An OutputStreamStreamOutput wrapper is almost certainly worse than a BufferedStreamOutput because it will make the
ByteArrayOutputStream perform significantly more allocations and copies until the collecting buffer gets large enough.
Most writes to a OutputStreamStreamOutput use a thread-local intermediate buffer (itself somewhat expensive) and then copy that
intermediate buffer directly to the output.
Any memory allocated in this way is untracked by the org.elasticsearch.common.breaker subsystem unless the caller takes steps to
add this tracking themselves.
-
Field Summary
Fields inherited from class org.elasticsearch.common.io.stream.StreamOutput
GENERIC_LIST_HEADER -
Constructor Summary
ConstructorsConstructorDescriptionBufferedStreamOutput(OutputStream delegate, org.apache.lucene.util.BytesRef buffer) Wrap the given stream, using the givenBytesReffor the buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this stream to further operations.voidflush()Forces any buffered output to be written.longposition()voidwriteByte(byte b) Writes a single byte.voidwriteBytes(byte[] b, int offset, int length) Writes an array of bytes.voidwriteGenericString(String str) Write aStringwithinStreamOutput.writeGenericValue(java.lang.Object), represented as the type code byte0followed by the string itself written as if withStreamOutput.writeString(java.lang.String).voidwriteInt(int i) Writes an int as four bytes.voidwriteIntLE(int i) Writes an int as four bytes, least significant bytes first.voidwriteLong(long i) Writes a long as eight bytes.voidwriteLongLE(long i) Writes a long as eight bytes.voidWrite a possibly-nullString, represented as awhich isinvalid reference
booleanfalseif the string is null, or elsetrueif it is not null, and in this latter case it is followed by the string itself written as if withStreamOutput.writeString(java.lang.String).voidwriteShort(short i) voidwriteString(String str) Write aString: its length in Unicode code units (chars) (NB not bytes) written usingStreamOutput.writeVInt(int)followed by the sequence of characters themselves encoded as UTF-8.voidwriteVInt(int i) Writes an int in a variable-length format.voidwriteVIntArray(int[] values) voidwriteZLong(long i) Writes a long in a variable-length format.Methods inherited from class org.elasticsearch.common.io.stream.StreamOutput
checkWriteable, getTransportVersion, legacyWriteWithSizePrefix, setTransportVersion, write, write, writeArray, writeArray, writeBigInteger, writeBoolean, writeByteArray, writeBytes, writeBytes, writeBytesRef, writeBytesReference, writeCollection, writeCollection, writeDouble, writeDoubleArray, writeDoubleLE, writeEnum, writeEnumSet, writeException, writeFloat, writeFloatArray, writeGenericList, writeGenericMap, writeGenericNull, writeGenericValue, writeGeoPoint, writeInstant, writeIntArray, writeLongArray, writeMap, writeMap, writeMap, writeMapValues, writeMapValues, writeMapWithConsistentOrder, writeMissingString, writeMissingWriteable, writeNamedWriteable, writeNamedWriteableCollection, writeOptional, writeOptionalArray, writeOptionalArray, writeOptionalBoolean, writeOptionalByteArray, writeOptionalBytesReference, writeOptionalCollection, writeOptionalCollection, writeOptionalDouble, writeOptionalEnum, writeOptionalException, writeOptionalFloat, writeOptionalFloatArray, writeOptionalInstant, writeOptionalInt, writeOptionalLong, writeOptionalMap, writeOptionalNamedWriteable, writeOptionalNamedWriteableCollection, writeOptionalSecureString, writeOptionalStringArray, writeOptionalStringCollection, writeOptionalText, writeOptionalTimeValue, writeOptionalVInt, writeOptionalVLong, writeOptionalWriteable, writeOptionalZoneId, writeSecureString, writeStringArray, writeStringArrayNullable, writeStringCollection, writeText, writeTimeValue, writeVLong, writeVLongArray, writeWithSizePrefix, writeWriteable, writeZoneIdMethods inherited from class java.io.OutputStream
nullOutputStream, write
-
Constructor Details
-
BufferedStreamOutput
Wrap the given stream, using the givenBytesReffor the buffer. It is the caller's responsibility to make sure that nothing else modifies the buffer's contents while this object is in use.
-
-
Method Details
-
position
public long position()- Specified by:
positionin classStreamOutput- Returns:
- the current position of the stream, in bytes. Increases as data is written to the stream. If the stream is seekable then
the seek operation updates the current
position().
-
writeByte
Description copied from class:StreamOutputWrites a single byte.- Specified by:
writeBytein classStreamOutput- Throws:
IOException
-
writeBytes
Description copied from class:StreamOutputWrites an array of bytes.- Specified by:
writeBytesin classStreamOutput- Parameters:
b- the bytes to writeoffset- the offset in the byte arraylength- the number of bytes to write- Throws:
IOException
-
flush
Description copied from class:StreamOutputForces any buffered output to be written.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classStreamOutput- Throws:
IOException
-
close
Description copied from class:StreamOutputCloses this stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classStreamOutput- Throws:
IOException
-
writeShort
- Overrides:
writeShortin classStreamOutput- Throws:
IOException
-
writeInt
Description copied from class:StreamOutputWrites an int as four bytes.- Overrides:
writeIntin classStreamOutput- Throws:
IOException
-
writeIntLE
Description copied from class:StreamOutputWrites an int as four bytes, least significant bytes first.- Overrides:
writeIntLEin classStreamOutput- Throws:
IOException
-
writeVInt
Description copied from class:StreamOutputWrites an int in a variable-length format. Writes between one and five bytes. Smaller values take fewer bytes. Negative numbers will always use all 5 bytes and are therefore better serialized usingStreamOutput.writeInt(int)- Overrides:
writeVIntin classStreamOutput- Throws:
IOException
-
writeVIntArray
- Overrides:
writeVIntArrayin classStreamOutput- Throws:
IOException
-
writeZLong
Description copied from class:StreamOutputWrites a long in a variable-length format. Writes between one and ten bytes. Values are remapped by sliding the sign bit into the lsb and then encoded as an unsigned number e.g., 0 -;> 0, -1 -;> 1, 1 -;> 2, ..., Long.MIN_VALUE -;> -1, Long.MAX_VALUE -;> -2 Numbers with small absolute value will have a small encoding If the numbers are known to be non-negative, useStreamOutput.writeVLong(long)- Overrides:
writeZLongin classStreamOutput- Throws:
IOException
-
writeLong
Description copied from class:StreamOutputWrites a long as eight bytes.- Overrides:
writeLongin classStreamOutput- Throws:
IOException
-
writeLongLE
Description copied from class:StreamOutputWrites a long as eight bytes.- Overrides:
writeLongLEin classStreamOutput- Throws:
IOException
-
writeString
Description copied from class:StreamOutputWrite aString: its length in Unicode code units (chars) (NB not bytes) written usingStreamOutput.writeVInt(int)followed by the sequence of characters themselves encoded as UTF-8.May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeString(java.lang.String, java.io.OutputStream).- Specified by:
writeStringin classStreamOutput- Throws:
IOException
-
writeOptionalString
Description copied from class:StreamOutputWrite a possibly-nullString, represented as awhich isinvalid reference
booleanfalseif the string is null, or elsetrueif it is not null, and in this latter case it is followed by the string itself written as if withStreamOutput.writeString(java.lang.String).May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeOptionalString(java.lang.String, java.io.OutputStream).- Specified by:
writeOptionalStringin classStreamOutput- Throws:
IOException
-
writeGenericString
Description copied from class:StreamOutputWrite aStringwithinStreamOutput.writeGenericValue(java.lang.Object), represented as the type code byte0followed by the string itself written as if withStreamOutput.writeString(java.lang.String).May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeGenericString(java.lang.String, java.io.OutputStream).- Specified by:
writeGenericStringin classStreamOutput- Throws:
IOException
-