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
public abstract class StreamInput extends InputStream
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 toStreamOutput. 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 withLists rather than Arrays. For the most part calls should adapt to lists, either by storingLists internally or just converting to and from aListwhen calling. This comment is repeated onStreamInput.
-
-
Constructor Summary
Constructors Constructor Description StreamInput()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract intavailable()abstract voidclose()Closes the stream to further operations.protected abstract voidensureCanReadBytes(int length)This method throws anEOFExceptionif the given number of bytes can not be read from the this stream.VersiongetVersion()The version of the node on the other side of this stream.<T> T[]readArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier)Reads an array from the stream using the specifiedWriteable.Readerto read array elements from the stream.booleanreadBoolean()Reads a boolean.abstract bytereadByte()Reads and returns a single byte.byte[]readByteArray()abstract voidreadBytes(byte[] b, int offset, int len)Reads a specified number of bytes into an array at the specified offset.BytesRefreadBytesRef()BytesRefreadBytesRef(int length)BytesReferencereadBytesReference()Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.BytesReferencereadBytesReference(int length)Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.doublereadDouble()double[]readDoubleArray()<E extends Enum<E>>
EreadEnum(Class<E> enumClass)Reads an enum with type E that was serialized based on the value of its ordinal<E extends Enum<E>>
EnumSet<E>readEnumSet(Class<E> enumClass)Reads an enum with type E that was serialized based on the value of it's ordinal<T extends Exception>
TreadException()floatreadFloat()float[]readFloatArray()voidreadFully(byte[] b)ObjectreadGenericValue()Reads a value of unspecified type.GeoPointreadGeoPoint()Reads aGeoPointfrom this stream inputInstantreadInstant()Read anInstantfrom the stream with nanosecond resolutionintreadInt()Reads four bytes and returns an int.int[]readIntArray()<T> List<T>readList(Writeable.Reader<T> reader)Reads a list of objects.longreadLong()Reads eight bytes and returns a long.long[]readLongArray()Map<String,Object>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.<K,V>
Map<K,List<V>>readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader)<C extends NamedWriteable>
CreadNamedWriteable(Class<C> categoryClass)Reads aNamedWriteablefrom 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 aNamedWriteablefrom the current stream with the given name.<T extends NamedWriteable>
List<T>readNamedWriteableList(Class<T> categoryClass)Reads a list ofNamedWriteables.<T> T[]readOptionalArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier)BooleanreadOptionalBoolean()BytesReferencereadOptionalBytesReference()Reads an optional bytes reference from this stream.DoublereadOptionalDouble()FloatreadOptionalFloat()InstantreadOptionalInstant()Read an optionalInstantfrom the stream.IntegerreadOptionalInt()Reads an optionalInteger.LongreadOptionalLong()<C extends NamedWriteable>
CreadOptionalNamedWriteable(Class<C> categoryClass)Reads an optionalNamedWriteable.SecureStringreadOptionalSecureString()StringreadOptionalString()String[]readOptionalStringArray()TextreadOptionalText()TimeValuereadOptionalTimeValue()Read an optionalTimeValuefrom the stream, returning null if no TimeValue was written.org.joda.time.DateTimeZonereadOptionalTimeZone()Read an optional DateTimeZone.IntegerreadOptionalVInt()<T extends Writeable>
TreadOptionalWriteable(Writeable.Reader<T> reader)ZoneIdreadOptionalZoneId()Read an optional ZoneId.SecureStringreadSecureString()<T> Set<T>readSet(Writeable.Reader<T> reader)Reads a set of objects.shortreadShort()StringreadString()String[]readStringArray()List<String>readStringList()Reads a list of strings.TextreadText()TimeValuereadTimeValue()Read aTimeValuefrom the streamorg.joda.time.DateTimeZonereadTimeZone()Read a DateTimeZone.intreadVInt()Reads an int stored in variable-length format.int[]readVIntArray()longreadVLong()Reads a long stored in variable-length format.long[]readVLongArray()longreadZLong()ZoneIdreadZoneId()Read a DateTimeZone.voidsetVersion(Version version)Set the version of the node on the other side of this stream.static StreamInputwrap(byte[] bytes)static StreamInputwrap(byte[] bytes, int offset, int length)-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
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.
-
readByte
public abstract byte readByte() throws IOExceptionReads and returns a single byte.- Throws:
IOException
-
readBytes
public abstract void readBytes(byte[] b, int offset, int len) throws IOExceptionReads 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
public BytesReference readBytesReference() throws IOException
Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.- Throws:
IOException
-
readOptionalBytesReference
@Nullable public BytesReference readOptionalBytesReference() throws IOException
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
public BytesReference readBytesReference(int length) throws IOException
Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.- Throws:
IOException
-
readBytesRef
public BytesRef readBytesRef() throws IOException
- Throws:
IOException
-
readBytesRef
public BytesRef readBytesRef(int length) throws IOException
- Throws:
IOException
-
readFully
public void readFully(byte[] b) throws IOException- Throws:
IOException
-
readShort
public short readShort() throws IOException- Throws:
IOException
-
readInt
public int readInt() throws IOExceptionReads four bytes and returns an int.- Throws:
IOException
-
readOptionalInt
public Integer readOptionalInt() throws IOException
Reads an optionalInteger.- Throws:
IOException
-
readVInt
public int readVInt() throws IOExceptionReads 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
-
readLong
public long readLong() throws IOExceptionReads eight bytes and returns a long.- Throws:
IOException
-
readVLong
public long readVLong() throws IOExceptionReads 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
-
readZLong
public long readZLong() throws IOException- Throws:
IOException
-
readOptionalLong
@Nullable public Long readOptionalLong() throws IOException
- Throws:
IOException
-
readOptionalText
@Nullable public Text readOptionalText() throws IOException
- Throws:
IOException
-
readText
public Text readText() throws IOException
- Throws:
IOException
-
readOptionalString
@Nullable public String readOptionalString() throws IOException
- Throws:
IOException
-
readOptionalSecureString
@Nullable public SecureString readOptionalSecureString() throws IOException
- Throws:
IOException
-
readOptionalFloat
@Nullable public Float readOptionalFloat() throws IOException
- Throws:
IOException
-
readOptionalVInt
@Nullable public Integer readOptionalVInt() throws IOException
- Throws:
IOException
-
readString
public String readString() throws IOException
- Throws:
IOException
-
readSecureString
public SecureString readSecureString() throws IOException
- Throws:
IOException
-
readFloat
public final float readFloat() throws IOException- Throws:
IOException
-
readDouble
public final double readDouble() throws IOException- Throws:
IOException
-
readOptionalDouble
@Nullable public final Double readOptionalDouble() throws IOException
- Throws:
IOException
-
readBoolean
public final boolean readBoolean() throws IOExceptionReads a boolean.- Throws:
IOException
-
readOptionalBoolean
@Nullable public final Boolean readOptionalBoolean() throws IOException
- Throws:
IOException
-
close
public abstract void close() throws IOExceptionCloses the stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
available
public abstract int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
readStringArray
public String[] readStringArray() throws IOException
- Throws:
IOException
-
readOptionalStringArray
@Nullable public String[] readOptionalStringArray() throws IOException
- 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
-
readMapOfLists
public <K,V> Map<K,List<V>> readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws IOException
Read aMapofK-type keys toV-typeLists.
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
@Nullable public Map<String,Object> readMap() throws IOException
If the returned map contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readGenericValue
@Nullable public Object readGenericValue() throws IOException
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
public final Instant readInstant() throws IOException
Read anInstantfrom the stream with nanosecond resolution- Throws:
IOException
-
readOptionalInstant
@Nullable public final Instant readOptionalInstant() throws IOException
Read an optionalInstantfrom the stream. Returnsnullwhen no instant is present.- Throws:
IOException
-
readGeoPoint
public GeoPoint readGeoPoint() throws IOException
Reads aGeoPointfrom this stream input- Throws:
IOException
-
readTimeZone
public org.joda.time.DateTimeZone readTimeZone() throws IOExceptionRead a DateTimeZone.- Throws:
IOException
-
readOptionalTimeZone
public org.joda.time.DateTimeZone readOptionalTimeZone() throws IOExceptionRead an optional DateTimeZone.- Throws:
IOException
-
readZoneId
public ZoneId readZoneId() throws IOException
Read a DateTimeZone.- Throws:
IOException
-
readOptionalZoneId
public ZoneId readOptionalZoneId() throws IOException
Read an optional ZoneId.- Throws:
IOException
-
readIntArray
public int[] readIntArray() throws IOException- Throws:
IOException
-
readVIntArray
public int[] readVIntArray() throws IOException- Throws:
IOException
-
readLongArray
public long[] readLongArray() throws IOException- Throws:
IOException
-
readVLongArray
public long[] readVLongArray() throws IOException- Throws:
IOException
-
readFloatArray
public float[] readFloatArray() throws IOException- Throws:
IOException
-
readDoubleArray
public double[] readDoubleArray() throws IOException- Throws:
IOException
-
readByteArray
public byte[] readByteArray() throws IOException- 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.Readerto 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
public <T extends Exception> T readException() throws IOException
- Throws:
IOException
-
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(Class<C> categoryClass) throws IOException
Reads aNamedWriteablefrom 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 throwsUnsupportedOperationExceptionas StreamInput doesn't hold a registry. UseFilterInputStreaminstead which wraps a stream and supports aNamedWriteableRegistrytoo.- Throws:
IOException
-
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(Class<C> categoryClass, String name) throws IOException
Reads aNamedWriteablefrom 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 throwsUnsupportedOperationExceptionas StreamInput doesn't hold a registry. UseFilterInputStreaminstead which wraps a stream and supports aNamedWriteableRegistrytoo. 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
-
readList
public <T> List<T> readList(Writeable.Reader<T> reader) throws IOException
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
public List<String> readStringList() throws IOException
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
-
readSet
public <T> Set<T> readSet(Writeable.Reader<T> reader) throws IOException
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 IOException
Reads a list ofNamedWriteables. If the returned list contains any entries it will be mutable. If it is empty it might be immutable.- Throws:
IOException
-
readEnum
public <E extends Enum<E>> E readEnum(Class<E> enumClass) throws IOException
Reads an enum with type E that was serialized based on the value of its ordinal- Throws:
IOException
-
readEnumSet
public <E extends Enum<E>> EnumSet<E> readEnumSet(Class<E> enumClass) throws IOException
Reads an enum with type E that was serialized based on the value of it's ordinal- Throws:
IOException
-
wrap
public static StreamInput wrap(byte[] bytes)
-
wrap
public static StreamInput wrap(byte[] bytes, int offset, int length)
-
ensureCanReadBytes
protected abstract void ensureCanReadBytes(int length) throws EOFExceptionThis method throws anEOFExceptionif 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
public TimeValue readTimeValue() throws IOException
Read aTimeValuefrom the stream- Throws:
IOException
-
readOptionalTimeValue
@Nullable public TimeValue readOptionalTimeValue() throws IOException
Read an optionalTimeValuefrom the stream, returning null if no TimeValue was written.- Throws:
IOException
-
-