Class StreamOutput
java.lang.Object
java.io.OutputStream
org.elasticsearch.common.io.stream.StreamOutput
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
BufferedChecksumStreamOutput,BytesStream,DataOutputStreamOutput,OutputStreamStreamOutput
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 Lists rather than Arrays. For the most part calls should adapt to
lists, either by storing Lists internally or just converting to and from a List when calling. This comment is repeated
on StreamInput.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckWriteable(Object value)abstract voidclose()Closes this stream to further operations.abstract voidflush()Forces any buffered output to be written.The version of the node on the other side of this stream.booleanhasFeature(String feature)Test if the stream has the specified feature.longposition()abstract voidreset()voidseek(long position)voidsetFeatures(Set<String> features)Set the features on the stream.voidsetVersion(Version version)Set the version of the node on the other side of this stream.voidwrite(byte[] b, int off, int len)voidwrite(int b)<T> voidwriteArray(Writeable.Writer<T> writer, T[] array)Writes the specified array to the stream using the specifiedWriteable.Writerfor each element in the array.<T extends Writeable>
voidwriteArray(T[] array)Writes the specified array ofWriteables.voidwriteBoolean(boolean b)Writes a boolean.abstract voidwriteByte(byte b)Writes a single byte.voidwriteByteArray(byte[] b)Writes an array of bytes.voidwriteBytes(byte[] b)Writes an array of bytes.voidwriteBytes(byte[] b, int length)Writes an array of bytes.abstract voidwriteBytes(byte[] b, int offset, int length)Writes an array of bytes.voidwriteBytesRef(org.apache.lucene.util.BytesRef bytes)voidWrites the bytes reference, including a length header.voidwriteCollection(Collection<? extends Writeable> collection)Writes a collection to this stream.<T> voidwriteCollection(Collection<T> collection, Writeable.Writer<T> writer)Writes a collection of objects via aWriteable.Writer.voidwriteDouble(double v)voidwriteDoubleArray(double[] values)<E extends Enum<E>>
voidwriteEnum(E enumValue)Writes an enum with type E based on its ordinal value<E extends Enum<E>>
voidwriteEnumSet(EnumSet<E> enumSet)Writes an EnumSet with type E that by serialized it based on it's ordinal valuevoidwriteException(Throwable throwable)voidwriteFloat(float v)voidwriteFloatArray(float[] values)voidwriteGenericValue(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.voidwriteGeoPoint(GeoPoint geoPoint)Writes the givenGeoPointto the streamvoidwriteInstant(Instant instant)Writes anInstantto the stream with nanosecond resolutionvoidwriteInt(int i)Writes an int as four bytes.voidwriteIntArray(int[] values)voidWrites a list ofWriteableobjectsvoidwriteLong(long i)Writes a long as eight bytes.voidwriteLongArray(long[] values)void<K, V> voidwriteMap(Map<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)writeMap(ImmutableOpenMap<K,V> map)<K, V> voidwriteMap(ImmutableOpenMap<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)<K, V> voidwriteMapOfLists(Map<K,List<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)voidwriteMapWithConsistentOrder(Map<String,? extends Object> map)write map to stream with consistent order to make sure every map generated bytes order are same.voidwriteNamedWriteable(NamedWriteable namedWriteable)Writes aNamedWriteableto the current stream, by first writing its name and then the object itselfvoidwriteNamedWriteableList(List<? extends NamedWriteable> list)Writes a list ofNamedWriteableobjects.<T> voidwriteOptionalArray(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.voidvoidWrites an optional bytes reference including a length header.void<E extends Enum<E>>
voidwriteOptionalEnum(E enumValue)Writes an optional enum with type E based on its ordinal valuevoidwriteOptionalFloat(Float floatValue)voidwriteOptionalInstant(Instant instant)Writes anInstantto the stream, which could possibly be nullvoidwriteOptionalInt(Integer integer)Writes an optionalInteger.voidvoidwriteOptionalNamedWriteable(NamedWriteable namedWriteable)Write an optionalNamedWriteableto the stream.voidwriteOptionalSecureString(SecureString secureStr)voidvoidwriteOptionalStringArray(String[] array)Writes a string array, for nullable string, writes false.voidwriteOptionalStringCollection(Collection<String> collection)Writes an optional collection of a strings.voidwriteOptionalText(Text text)voidwriteOptionalTimeValue(org.elasticsearch.core.TimeValue timeValue)Write an optionalTimeValueto the stream.voidwriteOptionalTimeZone(org.joda.time.DateTimeZone timeZone)Write an optional DateTimeZone to the stream.voidwriteOptionalVInt(Integer integer)voidvoidwriteOptionalWriteable(Writeable writeable)voidwriteOptionalZoneId(ZoneId timeZone)Write an optional ZoneId to the stream.voidwriteSecureString(SecureString secureStr)voidwriteShort(short v)voidwriteString(String str)voidwriteStringArray(String[] array)voidwriteStringArrayNullable(String[] array)Writes a string array, for nullable string, writes it as 0 (empty string).voidwriteStringCollection(Collection<String> collection)Writes a collection of a strings.voidvoidwriteTimeValue(org.elasticsearch.core.TimeValue timeValue)Write aTimeValueto the streamvoidwriteTimeZone(org.joda.time.DateTimeZone timeZone)Write a DateTimeZone to the stream.voidwriteVInt(int i)Writes an int in a variable-length format.voidwriteVIntArray(int[] values)voidwriteVLong(long i)Writes a non-negative long in a variable-length format.voidwriteVLongArray(long[] values)voidwriteZLong(long i)Writes a long in a variable-length format.voidwriteZoneId(ZoneId timeZone)Write a ZoneId to the stream.Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
Constructor Details
-
StreamOutput
public StreamOutput()
-
-
Method Details
-
getVersion
The version of the node on the other side of this stream. -
setVersion
Set the version of the node on the other side of this stream. -
hasFeature
Test if the stream has the specified feature. Features are used when serializingClusterState.CustomorMetadata.Custom; see alsoClusterState.FeatureAware.- Parameters:
feature- the feature to test- Returns:
- true if the stream has the specified feature
-
setFeatures
Set the features on the stream. SeehasFeature(String).- Parameters:
features- the features on the stream
-
getFeatures
-
position
- Throws:
IOException
-
seek
- 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
-
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
-
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
-
writeLong
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
-
writeOptionalDouble
- Throws:
IOException
-
writeBoolean
Writes a boolean.- Throws:
IOException
-
writeOptionalBoolean
- Throws:
IOException
-
flush
Forces any buffered output to be written.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
Closes this stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
reset
- Throws:
IOException
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein 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
-
writeMap
- 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.readMapandStreamInput.readGenericValueThis method only will handle the map keys order, not maps contained within the map- Throws:
IOException
-
writeMapOfLists
public final <K, V> void writeMapOfLists(Map<K,List<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws IOExceptionWrite aMapofK-type keys toV-typeLists.Map<String, List<String>> map = ...; out.writeMapOfLists(map, StreamOutput::writeString, StreamOutput::writeString);- Parameters:
keyWriter- The key writervalueWriter- The value writer- Throws:
IOException
-
writeMap
public final <K, V> void writeMap(Map<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws IOExceptionWrite aMapofK-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 <K, V> void writeMap(ImmutableOpenMap<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws IOException- Parameters:
keyWriter- The key writervalueWriter- The value writer- Throws:
IOException
-
writeMap
public final <K extends Writeable, V extends Writeable> void writeMap(ImmutableOpenMap<K,V> map) throws IOException- Throws:
IOException
-
writeInstant
Writes anInstantto the stream with nanosecond resolution- Throws:
IOException
-
writeOptionalInstant
Writes anInstantto the stream, which could possibly be null- 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.Writerfor 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 IOExceptionSame 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 ofWriteables. 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
- Throws:
IOException
-
writeException
- Throws:
IOException
-
writeNamedWriteable
Writes aNamedWriteableto the current stream, by first writing its name and then the object itself- Throws:
IOException
-
writeOptionalNamedWriteable
Write an optionalNamedWriteableto the stream.- Throws:
IOException
-
writeGeoPoint
Writes the givenGeoPointto the stream- Throws:
IOException
-
writeTimeZone
Write a DateTimeZone to the stream.- Throws:
IOException
-
writeZoneId
Write a ZoneId to the stream.- Throws:
IOException
-
writeOptionalTimeZone
Write an optional DateTimeZone to the stream.- Throws:
IOException
-
writeOptionalZoneId
Write an optional ZoneId to the stream.- Throws:
IOException
-
writeCollection
Writes a collection to this stream. The corresponding collection can be read from a stream input usingStreamInput.readList(Writeable.Reader).- Parameters:
collection- the collection to write to this stream- Throws:
IOException- if an I/O exception occurs writing the collection
-
writeList
Writes a list ofWriteableobjects- Throws:
IOException
-
writeCollection
public <T> void writeCollection(Collection<T> collection, Writeable.Writer<T> writer) throws IOExceptionWrites a collection of objects via aWriteable.Writer.- Parameters:
collection- the collection of objects- Throws:
IOException- if an I/O exception occurs writing the collection
-
writeStringCollection
Writes a collection of a strings. The corresponding collection can be read from a stream input usingStreamInput.readList(Writeable.Reader).- Parameters:
collection- the collection of strings- Throws:
IOException- if an I/O exception occurs writing the collection
-
writeOptionalStringCollection
Writes an optional collection of a strings. The corresponding collection can be read from a stream input usingStreamInput.readList(Writeable.Reader).- Parameters:
collection- the collection of strings- Throws:
IOException- if an I/O exception occurs writing the collection
-
writeNamedWriteableList
Writes a list ofNamedWriteableobjects.- Throws:
IOException
-
writeEnum
Writes an enum with type E based on its ordinal value- Throws:
IOException
-
writeOptionalEnum
Writes an optional enum with type E based on its ordinal value- Throws:
IOException
-
writeEnumSet
Writes an EnumSet with type E that by serialized it based on it's ordinal value- Throws:
IOException
-
writeTimeValue
Write aTimeValueto the stream- Throws:
IOException
-
writeOptionalTimeValue
public void writeOptionalTimeValue(@Nullable org.elasticsearch.core.TimeValue timeValue) throws IOExceptionWrite an optionalTimeValueto the stream.- Throws:
IOException
-