Class StreamOutput
- java.lang.Object
-
- java.io.OutputStream
-
- org.elasticsearch.common.io.stream.StreamOutput
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
- Direct Known Subclasses:
BufferedChecksumStreamOutput
,BytesStream
,DataOutputStreamOutput
,OutputStreamStreamOutput
public abstract class StreamOutput extends java.io.OutputStream
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 toStreamInput
. 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 withList
s rather than Arrays. For the most part calls should adapt to lists, either by storingList
s internally or just converting to and from aList
when calling. This comment is repeated onStreamInput
.
-
-
Constructor Summary
Constructors Constructor Description StreamOutput()
-
Method Summary
Modifier and Type Method Description abstract void
close()
Closes this stream to further operations.abstract void
flush()
Forces any buffered output to be written.Version
getVersion()
The version of the node on the other side of this stream.boolean
hasFeature(java.lang.String feature)
Test if the stream has the specified feature.long
position()
abstract void
reset()
void
seek(long position)
void
setFeatures(java.util.Set<java.lang.String> 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
writeCollection(java.util.Collection<? extends Writeable> collection)
Writes a collection to this stream.<T> void
writeCollection(java.util.Collection<T> collection, Writeable.Writer<T> writer)
Writes a collection of objects via aWriteable.Writer
.void
writeDouble(double v)
void
writeDoubleArray(double[] values)
<E extends java.lang.Enum<E>>
voidwriteEnum(E enumValue)
Writes an enum with type E that by serialized it based on it's ordinal value<E extends java.lang.Enum<E>>
voidwriteEnumSet(java.util.EnumSet<E> enumSet)
Writes an EnumSet with type E that by serialized it based on it's ordinal valuevoid
writeException(java.lang.Throwable throwable)
void
writeFloat(float v)
void
writeFloatArray(float[] values)
void
writeGenericValue(java.lang.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 streamvoid
writeInstant(java.time.Instant instant)
Writes anInstant
to the stream with nanosecond resolutionvoid
writeInt(int i)
Writes an int as four bytes.void
writeIntArray(int[] values)
void
writeList(java.util.List<? extends Writeable> list)
Writes a list ofWriteable
objectsvoid
writeLong(long i)
Writes a long as eight bytes.void
writeLongArray(long[] values)
void
writeMap(java.util.Map<java.lang.String,java.lang.Object> map)
<K,V>
voidwriteMap(java.util.Map<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)
Write aMap
ofK
-type keys toV
-type.<K,V>
voidwriteMapOfLists(java.util.Map<K,java.util.List<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter)
Write aMap
ofK
-type keys toV
-typeList
s.void
writeMapWithConsistentOrder(java.util.Map<java.lang.String,? extends java.lang.Object> 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
writeNamedWriteableList(java.util.List<? 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
writeOptionalBoolean(java.lang.Boolean b)
void
writeOptionalBytesReference(BytesReference bytes)
Writes an optional bytes reference including a length header.void
writeOptionalDouble(java.lang.Double v)
void
writeOptionalFloat(java.lang.Float floatValue)
void
writeOptionalInstant(java.time.Instant instant)
Writes anInstant
to the stream, which could possibly be nullvoid
writeOptionalLong(java.lang.Long l)
void
writeOptionalNamedWriteable(NamedWriteable namedWriteable)
Write an optionalNamedWriteable
to the stream.void
writeOptionalStreamable(Streamable streamable)
Serializes a potential null value.void
writeOptionalString(java.lang.String str)
void
writeOptionalStringArray(java.lang.String[] array)
Writes a string array, for nullable string, writes false.void
writeOptionalText(Text text)
void
writeOptionalTimeValue(TimeValue timeValue)
Write an optionalTimeValue
to the stream.void
writeOptionalTimeZone(org.joda.time.DateTimeZone timeZone)
Write an optional DateTimeZone to the stream.void
writeOptionalVInt(java.lang.Integer integer)
void
writeOptionalWriteable(Writeable writeable)
void
writeShort(short v)
void
writeStreamableList(java.util.List<? extends Streamable> list)
Writes a list ofStreamable
objectsvoid
writeString(java.lang.String str)
void
writeStringArray(java.lang.String[] array)
void
writeStringArrayNullable(java.lang.String[] array)
Writes a string array, for nullable string, writes it as 0 (empty string).void
writeStringCollection(java.util.Collection<java.lang.String> collection)
Writes a collection of a strings.void
writeText(Text text)
void
writeTimeValue(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.
-
-
-
Method Detail
-
getVersion
public Version getVersion()
The version of the node on the other side of this stream.
-
setVersion
public void setVersion(Version version)
Set the version of the node on the other side of this stream.
-
hasFeature
public boolean hasFeature(java.lang.String 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(java.util.Set<java.lang.String> features)
Set the features on the stream. SeehasFeature(String)
.- Parameters:
features
- the features on the stream
-
position
public long position() throws java.io.IOException
- Throws:
java.io.IOException
-
seek
public void seek(long position) throws java.io.IOException
- Throws:
java.io.IOException
-
writeByte
public abstract void writeByte(byte b) throws java.io.IOException
Writes a single byte.- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(byte[] b) throws java.io.IOException
Writes an array of bytes.- Parameters:
b
- the bytes to write- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(byte[] b, int length) throws java.io.IOException
Writes an array of bytes.- Parameters:
b
- the bytes to writelength
- the number of bytes to write- Throws:
java.io.IOException
-
writeBytes
public abstract void writeBytes(byte[] b, int offset, int length) throws java.io.IOException
Writes an array of bytes.- Parameters:
b
- the bytes to writeoffset
- the offset in the byte arraylength
- the number of bytes to write- Throws:
java.io.IOException
-
writeByteArray
public void writeByteArray(byte[] b) throws java.io.IOException
Writes an array of bytes.- Parameters:
b
- the bytes to write- Throws:
java.io.IOException
-
writeBytesReference
public void writeBytesReference(@Nullable BytesReference bytes) throws java.io.IOException
Writes the bytes reference, including a length header.- Throws:
java.io.IOException
-
writeOptionalBytesReference
public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws java.io.IOException
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:
java.io.IOException
-
writeBytesRef
public void writeBytesRef(org.apache.lucene.util.BytesRef bytes) throws java.io.IOException
- Throws:
java.io.IOException
-
writeShort
public final void writeShort(short v) throws java.io.IOException
- Throws:
java.io.IOException
-
writeInt
public void writeInt(int i) throws java.io.IOException
Writes an int as four bytes.- Throws:
java.io.IOException
-
writeVInt
public void writeVInt(int i) throws java.io.IOException
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:
java.io.IOException
-
writeLong
public void writeLong(long i) throws java.io.IOException
Writes a long as eight bytes.- Throws:
java.io.IOException
-
writeVLong
public void writeVLong(long i) throws java.io.IOException
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:
java.io.IOException
-
writeZLong
public void writeZLong(long i) throws java.io.IOException
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:
java.io.IOException
-
writeOptionalLong
public void writeOptionalLong(@Nullable java.lang.Long l) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOptionalString
public void writeOptionalString(@Nullable java.lang.String str) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOptionalVInt
public void writeOptionalVInt(@Nullable java.lang.Integer integer) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOptionalFloat
public void writeOptionalFloat(@Nullable java.lang.Float floatValue) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOptionalText
public void writeOptionalText(@Nullable Text text) throws java.io.IOException
- Throws:
java.io.IOException
-
writeText
public void writeText(Text text) throws java.io.IOException
- Throws:
java.io.IOException
-
writeString
public void writeString(java.lang.String str) throws java.io.IOException
- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException
- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOptionalDouble
public void writeOptionalDouble(@Nullable java.lang.Double v) throws java.io.IOException
- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean b) throws java.io.IOException
Writes a boolean.- Throws:
java.io.IOException
-
writeOptionalBoolean
public void writeOptionalBoolean(@Nullable java.lang.Boolean b) throws java.io.IOException
- Throws:
java.io.IOException
-
flush
public abstract void flush() throws java.io.IOException
Forces any buffered output to be written.- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
close
public abstract void close() throws java.io.IOException
Closes this stream to further operations.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
reset
public abstract void reset() throws java.io.IOException
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
writeStringArray
public void writeStringArray(java.lang.String[] array) throws java.io.IOException
- Throws:
java.io.IOException
-
writeStringArrayNullable
public void writeStringArrayNullable(@Nullable java.lang.String[] array) throws java.io.IOException
Writes a string array, for nullable string, writes it as 0 (empty string).- Throws:
java.io.IOException
-
writeOptionalStringArray
public void writeOptionalStringArray(@Nullable java.lang.String[] array) throws java.io.IOException
Writes a string array, for nullable string, writes false.- Throws:
java.io.IOException
-
writeMap
public void writeMap(@Nullable java.util.Map<java.lang.String,java.lang.Object> map) throws java.io.IOException
- Throws:
java.io.IOException
-
writeMapWithConsistentOrder
public void writeMapWithConsistentOrder(@Nullable java.util.Map<java.lang.String,? extends java.lang.Object> map) throws java.io.IOException
write 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:
java.io.IOException
-
writeMapOfLists
public final <K,V> void writeMapOfLists(java.util.Map<K,java.util.List<V>> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws java.io.IOException
Write aMap
ofK
-type keys toV
-typeList
s.Map<String, List<String>> map = ...; out.writeMapOfLists(map, StreamOutput::writeString, StreamOutput::writeString);
- Parameters:
keyWriter
- The key writervalueWriter
- The value writer- Throws:
java.io.IOException
-
writeMap
public final <K,V> void writeMap(java.util.Map<K,V> map, Writeable.Writer<K> keyWriter, Writeable.Writer<V> valueWriter) throws java.io.IOException
Write 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:
java.io.IOException
-
writeInstant
public final void writeInstant(java.time.Instant instant) throws java.io.IOException
Writes anInstant
to the stream with nanosecond resolution- Throws:
java.io.IOException
-
writeOptionalInstant
public final void writeOptionalInstant(@Nullable java.time.Instant instant) throws java.io.IOException
Writes anInstant
to the stream, which could possibly be null- Throws:
java.io.IOException
-
writeGenericValue
public void writeGenericValue(@Nullable java.lang.Object value) throws java.io.IOException
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:
java.io.IOException
-
writeIntArray
public void writeIntArray(int[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeVIntArray
public void writeVIntArray(int[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeLongArray
public void writeLongArray(long[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeVLongArray
public void writeVLongArray(long[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeFloatArray
public void writeFloatArray(float[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeDoubleArray
public void writeDoubleArray(double[] values) throws java.io.IOException
- Throws:
java.io.IOException
-
writeArray
public <T> void writeArray(Writeable.Writer<T> writer, T[] array) throws java.io.IOException
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:
java.io.IOException
- if an I/O exception occurs while writing the array
-
writeOptionalArray
public <T> void writeOptionalArray(Writeable.Writer<T> writer, @Nullable T[] array) throws java.io.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:
java.io.IOException
-
writeArray
public <T extends Writeable> void writeArray(T[] array) throws java.io.IOException
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:
java.io.IOException
-
writeOptionalArray
public <T extends Writeable> void writeOptionalArray(@Nullable T[] array) throws java.io.IOException
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:
java.io.IOException
-
writeOptionalStreamable
public void writeOptionalStreamable(@Nullable Streamable streamable) throws java.io.IOException
Serializes a potential null value.- Throws:
java.io.IOException
-
writeOptionalWriteable
public void writeOptionalWriteable(@Nullable Writeable writeable) throws java.io.IOException
- Throws:
java.io.IOException
-
writeException
public void writeException(java.lang.Throwable throwable) throws java.io.IOException
- Throws:
java.io.IOException
-
writeNamedWriteable
public void writeNamedWriteable(NamedWriteable namedWriteable) throws java.io.IOException
Writes aNamedWriteable
to the current stream, by first writing its name and then the object itself- Throws:
java.io.IOException
-
writeOptionalNamedWriteable
public void writeOptionalNamedWriteable(@Nullable NamedWriteable namedWriteable) throws java.io.IOException
Write an optionalNamedWriteable
to the stream.- Throws:
java.io.IOException
-
writeGeoPoint
public void writeGeoPoint(GeoPoint geoPoint) throws java.io.IOException
Writes the givenGeoPoint
to the stream- Throws:
java.io.IOException
-
writeTimeZone
public void writeTimeZone(org.joda.time.DateTimeZone timeZone) throws java.io.IOException
Write a DateTimeZone to the stream.- Throws:
java.io.IOException
-
writeOptionalTimeZone
public void writeOptionalTimeZone(@Nullable org.joda.time.DateTimeZone timeZone) throws java.io.IOException
Write an optional DateTimeZone to the stream.- Throws:
java.io.IOException
-
writeStreamableList
public void writeStreamableList(java.util.List<? extends Streamable> list) throws java.io.IOException
Writes a list ofStreamable
objects- Throws:
java.io.IOException
-
writeCollection
public void writeCollection(java.util.Collection<? extends Writeable> collection) throws java.io.IOException
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:
java.io.IOException
- if an I/O exception occurs writing the collection
-
writeList
public void writeList(java.util.List<? extends Writeable> list) throws java.io.IOException
Writes a list ofWriteable
objects- Throws:
java.io.IOException
-
writeCollection
public <T> void writeCollection(java.util.Collection<T> collection, Writeable.Writer<T> writer) throws java.io.IOException
Writes a collection of objects via aWriteable.Writer
.- Parameters:
collection
- the collection of objects- Throws:
java.io.IOException
- if an I/O exception occurs writing the collection
-
writeStringCollection
public void writeStringCollection(java.util.Collection<java.lang.String> collection) throws java.io.IOException
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:
java.io.IOException
- if an I/O exception occurs writing the collection
-
writeNamedWriteableList
public void writeNamedWriteableList(java.util.List<? extends NamedWriteable> list) throws java.io.IOException
Writes a list ofNamedWriteable
objects.- Throws:
java.io.IOException
-
writeEnum
public <E extends java.lang.Enum<E>> void writeEnum(E enumValue) throws java.io.IOException
Writes an enum with type E that by serialized it based on it's ordinal value- Throws:
java.io.IOException
-
writeEnumSet
public <E extends java.lang.Enum<E>> void writeEnumSet(java.util.EnumSet<E> enumSet) throws java.io.IOException
Writes an EnumSet with type E that by serialized it based on it's ordinal value- Throws:
java.io.IOException
-
writeTimeValue
public void writeTimeValue(TimeValue timeValue) throws java.io.IOException
Write aTimeValue
to the stream- Throws:
java.io.IOException
-
-