Class StreamInput
java.lang.Object
java.io.InputStream
org.elasticsearch.common.io.stream.StreamInput
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
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
-
Method Summary
Modifier and TypeMethodDescriptionabstract int
abstract void
close()
Closes the stream to further operations.protected abstract void
ensureCanReadBytes(int length)
This method throws anEOFException
if the given number of bytes can not be read from the this stream.The version of the node on the other side of this stream.Get the registry of named writeables if this stream has one,null
otherwise.<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.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.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>
Tfloat
float[]
void
readFully(byte[] b)
Reads a value of unspecified type.Reads aGeoPoint
from this stream input<K, V> ImmutableOpenMap<K,V>
readImmutableMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader)
ReadImmutableOpenMap
using given key and value readers.Read anInstant
from the stream with nanosecond resolutionint
readInt()
Reads four bytes and returns an int.int[]
<T> List<T>
readList(Writeable.Reader<T> reader)
Reads a list of objects.long
readLong()
Reads eight bytes and returns a long.long[]
readMap()
If the returned map contains any entries it will be mutable.<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.readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader)
<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>readNamedWriteableList(Class<T> categoryClass)
Reads a list ofNamedWriteable
s.<T> T[]
readOptionalArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier)
Reads an optional bytes reference from this stream.<E extends Enum<E>>
EreadOptionalEnum(Class<E> enumClass)
Reads an optional enum with type E that was serialized based on the value of its ordinalRead an optionalInstant
from the stream.Reads an optionalInteger
.<C extends NamedWriteable>
CreadOptionalNamedWriteable(Class<C> categoryClass)
Reads an optionalNamedWriteable
.String[]
Reads an optional list of strings.org.elasticsearch.core.TimeValue
Read an optionalTimeValue
from the stream, returning null if no TimeValue was written.org.joda.time.DateTimeZone
Read an optional DateTimeZone.<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.<T> Set<T>
readSet(Writeable.Reader<T> reader)
Reads a set of objects.short
String[]
Reads a list of strings.readText()
org.elasticsearch.core.TimeValue
Read aTimeValue
from the streamorg.joda.time.DateTimeZone
Read a DateTimeZone.int
readVInt()
Reads an int stored in variable-length format.int[]
protected int
long
Reads a long stored in variable-length format.long[]
protected long
long
Read a DateTimeZone.void
setVersion(Version version)
Set the version of the node on the other side of this stream.protected static void
throwOnBrokenVInt(byte b, int accumulated)
protected static void
throwOnBrokenVLong(byte b, long accumulated)
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
-
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. -
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
-
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
-
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
-
readMap
public <K, V> Map<K,V> readMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOExceptionIf 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
public <K, V> Map<K,List<V>> readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOExceptionRead aMap
ofK
-type 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, StreamInput::readString);
- Parameters:
keyReader
- The key readervalueReader
- The value reader- Returns:
- Never
null
. - Throws:
IOException
-
readMap
If the returned map contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readImmutableMap
public <K, V> ImmutableOpenMap<K,V> readImmutableMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOExceptionReadImmutableOpenMap
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
-
readTimeZone
Read a DateTimeZone.- Throws:
IOException
-
readOptionalTimeZone
Read an optional DateTimeZone.- Throws:
IOException
-
readZoneId
Read a DateTimeZone.- 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 IOExceptionReads 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
-
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 IOExceptionReads 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 IOExceptionReads 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 IOExceptionReads an optionalNamedWriteable
.- Throws:
IOException
-
readList
Reads a list of objects. The list is expected to have been written usingStreamOutput.writeList(List)
. If the returned list contains any entries it will be mutable. If it is empty it might be immutable.- Returns:
- the list of objects
- Throws:
IOException
- if an I/O exception occurs reading the list
-
readStringList
Reads a list of strings. The list is expected to have been written usingStreamOutput.writeStringCollection(Collection)
. If the returned list contains any entries it will be mutable. If it is empty it might be immutable.- Returns:
- the list of strings
- Throws:
IOException
- if an I/O exception occurs reading the list
-
readOptionalStringList
Reads an optional list of strings. The list is expected to have been written usingStreamOutput.writeOptionalStringCollection(Collection)
. If the returned list contains any entries it will be mutable. If it is empty it might be immutable.- Returns:
- the list of strings
- Throws:
IOException
- if an I/O exception occurs reading the list
-
readSet
Reads a set of objects. If the returned set contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readNamedWriteableList
public <T extends NamedWriteable> List<T> readNamedWriteableList(Class<T> categoryClass) throws IOExceptionReads a list ofNamedWriteable
s. If the returned list contains any entries it will be mutable. If it is empty it might be immutable.- 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 this 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
-
readTimeValue
Read aTimeValue
from the stream- Throws:
IOException
-
readOptionalTimeValue
Read an optionalTimeValue
from the stream, returning null if no TimeValue was written.- Throws:
IOException
-