java.lang.Object
java.io.InputStream
org.elasticsearch.common.io.stream.StreamInput
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
ByteArrayStreamInput
,ByteBufferStreamInput
,FilterStreamInput
,InputStreamStreamInput
A stream from this node to another node. Technically, it can also be streamed to 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
StreamOutput
. 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract int
abstract void
close()
Closes the stream to further operations.protected String
doReadString
(int charCount) protected abstract void
ensureCanReadBytes
(int length) This method throws anEOFException
if the given number of bytes can not be read from the stream.The transport version the data is serialized as.Get the registry of named writeables if this stream has one,null
otherwise.Reads all remaining bytes in the stream as a releasable bytes reference.<T> T[]
readArray
(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier) Reads an array from the stream using the specifiedWriteable.Reader
to read array elements from the stream.protected int
Reads a vint viareadVInt()
and applies basic checks to ensure the read array size is sane.final boolean
Reads a boolean.abstract byte
readByte()
Reads and returns a single byte.byte[]
abstract void
readBytes
(byte[] b, int offset, int len) Reads a specified number of bytes into an array at the specified offset.org.apache.lucene.util.BytesRef
org.apache.lucene.util.BytesRef
readBytesRef
(int length) Reads a bytes reference from this stream, copying any bytes read to a newbyte[]
.readBytesReference
(int length) Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.<C> C
readCollection
(IntFunction<C> constructor, CheckedBiConsumer<StreamInput, C, IOException> itemConsumer) Reads a collection which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
, accumulating the results using the provided consumer.<T> List<T>
readCollectionAsImmutableList
(Writeable.Reader<T> reader) Reads a list of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
.<T> Set<T>
readCollectionAsImmutableSet
(Writeable.Reader<T> reader) Reads a set of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
}.<T> List<T>
readCollectionAsList
(Writeable.Reader<T> reader) Reads a list of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
.<T> Set<T>
readCollectionAsSet
(Writeable.Reader<T> reader) Reads a set of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
.final double
double[]
<E extends Enum<E>>
EReads an enum with type E that was serialized based on the value of its ordinalreadEnumSet
(Class<E> enumClass) Reads an enum with type E that was serialized based on the value of it's ordinal<T extends Exception>
Tfinal float
float[]
void
readFully
(byte[] b) If the returned map contains any entries it will be mutable.Reads a value of unspecified type.Reads aGeoPoint
from this stream input<K,
V> Map<K, V> readImmutableMap
(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) Read aMap
using the given key and value readers.readImmutableMap
(Writeable.Reader<V> valueReader) Same asreadMap(Writeable.Reader, Writeable.Reader)
but always reading string keys.<K,
V> ImmutableOpenMap<K, V> readImmutableOpenMap
(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) ReadImmutableOpenMap
using given key and value readers.final Instant
Read anInstant
from the stream with nanosecond resolutionint
readInt()
Reads four bytes and returns an int.int[]
long
readLong()
Reads eight bytes and returns a long.long[]
<K,
V> Map<K, V> readMap
(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) If the returned map contains any entries it will be mutable.readMap
(Writeable.Reader<V> valueReader) Same asreadMap(Writeable.Reader, Writeable.Reader)
but always reading string keys.readMapOfLists
(Writeable.Reader<V> valueReader) <K,
V> Map<K, V> readMapValues
(Writeable.Reader<V> valueReader, Function<V, K> keyMapper) Reads a multipleV
-values and then converts them to aMap
using keyMapper.<C extends NamedWriteable>
CreadNamedWriteable
(Class<C> categoryClass) Reads aNamedWriteable
from the current stream, by first reading its name and then looking for the corresponding entry in the registry by name, so that the proper object can be read and returned.<C extends NamedWriteable>
CreadNamedWriteable
(Class<C> categoryClass, String name) Reads aNamedWriteable
from the current stream with the given name.<T extends NamedWriteable>
List<T>readNamedWriteableCollectionAsList
(Class<T> categoryClass) Reads a list ofNamedWriteable
s which was written usingStreamOutput.writeNamedWriteableCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.NamedWriteable>)
.<T> T[]
readOptionalArray
(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier) final Boolean
byte[]
Reads an optional byte array.Reads an optional bytes reference from this stream.<T> List<T>
readOptionalCollectionAsList
(Writeable.Reader<T> reader) Reads a possibly-null
list which was written usingStreamOutput.writeOptionalCollection(java.util.Collection<T>)
.final Double
<E extends Enum<E>>
EreadOptionalEnum
(Class<E> enumClass) Reads an optional enum with type E that was serialized based on the value of its ordinalfloat[]
Reads an optional float array.final Instant
Read an optionalInstant
from the stream.Reads an optionalInteger
.<C extends NamedWriteable>
CreadOptionalNamedWriteable
(Class<C> categoryClass) Reads an optionalNamedWriteable
.String[]
Reads a possibly-null
list of strings which was written usingStreamOutput.writeOptionalStringCollection(java.util.Collection<java.lang.String>)
.Read an optionalTimeValue
from the stream, returning null if no TimeValue was written.<T extends Writeable>
TreadOptionalWriteable
(Writeable.Reader<T> reader) Read an optional ZoneId.<K,
V> Map<K, V> readOrderedMap
(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) Reads a releasable bytes reference from this stream.short
Reads the same bytes returned byreadReleasableBytesReference()
but does not retain a reference to these bytes.String[]
Reads a list of strings which was written usingStreamOutput.writeStringCollection(java.util.Collection<java.lang.String>)
.Reads a list of strings which was written usingStreamOutput.writeStringCollection(java.util.Collection<java.lang.String>)
.readText()
Read aTimeValue
from the streamint
readVInt()
Reads an int stored in variable-length format.int[]
protected final int
long
Reads a long stored in variable-length format.long[]
protected final long
long
Read a ZoneId.void
setTransportVersion
(TransportVersion version) Set the transport version of the data in this stream.boolean
Checks if thisInputStream
supportsreadAllToReleasableBytesReference()
.protected static void
throwEOF
(int bytesToRead, int bytesAvailable) protected static void
throwOnBrokenVInt
(byte b, int accumulated) protected static void
throwOnBrokenVLong
(byte b, long accumulated) protected static void
throwOnNullRead
(Writeable.Reader<?> reader) protected String
tryReadStringFromBytes
(byte[] bytes, int start, int limit, int chars) static StreamInput
wrap
(byte[] bytes) static StreamInput
wrap
(byte[] bytes, int offset, int length) Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
StreamInput
public StreamInput()
-
-
Method Details
-
getTransportVersion
The transport version the data is serialized as. -
setTransportVersion
Set the transport version of the data in this stream. -
readByte
Reads and returns a single byte.- Throws:
IOException
-
readBytes
Reads a specified number of bytes into an array at the specified offset.- Parameters:
b
- the array to read bytes intooffset
- the offset in the array to start storing byteslen
- the number of bytes to read- Throws:
IOException
-
readBytesReference
Reads a bytes reference from this stream, copying any bytes read to a newbyte[]
. UsereadReleasableBytesReference()
when reading large bytes references where possible top avoid needless allocations and copying.- Throws:
IOException
-
readReleasableBytesReference
Reads a releasable bytes reference from this stream. UnlikereadBytesReference()
the returned bytes reference may reference bytes in a pooled buffer and must be explicitly released viaReleasableBytesReference.close()
once no longer used. Prefer this method overreadBytesReference()
when reading large bytes references to avoid allocations and copying.- Throws:
IOException
-
readSlicedBytesReference
Reads the same bytes returned byreadReleasableBytesReference()
but does not retain a reference to these bytes. The returnedBytesReference
thus only contains valid content as long as the underlying buffer has not been released. This method should be preferred overreadReleasableBytesReference()
when the returned reference is known to not be used past the lifetime of the underlying buffer as it requires fewer allocations and does not require a potentially costly reference count change.- Throws:
IOException
-
supportReadAllToReleasableBytesReference
public boolean supportReadAllToReleasableBytesReference()Checks if thisInputStream
supportsreadAllToReleasableBytesReference()
. -
readAllToReleasableBytesReference
Reads all remaining bytes in the stream as a releasable bytes reference. Similarly toreadReleasableBytesReference()
the returned bytes reference may reference bytes in a pooled buffer and must be explicitly released viaReleasableBytesReference.close()
once no longer used. However, unlikereadReleasableBytesReference()
, this method doesn't have the prefix size.NOTE: Always check
supportReadAllToReleasableBytesReference()
before calling this method.- Throws:
IOException
-
readOptionalBytesReference
Reads an optional bytes reference from this stream. It might hold an actual reference to the underlying bytes of the stream. Use this only if you must differentiate null from empty. UsereadBytesReference()
andStreamOutput.writeBytesReference(BytesReference)
if you do not.- Throws:
IOException
-
readBytesReference
Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.- Throws:
IOException
-
readBytesRef
- Throws:
IOException
-
readBytesRef
- Throws:
IOException
-
readFully
- Throws:
IOException
-
readShort
- Throws:
IOException
-
readInt
Reads four bytes and returns an int.- Throws:
IOException
-
readOptionalInt
Reads an optionalInteger
.- Throws:
IOException
-
readVInt
Reads an int stored in variable-length format. Reads between one and five bytes. Smaller values take fewer bytes. Negative numbers will always use all 5 bytes and are therefore better serialized usingreadInt()
- Throws:
IOException
-
readVIntSlow
- Throws:
IOException
-
throwOnBrokenVInt
- Throws:
IOException
-
readLong
Reads eight bytes and returns a long.- Throws:
IOException
-
readVLong
Reads a long stored in variable-length format. Reads between one and ten bytes. Smaller values take fewer bytes. Negative numbers are encoded in ten bytes so preferreadLong()
orreadZLong()
for negative numbers.- Throws:
IOException
-
readVLongSlow
- Throws:
IOException
-
throwOnBrokenVLong
- Throws:
IOException
-
readOptionalVLong
- Throws:
IOException
-
readZLong
- Throws:
IOException
-
readOptionalLong
- Throws:
IOException
-
readBigInteger
- Throws:
IOException
-
readOptionalText
- Throws:
IOException
-
readText
- Throws:
IOException
-
readOptionalString
- Throws:
IOException
-
readOptionalSecureString
- Throws:
IOException
-
readOptionalFloat
- Throws:
IOException
-
readOptionalVInt
- Throws:
IOException
-
readString
- Throws:
IOException
-
doReadString
- Throws:
IOException
-
tryReadStringFromBytes
protected String tryReadStringFromBytes(byte[] bytes, int start, int limit, int chars) throws IOException - Throws:
IOException
-
readSecureString
- Throws:
IOException
-
readFloat
- Throws:
IOException
-
readDouble
- Throws:
IOException
-
readOptionalDouble
- Throws:
IOException
-
readBoolean
Reads a boolean.- Throws:
IOException
-
readOptionalBoolean
- Throws:
IOException
-
close
Closes the stream to further operations.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
readStringArray
- Throws:
IOException
-
readOptionalStringArray
- Throws:
IOException
-
readOptionalByteArray
Reads an optional byte array. It's effectively the same as readByteArray, except it supports null.- Returns:
- a byte array or null
- Throws:
IOException
-
readOptionalFloatArray
Reads an optional float array. It's effectively the same as readFloatArray, except it supports null.- Returns:
- a float array or null
- Throws:
IOException
-
readMap
Same asreadMap(Writeable.Reader, Writeable.Reader)
but always reading string keys.- Throws:
IOException
-
readMap
public <K,V> Map<K,V> readMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOException If the returned map contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readOrderedMap
public <K,V> Map<K,V> readOrderedMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOException - Throws:
IOException
-
readMapOfLists
Read aMap
of string keys toV
-typeList
s.
If the map or a list in it contains any elements it will be mutable, otherwise either the empty map or empty lists it contains might be immutable.Map<String, List<String>> map = in.readMapOfLists(StreamInput::readString);
- Parameters:
valueReader
- The value reader- Returns:
- Never
null
. - Throws:
IOException
-
readMapValues
public <K,V> Map<K,V> readMapValues(Writeable.Reader<V> valueReader, Function<V, K> keyMapper) throws IOExceptionReads a multipleV
-values and then converts them to aMap
using keyMapper.- Parameters:
valueReader
- The value readerkeyMapper
- function to create a key from a value- Returns:
- Never
null
. - Throws:
IOException
-
readGenericMap
If the returned map contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readImmutableMap
Same asreadMap(Writeable.Reader, Writeable.Reader)
but always reading string keys.- Throws:
IOException
-
readImmutableMap
public <K,V> Map<K,V> readImmutableMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOException Read aMap
using the given key and value readers. The return Map is immutable.- Parameters:
keyReader
- Method to read a key. Must not return null.valueReader
- Method to read a value. Must not return null.- Returns:
- The immutable map
- Throws:
IOException
-
readImmutableOpenMap
public <K,V> ImmutableOpenMap<K,V> readImmutableOpenMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOException ReadImmutableOpenMap
using given key and value readers.- Parameters:
keyReader
- key readervalueReader
- value reader- Throws:
IOException
-
readGenericValue
Reads a value of unspecified type. If a collection is read then the collection will be mutable if it contains any entry but might be immutable if it is empty.- Throws:
IOException
-
readInstant
Read anInstant
from the stream with nanosecond resolution- Throws:
IOException
-
readOptionalInstant
Read an optionalInstant
from the stream. Returnsnull
when no instant is present.- Throws:
IOException
-
readGeoPoint
Reads aGeoPoint
from this stream input- Throws:
IOException
-
readZoneId
Read a ZoneId.- Throws:
IOException
-
readOptionalZoneId
Read an optional ZoneId.- Throws:
IOException
-
readIntArray
- Throws:
IOException
-
readVIntArray
- Throws:
IOException
-
readLongArray
- Throws:
IOException
-
readVLongArray
- Throws:
IOException
-
readFloatArray
- Throws:
IOException
-
readDoubleArray
- Throws:
IOException
-
readByteArray
- Throws:
IOException
-
readArray
public <T> T[] readArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier) throws IOException Reads an array from the stream using the specifiedWriteable.Reader
to read array elements from the stream. This method can be seen as the reader version ofStreamOutput.writeArray(Writeable.Writer, Object[])
. It is assumed that the stream first contains a variable-length integer representing the size of the array, and then contains that many elements that can be read from the stream.- Type Parameters:
T
- the type of the elements of the array- Parameters:
reader
- the reader used to read individual elementsarraySupplier
- a supplier used to construct a new array- Returns:
- an array read from the stream
- Throws:
IOException
- if an I/O exception occurs while reading the array
-
readOptionalArray
public <T> T[] readOptionalArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier) throws IOException - Throws:
IOException
-
readOptionalWriteable
@Nullable public <T extends Writeable> T readOptionalWriteable(Writeable.Reader<T> reader) throws IOException - Throws:
IOException
-
throwOnNullRead
- Throws:
IOException
-
readException
- Throws:
IOException
-
namedWriteableRegistry
Get the registry of named writeables if this stream has one,null
otherwise. -
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(Class<C> categoryClass) throws IOException Reads aNamedWriteable
from the current stream, by first reading its name and then looking for the corresponding entry in the registry by name, so that the proper object can be read and returned. Default implementation throwsUnsupportedOperationException
as StreamInput doesn't hold a registry. UseFilterInputStream
instead which wraps a stream and supports aNamedWriteableRegistry
too.- Throws:
IOException
-
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(Class<C> categoryClass, String name) throws IOException Reads aNamedWriteable
from the current stream with the given name. It is assumed that the caller obtained the name from other source, so it's not read from the stream. The name is used for looking for the corresponding entry in the registry by name, so that the proper object can be read and returned. Default implementation throwsUnsupportedOperationException
as StreamInput doesn't hold a registry. UseFilterInputStream
instead which wraps a stream and supports aNamedWriteableRegistry
too. PreferreadNamedWriteable(Class)
andStreamOutput.writeNamedWriteable(NamedWriteable)
unless you have a compelling reason to use this method instead.- Throws:
IOException
-
readOptionalNamedWriteable
@Nullable public <C extends NamedWriteable> C readOptionalNamedWriteable(Class<C> categoryClass) throws IOException Reads an optionalNamedWriteable
.- Throws:
IOException
-
readCollectionAsList
Reads a list of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
. If the returned list contains any entries it will be a (mutable)ArrayList
. If it is empty it might be immutable.- Throws:
IOException
-
readCollectionAsImmutableList
Reads a list of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
. The returned list is immutable.- Throws:
IOException
-
readStringCollectionAsImmutableList
Reads a list of strings which was written usingStreamOutput.writeStringCollection(java.util.Collection<java.lang.String>)
. The returned list is immutable.- Throws:
IOException
-
readStringCollectionAsList
Reads a list of strings which was written usingStreamOutput.writeStringCollection(java.util.Collection<java.lang.String>)
. If the returned list contains any entries it will be a (mutable)ArrayList
. If it is empty it might be immutable.- Throws:
IOException
-
readOptionalCollectionAsList
@Nullable public <T> List<T> readOptionalCollectionAsList(Writeable.Reader<T> reader) throws IOException Reads a possibly-null
list which was written usingStreamOutput.writeOptionalCollection(java.util.Collection<T>)
. If the returned list contains any entries it will be a (mutable)ArrayList
. If it is empty it might be immutable.- Throws:
IOException
-
readOptionalStringCollectionAsList
Reads a possibly-null
list of strings which was written usingStreamOutput.writeOptionalStringCollection(java.util.Collection<java.lang.String>)
. If the returned list contains any entries it will be a (mutable)ArrayList
. If it is empty it might be immutable.- Throws:
IOException
-
readCollectionAsSet
Reads a set of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
. If the returned set contains any entries it will a (mutable)HashSet
. If it is empty it might be immutable. The collection that was originally written should also have been a set.- Throws:
IOException
-
readCollectionAsImmutableSet
Reads a set of objects which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
}. The returned set is immutable. The collection that was originally written should also have been a set.- Throws:
IOException
-
readNamedWriteableCollectionAsList
public <T extends NamedWriteable> List<T> readNamedWriteableCollectionAsList(Class<T> categoryClass) throws IOException Reads a list ofNamedWriteable
s which was written usingStreamOutput.writeNamedWriteableCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.NamedWriteable>)
. If the returned list contains any entries it will be a (mutable)ArrayList
. If it is empty it might be immutable.- Throws:
IOException
-
readCollection
public <C> C readCollection(IntFunction<C> constructor, CheckedBiConsumer<StreamInput, C, throws IOExceptionIOException> itemConsumer) Reads a collection which was written usingStreamOutput.writeCollection(java.util.Collection<? extends org.elasticsearch.common.io.stream.Writeable>)
, accumulating the results using the provided consumer.- Throws:
IOException
-
readEnum
Reads an enum with type E that was serialized based on the value of its ordinal- Throws:
IOException
-
readOptionalEnum
Reads an optional enum with type E that was serialized based on the value of its ordinal- Throws:
IOException
-
readEnumSet
Reads an enum with type E that was serialized based on the value of it's ordinal- Throws:
IOException
-
wrap
-
wrap
-
readArraySize
Reads a vint viareadVInt()
and applies basic checks to ensure the read array size is sane. This method usesensureCanReadBytes(int)
to ensure this stream has enough bytes to read for the read array size.- Throws:
IOException
-
ensureCanReadBytes
This method throws anEOFException
if the given number of bytes can not be read from the stream. This method might be a no-op depending on the underlying implementation if the information of the remaining bytes is not present.- Throws:
EOFException
-
throwEOF
- Throws:
EOFException
-
readTimeValue
Read aTimeValue
from the stream- Throws:
IOException
-
readOptionalTimeValue
Read an optionalTimeValue
from the stream, returning null if no TimeValue was written.- Throws:
IOException
-