java.lang.Object
java.io.OutputStream
org.elasticsearch.common.io.stream.StreamOutput
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
BufferedChecksumStreamOutput
,BytesRefStreamOutput
,BytesStream
,CountingStreamOutput
,DataOutputStreamOutput
,OutputStreamStreamOutput
,VersionCheckingStreamOutput
A stream from another node to this node. Technically, it can also be streamed from a byte array but that is mostly for testing.
This class's methods are optimized so you can put the methods that read and write a class next to each other and you can scan them
visually for differences. That means that most variables should be read and written in a single line so even large objects fit both
reading and writing on the screen. It also means that the methods on this class are named very similarly to
StreamInput
. Finally
it means that the "barrier to entry" for adding new methods to this class is relatively low even though it is a shared class with code
everywhere. That being said, this class deals primarily with List
s rather than Arrays. For the most part calls should adapt to
lists, either by storing List
s internally or just converting to and from a List
when calling. This comment is repeated
on StreamInput
.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkWriteable
(Object value) abstract void
close()
Closes this stream to further operations.abstract void
flush()
Forces any buffered output to be written.The transport version to serialize the data as.long
position()
static int
putVInt
(byte[] buffer, int i, int off) void
setTransportVersion
(TransportVersion version) Set the transport version of the data in this stream.void
write
(byte[] b, int off, int len) void
write
(int b) <T> void
writeArray
(Writeable.Writer<T> writer, T[] array) Writes the specified array to the stream using the specifiedWriteable.Writer
for each element in the array.<T extends Writeable>
voidwriteArray
(T[] array) Writes the specified array ofWriteable
s.void
writeBigInteger
(BigInteger bigInteger) Write aBigInteger
to the streamvoid
writeBoolean
(boolean b) Writes a boolean.abstract void
writeByte
(byte b) Writes a single byte.void
writeByteArray
(byte[] b) Writes an array of bytes.void
writeBytes
(byte[] b) Writes an array of bytes.void
writeBytes
(byte[] b, int length) Writes an array of bytes.abstract void
writeBytes
(byte[] b, int offset, int length) Writes an array of bytes.void
writeBytesRef
(org.apache.lucene.util.BytesRef bytes) void
Writes the bytes reference, including a length header.void
writeCollection
(Collection<? extends Writeable> collection) Writes a collection which can then be read usingStreamInput.readCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
or anotherreadCollectionAs*
method.<T> void
writeCollection
(Collection<T> collection, Writeable.Writer<T> writer) Writes a collection which can then be read usingStreamInput.readCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
or anotherreadCollectionAs*
method.void
writeDouble
(double v) void
writeDoubleArray
(double[] values) void
writeDoubleLE
(double v) <E extends Enum<E>>
voidwriteEnum
(E enumValue) Writes an enum with typeE
in terms of the value of its ordinal.<E extends Enum<E>>
voidwriteEnumSet
(EnumSet<E> enumSet) Writes a set of enum with typeE
in terms of the value of its ordinal.void
writeException
(Throwable throwable) void
writeFloat
(float v) void
writeFloatArray
(float[] values) <T> void
writeGenericList
(List<T> v, Writeable.Writer<T> writer) void
writeGenericMap
(Map<String, Object> map) void
void
writeGenericString
(String value) void
writeGenericValue
(Object value) Notice: when serialization a map, the stream out map with the stream in map maybe have the different key-value orders, they will maybe have different stream order.void
writeGeoPoint
(GeoPoint geoPoint) Writes the givenGeoPoint
to the streamfinal void
writeInstant
(Instant instant) Writes anInstant
to the stream with nanosecond resolutionvoid
writeInt
(int i) Writes an int as four bytes.void
writeIntArray
(int[] values) void
writeIntLE
(int i) Writes an int as four bytes, least significant bytes first.void
writeLong
(long i) Writes a long as eight bytes.void
writeLongArray
(long[] values) void
writeLongLE
(long i) Writes a long as eight bytes.final <V> void
writeMap
(Map<String, V> map, Writeable.Writer<V> valueWriter) Same aswriteMap(Map, Writer, Writer)
but forString
keys.final <K,
V> void writeMap
(Map<K, V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) final <V extends Writeable>
voidwriteMapValues
(Map<?, V> map) Writes values of a map as a collectionfinal <V> void
writeMapValues
(Map<?, V> map, Writeable.Writer<V> valueWriter) Writes values of a map as a collectionvoid
writeMapWithConsistentOrder
(Map<String, ? extends Object> map) write map to stream with consistent order to make sure every map generated bytes order are same.void
Similar towriteOptionalString(java.lang.String)
but for use when the value is always missing.<T extends Writeable>
voidwriteMissingWriteable
(Class<T> ignored) Similar towriteOptionalWriteable(org.elasticsearch.common.io.stream.Writeable)
but for use when the value is always missing.void
writeNamedWriteable
(NamedWriteable namedWriteable) Writes aNamedWriteable
to the current stream, by first writing its name and then the object itselfvoid
writeNamedWriteableCollection
(Collection<? extends NamedWriteable> list) Writes a collection ofNamedWriteable
objects which can then be read usingStreamInput.readNamedWriteableCollectionAsList(java.lang.Class<T>)
.<T> void
writeOptional
(Writeable.Writer<T> writer, T maybeItem) Writes a boolean value indicating whether the given object isnull
, followed by the object's serialization if it is notnull
.<T> void
writeOptionalArray
(Writeable.Writer<T> writer, T[] array) Same aswriteArray(Writer, Object[])
but the provided array may be null.<T extends Writeable>
voidwriteOptionalArray
(T[] array) Same aswriteArray(Writeable[])
but the provided array may be null.void
void
writeOptionalByteArray
(byte[] array) Writes a byte array, for null arrays it writes false.void
Writes an optional bytes reference including a length header.<T extends Writeable>
voidwriteOptionalCollection
(Collection<T> collection) Writes a possibly-null
collection which can then be read usingStreamInput.readOptionalCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
.<T> void
writeOptionalCollection
(Collection<T> collection, Writeable.Writer<T> writer) Writes a possibly-null
collection which can then be read usingStreamInput.readOptionalCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
.void
<E extends Enum<E>>
voidwriteOptionalEnum
(E enumValue) Writes an optional enum with typeE
in terms of the value of its ordinal.void
writeOptionalFloat
(Float floatValue) void
writeOptionalFloatArray
(float[] array) Writes a float array, for null arrays it writes false.final void
writeOptionalInstant
(Instant instant) Writes anInstant
to the stream, which could possibly be nullvoid
writeOptionalInt
(Integer integer) Writes an optionalInteger
.void
void
writeOptionalNamedWriteable
(NamedWriteable namedWriteable) Write an optionalNamedWriteable
to the stream.void
writeOptionalSecureString
(SecureString secureStr) void
void
writeOptionalStringArray
(String[] array) Writes a string array, for nullable string, writes false.void
writeOptionalStringCollection
(Collection<String> collection) Writes a possibly-null
collection of strings which can then be read usingStreamInput.readOptionalStringCollectionAsList()
.void
writeOptionalText
(Text text) void
writeOptionalTimeValue
(TimeValue timeValue) Write an optionalTimeValue
to the stream.void
writeOptionalVInt
(Integer integer) void
void
writeOptionalWriteable
(Writeable writeable) Writes a boolean value indicating whether the given object isnull
, followed by the object's serialization if it is notnull
.void
writeOptionalZoneId
(ZoneId timeZone) Write an optional ZoneId to the stream.void
writeSecureString
(SecureString secureStr) final void
writeShort
(short v) void
writeString
(String str) void
writeStringArray
(String[] array) void
writeStringArrayNullable
(String[] array) Writes a string array, for nullable string, writes it as 0 (empty string).void
writeStringCollection
(Collection<String> collection) Writes a collection of strings which can then be read usingStreamInput.readStringCollectionAsList()
or anotherreadStringCollectionAs*
method.void
void
writeTimeValue
(TimeValue timeValue) Write aTimeValue
to the streamvoid
writeVInt
(int i) Writes an int in a variable-length format.void
writeVIntArray
(int[] values) void
writeVLong
(long i) Writes a non-negative long in a variable-length format.void
writeVLongArray
(long[] values) void
writeWithSizePrefix
(Writeable writeable) Serializes a writable just likeWriteable.writeTo(StreamOutput)
would but prefixes it with the serialized size of the result.void
writeWriteable
(Writeable writeable) This method allow to use a method reference when writing collection elements such asout.writeMap(map, StreamOutput::writeString, StreamOutput::writeWriteable)
void
writeZLong
(long i) Writes a long in a variable-length format.void
writeZoneId
(ZoneId timeZone) Write a ZoneId to the stream.Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
Field Details
-
GENERIC_LIST_HEADER
public static final byte GENERIC_LIST_HEADER- See Also:
-
-
Constructor Details
-
StreamOutput
public StreamOutput()
-
-
Method Details
-
getTransportVersion
The transport version to serialize the data as. -
setTransportVersion
Set the transport version of the data in this stream. -
position
- Throws:
IOException
-
writeByte
Writes a single byte.- Throws:
IOException
-
writeBytes
Writes an array of bytes.- Parameters:
b
- the bytes to write- Throws:
IOException
-
writeBytes
Writes an array of bytes.- Parameters:
b
- the bytes to writelength
- the number of bytes to write- Throws:
IOException
-
writeBytes
Writes an array of bytes.- Parameters:
b
- the bytes to writeoffset
- the offset in the byte arraylength
- the number of bytes to write- Throws:
IOException
-
writeByteArray
Writes an array of bytes.- Parameters:
b
- the bytes to write- Throws:
IOException
-
writeWithSizePrefix
Serializes a writable just likeWriteable.writeTo(StreamOutput)
would but prefixes it with the serialized size of the result.- Parameters:
writeable
-Writeable
to serialize- Throws:
IOException
-
writeBytesReference
Writes the bytes reference, including a length header.- Throws:
IOException
-
writeOptionalBytesReference
Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes references. UsewriteBytesReference(BytesReference)
andStreamInput.readBytesReference()
if you do not.- Throws:
IOException
-
writeBytesRef
- Throws:
IOException
-
writeShort
- Throws:
IOException
-
writeInt
Writes an int as four bytes.- Throws:
IOException
-
writeIntLE
Writes an int as four bytes, least significant bytes first.- Throws:
IOException
-
writeVInt
Writes 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 usingwriteInt(int)
- Throws:
IOException
-
putVInt
public static int putVInt(byte[] buffer, int i, int off) -
writeLong
Writes a long as eight bytes.- Throws:
IOException
-
writeLongLE
Writes a long as eight bytes.- Throws:
IOException
-
writeVLong
Writes a non-negative long in a variable-length format. Writes between one and ten bytes. Smaller values take fewer bytes. Negative numbers use ten bytes and trip assertions (if running in tests) so preferwriteLong(long)
orwriteZLong(long)
for negative numbers.- Throws:
IOException
-
writeOptionalVLong
- Throws:
IOException
-
writeZLong
Writes 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, usewriteVLong(long)
- Throws:
IOException
-
writeOptionalLong
- Throws:
IOException
-
writeOptionalString
- Throws:
IOException
-
writeOptionalSecureString
- Throws:
IOException
-
writeOptionalInt
Writes an optionalInteger
.- Throws:
IOException
-
writeOptionalVInt
- Throws:
IOException
-
writeOptionalFloat
- Throws:
IOException
-
writeOptionalText
- Throws:
IOException
-
writeText
- Throws:
IOException
-
writeString
- Throws:
IOException
-
writeSecureString
- Throws:
IOException
-
writeFloat
- Throws:
IOException
-
writeDouble
- Throws:
IOException
-
writeDoubleLE
- Throws:
IOException
-
writeOptionalDouble
- Throws:
IOException
-
writeBoolean
Writes a boolean.- Throws:
IOException
-
writeOptionalBoolean
- Throws:
IOException
-
flush
Forces any buffered output to be written.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
Closes this stream to further operations.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
writeStringArray
- Throws:
IOException
-
writeStringArrayNullable
Writes a string array, for nullable string, writes it as 0 (empty string).- Throws:
IOException
-
writeOptionalStringArray
Writes a string array, for nullable string, writes false.- Throws:
IOException
-
writeOptionalByteArray
Writes a byte array, for null arrays it writes false.- Parameters:
array
- an array or null- Throws:
IOException
-
writeOptionalFloatArray
Writes a float array, for null arrays it writes false.- Parameters:
array
- an array or null- Throws:
IOException
-
writeGenericMap
- Throws:
IOException
-
writeMapWithConsistentOrder
public void writeMapWithConsistentOrder(@Nullable Map<String, ? extends Object> map) throws IOExceptionwrite map to stream with consistent order to make sure every map generated bytes order are same. This method is compatible withStreamInput.readMap
andStreamInput.readGenericValue
This method only will handle the map keys order, not maps contained within the map- Throws:
IOException
-
writeMapValues
public final <V> void writeMapValues(Map<?, V> map, Writeable.Writer<V> valueWriter) throws IOExceptionWrites values of a map as a collection- Throws:
IOException
-
writeMapValues
Writes values of a map as a collection- Throws:
IOException
-
writeMap
public final <K extends Writeable,V extends Writeable> void writeMap(Map<K, V> map) throws IOException- Throws:
IOException
-
writeMap
public final <K,V> void writeMap(Map<K, V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws IOExceptionWrite aMap
ofK
-type keys toV
-type.Map<String, String> map = ...; out.writeMap(map, StreamOutput::writeString, StreamOutput::writeString);
- Parameters:
keyWriter
- The key writervalueWriter
- The value writer- Throws:
IOException
-
writeMap
public final <V> void writeMap(Map<String, V> map, Writeable.Writer<V> valueWriter) throws IOExceptionSame aswriteMap(Map, Writer, Writer)
but forString
keys.- Throws:
IOException
-
writeInstant
Writes anInstant
to the stream with nanosecond resolution- Throws:
IOException
-
writeOptionalInstant
Writes anInstant
to the stream, which could possibly be null- Throws:
IOException
-
writeGenericList
- Throws:
IOException
-
writeGenericString
- Throws:
IOException
-
writeGenericNull
- Throws:
IOException
-
writeGenericValue
Notice: when serialization a map, the stream out map with the stream in map maybe have the different key-value orders, they will maybe have different stream order. If want to keep stream out map and stream in map have the same stream order when stream, can usewriteMapWithConsistentOrder
- Throws:
IOException
-
checkWriteable
- Throws:
IllegalArgumentException
-
writeIntArray
- Throws:
IOException
-
writeVIntArray
- Throws:
IOException
-
writeLongArray
- Throws:
IOException
-
writeVLongArray
- Throws:
IOException
-
writeFloatArray
- Throws:
IOException
-
writeDoubleArray
- Throws:
IOException
-
writeArray
Writes the specified array to the stream using the specifiedWriteable.Writer
for each element in the array. This method can be seen as writer version ofStreamInput.readArray(Writeable.Reader, IntFunction)
. The length of array encoded as a variable-length integer is first written to the stream, and then the elements of the array are written to the stream.- Type Parameters:
T
- the type of the elements of the array- Parameters:
writer
- the writer used to write individual elementsarray
- the array- Throws:
IOException
- if an I/O exception occurs while writing the array
-
writeOptionalArray
public <T> void writeOptionalArray(Writeable.Writer<T> writer, @Nullable T[] array) throws IOException Same aswriteArray(Writer, Object[])
but the provided array may be null. An additional boolean value is serialized to indicate whether the array was null or not.- Throws:
IOException
-
writeArray
Writes the specified array ofWriteable
s. This method can be seen as writer version ofStreamInput.readArray(Writeable.Reader, IntFunction)
. The length of array encoded as a variable-length integer is first written to the stream, and then the elements of the array are written to the stream.- Throws:
IOException
-
writeOptionalArray
Same aswriteArray(Writeable[])
but the provided array may be null. An additional boolean value is serialized to indicate whether the array was null or not.- Throws:
IOException
-
writeOptionalWriteable
Writes a boolean value indicating whether the given object isnull
, followed by the object's serialization if it is notnull
.- Throws:
IOException
- See Also:
-
writeOptional
Writes a boolean value indicating whether the given object isnull
, followed by the object's serialization if it is notnull
.- Throws:
IOException
- See Also:
-
writeWriteable
This method allow to use a method reference when writing collection elements such asout.writeMap(map, StreamOutput::writeString, StreamOutput::writeWriteable)
- Throws:
IOException
-
writeException
- Throws:
IOException
-
writeNamedWriteable
Writes aNamedWriteable
to the current stream, by first writing its name and then the object itself- Throws:
IOException
-
writeOptionalNamedWriteable
Write an optionalNamedWriteable
to the stream.- Throws:
IOException
-
writeGeoPoint
Writes the givenGeoPoint
to the stream- Throws:
IOException
-
writeZoneId
Write a ZoneId to the stream.- Throws:
IOException
-
writeOptionalZoneId
Write an optional ZoneId to the stream.- Throws:
IOException
-
writeCollection
Writes a collection which can then be read usingStreamInput.readCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
or anotherreadCollectionAs*
method. Make sure to read the collection back into the same type as was originally written.- Throws:
IOException
-
writeCollection
public <T> void writeCollection(Collection<T> collection, Writeable.Writer<T> writer) throws IOException Writes a collection which can then be read usingStreamInput.readCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
or anotherreadCollectionAs*
method. Make sure to read the collection back into the same type as was originally written.- Throws:
IOException
-
writeStringCollection
Writes a collection of strings which can then be read usingStreamInput.readStringCollectionAsList()
or anotherreadStringCollectionAs*
method. Make sure to read the collection back into the same type as was originally written.- Throws:
IOException
-
writeOptionalCollection
public <T extends Writeable> void writeOptionalCollection(@Nullable Collection<T> collection) throws IOException Writes a possibly-null
collection which can then be read usingStreamInput.readOptionalCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
.- Throws:
IOException
-
writeOptionalCollection
public <T> void writeOptionalCollection(@Nullable Collection<T> collection, Writeable.Writer<T> writer) throws IOException Writes a possibly-null
collection which can then be read usingStreamInput.readOptionalCollectionAsList(org.elasticsearch.common.io.stream.Writeable.Reader<T>)
.- Throws:
IOException
-
writeOptionalStringCollection
public void writeOptionalStringCollection(@Nullable Collection<String> collection) throws IOException Writes a possibly-null
collection of strings which can then be read usingStreamInput.readOptionalStringCollectionAsList()
.- Throws:
IOException
-
writeNamedWriteableCollection
public void writeNamedWriteableCollection(Collection<? extends NamedWriteable> list) throws IOException Writes a collection ofNamedWriteable
objects which can then be read usingStreamInput.readNamedWriteableCollectionAsList(java.lang.Class<T>)
.- Throws:
IOException
-
writeEnum
Writes an enum with typeE
in terms of the value of its ordinal. Enums serialized like this must have a corresponding test which usesEnumSerializationTestUtils#assertEnumSerialization
to fix the wire protocol.- Throws:
IOException
-
writeOptionalEnum
Writes an optional enum with typeE
in terms of the value of its ordinal. Enums serialized like this must have a corresponding test which usesEnumSerializationTestUtils#assertEnumSerialization
to fix the wire protocol.- Throws:
IOException
-
writeEnumSet
Writes a set of enum with typeE
in terms of the value of its ordinal. Enums serialized like this must have a corresponding test which usesEnumSerializationTestUtils#assertEnumSerialization
to fix the wire protocol.- Throws:
IOException
-
writeTimeValue
Write aTimeValue
to the stream- Throws:
IOException
-
writeOptionalTimeValue
Write an optionalTimeValue
to the stream.- Throws:
IOException
-
writeMissingWriteable
Similar towriteOptionalWriteable(org.elasticsearch.common.io.stream.Writeable)
but for use when the value is always missing.- Throws:
IOException
-
writeMissingString
Similar towriteOptionalString(java.lang.String)
but for use when the value is always missing.- Throws:
IOException
-
writeBigInteger
Write aBigInteger
to the stream- Throws:
IOException
-