Class StreamOutput
org.elasticsearch.common.io.stream.StreamOutput
- All Implemented Interfaces:
,
,
- Direct Known Subclasses:
BufferedChecksumStreamOutput
,BytesStream
,DataOutputStreamOutput
,OutputStreamStreamOutput
public abstract class StreamOutput
extends
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
.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
value)
abstract void
close()
Closes this stream to further operations.abstract void
flush()
Forces any buffered output to be written.<>
The version of the node on the other side of this stream.boolean
feature)
Test if the stream has the specified feature.long
position()
abstract void
reset()
void
seek(long position)
void
<> features)
Set the features on the stream.void
setVersion(Version version)
Set the version of the node on the other side of 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
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
writeBytesReference(BytesReference bytes)
Writes the bytes reference, including a length header.void
<? extends Writeable> collection)
Writes a collection to this stream.<T> void
<T> collection, Writeable.Writer<T> writer)
Writes a collection of objects via aWriteable.Writer
.void
writeDouble(double v)
void
writeDoubleArray(double[] values)
<E extends <E>>
voidwriteEnum(E enumValue)
Writes an enum with type E based on its ordinal value<E extends <E>>
void<E> enumSet)
Writes an EnumSet with type E that by serialized it based on it's ordinal valuevoid
throwable)
void
writeFloat(float v)
void
writeFloatArray(float[] values)
void
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 streamvoid
instant)
Writes an to the stream with nanosecond resolutionvoid
writeInt(int i)
Writes an int as four bytes.void
writeIntArray(int[] values)
void
<? extends Writeable> list)
Writes a list ofWriteable
objectsvoid
writeLong(long i)
Writes a long as eight bytes.void
writeLongArray(long[] values)
void
<,> map)
<K, V> void
<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)
Write a ofK
-type keys toV
-type.writeMap(ImmutableOpenMap<K,V> map)
<K, V> void
writeMap(ImmutableOpenMap<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)
<K, V> void
<K,<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)
Write a ofK
-type keys toV
-type s.void
<,? extends > map)
write map to stream with consistent order to make sure every map generated bytes order are same.void
writeNamedWriteable(NamedWriteable namedWriteable)
Writes aNamedWriteable
to the current stream, by first writing its name and then the object itselfvoid
<? extends NamedWriteable> list)
Writes a list ofNamedWriteable
objects.<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
b)
void
Writes an optional bytes reference including a length header.void
v)
<E extends <E>>
voidwriteOptionalEnum(E enumValue)
Writes an optional enum with type E based on its ordinal valuevoid
floatValue)
void
instant)
Writes an to the stream, which could possibly be nullvoid
integer)
Writes an optional .void
l)
void
writeOptionalNamedWriteable(NamedWriteable namedWriteable)
Write an optionalNamedWriteable
to the stream.void
writeOptionalSecureString(SecureString secureStr)
void
str)
void
[] array)
Writes a string array, for nullable string, writes false.void
<> collection)
Writes an optional collection of a strings.void
writeOptionalText(Text text)
void
writeOptionalTimeValue(org.elasticsearch.core.TimeValue timeValue)
Write an optionalTimeValue
to the stream.void
writeOptionalTimeZone(org.joda.time.DateTimeZone timeZone)
Write an optional DateTimeZone to the stream.void
integer)
void
l)
void
writeOptionalWriteable(Writeable writeable)
void
timeZone)
Write an optional to the stream.void
writeSecureString(SecureString secureStr)
void
writeShort(short v)
void
str)
void
[] array)
void
[] array)
Writes a string array, for nullable string, writes it as 0 (empty string).void
<> collection)
Writes a collection of a strings.void
void
writeTimeValue(org.elasticsearch.core.TimeValue timeValue)
Write aTimeValue
to the streamvoid
writeTimeZone(org.joda.time.DateTimeZone timeZone)
Write a DateTimeZone to the stream.void
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
writeZLong(long i)
Writes a long in a variable-length format.void
timeZone)
Write a to the stream.Methods inherited from class java.io.
,
Methods inherited from class java.lang.
, , , , , , , , , ,
-
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
public boolean hasFeature( feature)Test if the stream has the specified feature. Features are used when serializingClusterState.Custom
orMetadata.Custom
; see alsoClusterState.FeatureAware
.- Parameters:
feature
- the feature to test- Returns:
- true if the stream has the specified feature
-
setFeatures
public void setFeatures(<> features)Set the features on the stream. SeehasFeature(String)
.- Parameters:
features
- the features on the stream
-
getFeatures
public <> getFeatures() -
position
public long position() throws- Throws:
-
seek
public void seek(long position) throws- Throws:
-
writeByte
public abstract void writeByte(byte b) throwsWrites a single byte.- Throws:
-
writeBytes
public void writeBytes(byte[] b) throwsWrites an array of bytes.- Parameters:
b
- the bytes to write- Throws:
-
writeBytes
public void writeBytes(byte[] b, int length) throwsWrites an array of bytes.- Parameters:
b
- the bytes to writelength
- the number of bytes to write- Throws:
-
writeBytes
public abstract void writeBytes(byte[] b, int offset, int length) throwsWrites an array of bytes.- Parameters:
b
- the bytes to writeoffset
- the offset in the byte arraylength
- the number of bytes to write- Throws:
-
writeByteArray
public void writeByteArray(byte[] b) throwsWrites an array of bytes.- Parameters:
b
- the bytes to write- Throws:
-
writeBytesReference
Writes the bytes reference, including a length header.- Throws:
-
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:
-
writeBytesRef
public void writeBytesRef(org.apache.lucene.util.BytesRef bytes) throws- Throws:
-
writeShort
public final void writeShort(short v) throws- Throws:
-
writeInt
public void writeInt(int i) throwsWrites an int as four bytes.- Throws:
-
writeVInt
public void writeVInt(int i) throwsWrites 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:
-
writeLong
public void writeLong(long i) throwsWrites a long as eight bytes.- Throws:
-
writeVLong
public void writeVLong(long i) throwsWrites 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:
-
writeOptionalVLong
public void writeOptionalVLong(@Nullable l) throws- Throws:
-
writeZLong
public void writeZLong(long i) throwsWrites 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:
-
writeOptionalLong
public void writeOptionalLong(@Nullable l) throws- Throws:
-
writeOptionalString
public void writeOptionalString(@Nullable str) throws- Throws:
-
writeOptionalSecureString
- Throws:
-
writeOptionalInt
public void writeOptionalInt(@Nullable integer) throwsWrites an optional .- Throws:
-
writeOptionalVInt
public void writeOptionalVInt(@Nullable integer) throws- Throws:
-
writeOptionalFloat
public void writeOptionalFloat(@Nullable floatValue) throws- Throws:
-
writeOptionalText
- Throws:
-
writeText
- Throws:
-
writeString
public void writeString( str) throws- Throws:
-
writeSecureString
- Throws:
-
writeFloat
public void writeFloat(float v) throws- Throws:
-
writeDouble
public void writeDouble(double v) throws- Throws:
-
writeOptionalDouble
public void writeOptionalDouble(@Nullable v) throws- Throws:
-
writeBoolean
public void writeBoolean(boolean b) throwsWrites a boolean.- Throws:
-
writeOptionalBoolean
public void writeOptionalBoolean(@Nullable b) throws- Throws:
-
flush
public abstract void flush() throwsForces any buffered output to be written.- Specified by:
in interface
- Overrides:
in class
- Throws:
-
close
public abstract void close() throwsCloses this stream to further operations.- Specified by:
in interface
- Specified by:
in interface
- Overrides:
in class
- Throws:
-
reset
public abstract void reset() throws- Throws:
-
write
public void write(int b) throws- Specified by:
in class
- Throws:
-
write
public void write(byte[] b, int off, int len) throws- Overrides:
in class
- Throws:
-
writeStringArray
public void writeStringArray([] array) throws- Throws:
-
writeStringArrayNullable
public void writeStringArrayNullable(@Nullable [] array) throwsWrites a string array, for nullable string, writes it as 0 (empty string).- Throws:
-
writeOptionalStringArray
public void writeOptionalStringArray(@Nullable [] array) throwsWrites a string array, for nullable string, writes false.- Throws:
-
writeMap
public void writeMap(@Nullable <,> map) throws- Throws:
-
writeMapWithConsistentOrder
public void writeMapWithConsistentOrder(@Nullable <,? extends > map) throwswrite 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:
-
writeMapOfLists
public final <K, V> void writeMapOfLists(<K,<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throwsWrite a ofK
-type keys toV
-type s.Map<String, List<String>> map = ...; out.writeMapOfLists(map, StreamOutput::writeString, StreamOutput::writeString);
- Parameters:
keyWriter
- The key writervalueWriter
- The value writer- Throws:
-
writeMap
public final <K, V> void writeMap(<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throwsWrite a 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:
-
writeMap
public final <K, V> void writeMap(ImmutableOpenMap<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws- Parameters:
keyWriter
- The key writervalueWriter
- The value writer- Throws:
-
writeMap
public final <K extends Writeable, V extends Writeable> void writeMap(ImmutableOpenMap<K,V> map) throws- Throws:
-
writeInstant
public final void writeInstant( instant) throwsWrites an to the stream with nanosecond resolution- Throws:
-
writeOptionalInstant
public final void writeOptionalInstant(@Nullable instant) throwsWrites an to the stream, which could possibly be null- Throws:
-
writeGenericValue
public void writeGenericValue(@Nullable value) throwsNotice: 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:
-
checkWriteable
public static void checkWriteable(@Nullable value) throws- Throws:
-
writeIntArray
public void writeIntArray(int[] values) throws- Throws:
-
writeVIntArray
public void writeVIntArray(int[] values) throws- Throws:
-
writeLongArray
public void writeLongArray(long[] values) throws- Throws:
-
writeVLongArray
public void writeVLongArray(long[] values) throws- Throws:
-
writeFloatArray
public void writeFloatArray(float[] values) throws- Throws:
-
writeDoubleArray
public void writeDoubleArray(double[] values) throws- Throws:
-
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:
- if an I/O exception occurs while writing the array
-
writeOptionalArray
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:
-
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:
-
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:
-
writeOptionalWriteable
- Throws:
-
writeException
public void writeException( throwable) throws- Throws:
-
writeNamedWriteable
Writes aNamedWriteable
to the current stream, by first writing its name and then the object itself- Throws:
-
writeOptionalNamedWriteable
Write an optionalNamedWriteable
to the stream.- Throws:
-
writeGeoPoint
Writes the givenGeoPoint
to the stream- Throws:
-
writeTimeZone
public void writeTimeZone(org.joda.time.DateTimeZone timeZone) throwsWrite a DateTimeZone to the stream.- Throws:
-
writeZoneId
public void writeZoneId( timeZone) throwsWrite a to the stream.- Throws:
-
writeOptionalTimeZone
public void writeOptionalTimeZone(@Nullable org.joda.time.DateTimeZone timeZone) throwsWrite an optional DateTimeZone to the stream.- Throws:
-
writeOptionalZoneId
public void writeOptionalZoneId(@Nullable timeZone) throwsWrite an optional to the stream.- Throws:
-
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:
- if an I/O exception occurs writing the collection
-
writeList
Writes a list ofWriteable
objects- Throws:
-
writeCollection
Writes a collection of objects via aWriteable.Writer
.- Parameters:
collection
- the collection of objects- Throws:
- if an I/O exception occurs writing the collection
-
writeStringCollection
public void writeStringCollection(<> collection) throwsWrites 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:
- if an I/O exception occurs writing the collection
-
writeOptionalStringCollection
public void writeOptionalStringCollection(<> collection) throwsWrites 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:
- if an I/O exception occurs writing the collection
-
writeNamedWriteableList
Writes a list ofNamedWriteable
objects.- Throws:
-
writeEnum
public <E extends <E>> void writeEnum(E enumValue) throwsWrites an enum with type E based on its ordinal value- Throws:
-
writeOptionalEnum
public <E extends <E>> void writeOptionalEnum(@Nullable E enumValue) throwsWrites an optional enum with type E based on its ordinal value- Throws:
-
writeEnumSet
public <E extends <E>> void writeEnumSet(<E> enumSet) throwsWrites an EnumSet with type E that by serialized it based on it's ordinal value- Throws:
-
writeTimeValue
public void writeTimeValue(org.elasticsearch.core.TimeValue timeValue) throwsWrite aTimeValue
to the stream- Throws:
-
writeOptionalTimeValue
public void writeOptionalTimeValue(@Nullable org.elasticsearch.core.TimeValue timeValue) throwsWrite an optionalTimeValue
to the stream.- Throws:
-