- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
ReleasableBytesStreamOutput
StreamOutput that accumulates the resulting data in memory, using BigArrays to avoids frequent reallocation &
copying of the internal data once the resulting data grows large enough whilst avoiding excessive overhead in the final result for small
objects.
A BytesStreamOutput accumulates data using a non-recycling BigArrays and, as with an OutputStreamStreamOutput, it
uses a thread-locally-cached buffer for some of its writes and pushes data to the underlying array in small chunks, causing frequent
calls to BigArrays.resize(org.elasticsearch.common.util.ByteArray, long). If the array is large enough (≥16kiB) then the resize operations happen in-place, allocating a new
16kiB byte[] and appending it to the array, but for smaller arrays these resize operations allocate a completely fresh
byte[] into which they copy the entire contents of the old one.
BigArrays.resize(org.elasticsearch.common.util.ByteArray, long) grows smaller arrays more slowly than a ByteArrayOutputStream, with a target of 12.5% overhead rather
than 100%, which means that a sequence of smaller writes causes more allocations and copying overall. It may be worth adding a
BufferedStreamOutput wrapper to reduce the frequency of the resize operations, especially if a suitable buffer is already
allocated and available.
The resulting BytesReference is a view over the underlying byte[] pages and involves no significant extra allocation to
obtain. It is oversized: The worst case for overhead is when the data is one byte more than a 16kiB page and therefore the result must
retain two pages even though all but one byte of the second page is unused. For smaller objects the overhead will be 12.5%.
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
FieldsFields inherited from class org.elasticsearch.common.io.stream.StreamOutput
GENERIC_LIST_HEADER -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreate a non recyclingBytesStreamOutputwith an initial capacity of 0.BytesStreamOutput(int expectedSize) Create a non recyclingBytesStreamOutputwith enough initial pages acquired to satisfy the capacity given by expected size.protectedBytesStreamOutput(int expectedSize, BigArrays bigArrays) -
Method Summary
Modifier and TypeMethodDescriptionbytes()voidclose()Closes this stream to further operations.Likebytes()but copies the bytes to a freshly allocated buffer.protected voidensureCapacity(long offset) voidflush()Forces any buffered output to be written.longposition()voidreset()voidseek(long position) intsize()Returns the current size of the buffer.voidskip(int length) voidwriteByte(byte b) Writes a single byte.voidwriteBytes(byte[] b, int offset, int length) Writes an array of bytes.voidwriteGenericString(String value) Write aStringwithinStreamOutput.writeGenericValue(java.lang.Object), represented as the type code byte0followed by the string itself written as if withStreamOutput.writeString(java.lang.String).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).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.Methods inherited from class org.elasticsearch.common.io.stream.BytesStream
writeWithSizePrefixMethods 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, writeInt, writeIntArray, writeIntLE, writeLong, writeLongArray, writeLongLE, 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, writeShort, writeStringArray, writeStringArrayNullable, writeStringCollection, writeText, writeTimeValue, writeVInt, writeVIntArray, writeVLong, writeVLongArray, writeWriteable, writeZLong, writeZoneIdMethods inherited from class java.io.OutputStream
nullOutputStream, write
-
Field Details
-
bigArrays
-
bytes
-
count
protected int count
-
-
Constructor Details
-
BytesStreamOutput
public BytesStreamOutput()Create a non recyclingBytesStreamOutputwith an initial capacity of 0. -
BytesStreamOutput
public BytesStreamOutput(int expectedSize) Create a non recyclingBytesStreamOutputwith enough initial pages acquired to satisfy the capacity given by expected size.- Parameters:
expectedSize- the expected maximum size of the stream in bytes.
-
BytesStreamOutput
-
-
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
public void writeByte(byte b) Description copied from class:StreamOutputWrites a single byte.- Specified by:
writeBytein classStreamOutput
-
writeBytes
public void writeBytes(byte[] b, int offset, int length) 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
-
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
-
reset
public void reset() -
flush
public void flush()Description copied from class:StreamOutputForces any buffered output to be written.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classStreamOutput
-
seek
public void seek(long position) - Specified by:
seekin classBytesStream
-
skip
public void skip(int length) -
close
public void close()Description copied from class:StreamOutputCloses this stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classStreamOutput
-
size
public int size()Returns the current size of the buffer.- Returns:
- the value of the
countfield, which is the number of valid bytes in this output stream. - See Also:
-
bytes
- Specified by:
bytesin classBytesStream
-
copyBytes
Likebytes()but copies the bytes to a freshly allocated buffer.- Returns:
- copy of the bytes in this instances
-
ensureCapacity
protected void ensureCapacity(long offset)
-