Class StreamInput
- java.lang.Object
-
- java.io.InputStream
-
- org.elasticsearch.common.io.stream.StreamInput
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
ByteBufferStreamInput,FilterStreamInput,InputStreamStreamInput
public abstract class StreamInput extends java.io.InputStreamA 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
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, java.util.function.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.org.apache.lucene.util.BytesRefreadBytesRef()org.apache.lucene.util.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 java.lang.Enum<E>>
EreadEnum(java.lang.Class<E> enumClass)Reads an enum with type E that was serialized based on the value of its ordinal<E extends java.lang.Enum<E>>
java.util.EnumSet<E>readEnumSet(java.lang.Class<E> enumClass)Reads an enum with type E that was serialized based on the value of it's ordinal<T extends java.lang.Exception>
TreadException()floatreadFloat()float[]readFloatArray()voidreadFully(byte[] b)java.lang.ObjectreadGenericValue()GeoPointreadGeoPoint()Reads aGeoPointfrom this stream inputjava.time.InstantreadInstant()Read anInstantfrom the stream with nanosecond resolutionintreadInt()Reads four bytes and returns an int.int[]readIntArray()<T> java.util.List<T>readList(Writeable.Reader<T> reader)Reads a list of objects.longreadLong()Reads eight bytes and returns a long.long[]readLongArray()java.util.Map<java.lang.String,java.lang.Object>readMap()<K,V>
java.util.Map<K,V>readMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader)<K,V>
java.util.Map<K,java.util.List<V>>readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader)Read aMapofK-type keys toV-typeLists.<C extends NamedWriteable>
CreadNamedWriteable(java.lang.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(java.lang.Class<C> categoryClass, java.lang.String name)Reads aNamedWriteablefrom the current stream with the given name.<T extends NamedWriteable>
java.util.List<T>readNamedWriteableList(java.lang.Class<T> categoryClass)Reads a list ofNamedWriteables.<T> T[]readOptionalArray(Writeable.Reader<T> reader, java.util.function.IntFunction<T[]> arraySupplier)java.lang.BooleanreadOptionalBoolean()BytesReferencereadOptionalBytesReference()Reads an optional bytes reference from this stream.java.lang.DoublereadOptionalDouble()java.lang.FloatreadOptionalFloat()java.time.InstantreadOptionalInstant()Read an optionalInstantfrom the stream.java.lang.IntegerreadOptionalInt()Reads an optionalInteger.java.lang.LongreadOptionalLong()<C extends NamedWriteable>
CreadOptionalNamedWriteable(java.lang.Class<C> categoryClass)Reads an optionalNamedWriteable.<T extends Streamable>
TreadOptionalStreamable(java.util.function.Supplier<T> supplier)Serializes a potential null value.java.lang.StringreadOptionalString()java.lang.String[]readOptionalStringArray()TextreadOptionalText()TimeValuereadOptionalTimeValue()Read an optionalTimeValuefrom the stream, returning null if no TimeValue was written.org.joda.time.DateTimeZonereadOptionalTimeZone()Read an optional DateTimeZone.java.lang.IntegerreadOptionalVInt()<T extends Writeable>
TreadOptionalWriteable(Writeable.Reader<T> reader)java.time.ZoneIdreadOptionalZoneId()Read an optional ZoneId.<T> java.util.Set<T>readSet(Writeable.Reader<T> reader)Reads a set of objectsshortreadShort()<T extends Streamable>
java.util.List<T>readStreamableList(java.util.function.Supplier<T> constructor)java.lang.StringreadString()java.lang.String[]readStringArray()java.util.List<java.lang.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()java.time.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)
-
-
-
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 java.io.IOExceptionReads and returns a single byte.- Throws:
java.io.IOException
-
readBytes
public abstract void readBytes(byte[] b, int offset, int len) throws java.io.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:
java.io.IOException
-
readBytesReference
public BytesReference readBytesReference() throws java.io.IOException
Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.- Throws:
java.io.IOException
-
readOptionalBytesReference
@Nullable public BytesReference readOptionalBytesReference() throws java.io.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:
java.io.IOException
-
readBytesReference
public BytesReference readBytesReference(int length) throws java.io.IOException
Reads a bytes reference from this stream, might hold an actual reference to the underlying bytes of the stream.- Throws:
java.io.IOException
-
readBytesRef
public org.apache.lucene.util.BytesRef readBytesRef() throws java.io.IOException- Throws:
java.io.IOException
-
readBytesRef
public org.apache.lucene.util.BytesRef readBytesRef(int length) throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b) throws java.io.IOException- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOExceptionReads four bytes and returns an int.- Throws:
java.io.IOException
-
readOptionalInt
public java.lang.Integer readOptionalInt() throws java.io.IOExceptionReads an optionalInteger.- Throws:
java.io.IOException
-
readVInt
public int readVInt() throws java.io.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:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOExceptionReads eight bytes and returns a long.- Throws:
java.io.IOException
-
readVLong
public long readVLong() throws java.io.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:
java.io.IOException
-
readZLong
public long readZLong() throws java.io.IOException- Throws:
java.io.IOException
-
readOptionalLong
@Nullable public java.lang.Long readOptionalLong() throws java.io.IOException
- Throws:
java.io.IOException
-
readOptionalText
@Nullable public Text readOptionalText() throws java.io.IOException
- Throws:
java.io.IOException
-
readText
public Text readText() throws java.io.IOException
- Throws:
java.io.IOException
-
readOptionalString
@Nullable public java.lang.String readOptionalString() throws java.io.IOException
- Throws:
java.io.IOException
-
readOptionalFloat
@Nullable public java.lang.Float readOptionalFloat() throws java.io.IOException
- Throws:
java.io.IOException
-
readOptionalVInt
@Nullable public java.lang.Integer readOptionalVInt() throws java.io.IOException
- Throws:
java.io.IOException
-
readString
public java.lang.String readString() throws java.io.IOException- Throws:
java.io.IOException
-
readFloat
public final float readFloat() throws java.io.IOException- Throws:
java.io.IOException
-
readDouble
public final double readDouble() throws java.io.IOException- Throws:
java.io.IOException
-
readOptionalDouble
@Nullable public final java.lang.Double readOptionalDouble() throws java.io.IOException
- Throws:
java.io.IOException
-
readBoolean
public final boolean readBoolean() throws java.io.IOExceptionReads a boolean.- Throws:
java.io.IOException
-
readOptionalBoolean
@Nullable public final java.lang.Boolean readOptionalBoolean() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public abstract void close() throws java.io.IOExceptionCloses the stream to further operations.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
available
public abstract int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
readStringArray
public java.lang.String[] readStringArray() throws java.io.IOException- Throws:
java.io.IOException
-
readOptionalStringArray
@Nullable public java.lang.String[] readOptionalStringArray() throws java.io.IOException
- Throws:
java.io.IOException
-
readMap
public <K,V> java.util.Map<K,V> readMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws java.io.IOException
- Throws:
java.io.IOException
-
readMapOfLists
public <K,V> java.util.Map<K,java.util.List<V>> readMapOfLists(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) throws java.io.IOException
Read aMapofK-type keys toV-typeLists.Map<String, List<String>> map = in.readMapOfLists(StreamInput::readString, StreamInput::readString);- Parameters:
keyReader- The key readervalueReader- The value reader- Returns:
- Never
null. - Throws:
java.io.IOException
-
readMap
@Nullable public java.util.Map<java.lang.String,java.lang.Object> readMap() throws java.io.IOException
- Throws:
java.io.IOException
-
readGenericValue
@Nullable public java.lang.Object readGenericValue() throws java.io.IOException
- Throws:
java.io.IOException
-
readInstant
public final java.time.Instant readInstant() throws java.io.IOExceptionRead anInstantfrom the stream with nanosecond resolution- Throws:
java.io.IOException
-
readOptionalInstant
@Nullable public final java.time.Instant readOptionalInstant() throws java.io.IOException
Read an optionalInstantfrom the stream. Returnsnullwhen no instant is present.- Throws:
java.io.IOException
-
readGeoPoint
public GeoPoint readGeoPoint() throws java.io.IOException
Reads aGeoPointfrom this stream input- Throws:
java.io.IOException
-
readTimeZone
public org.joda.time.DateTimeZone readTimeZone() throws java.io.IOExceptionRead a DateTimeZone.- Throws:
java.io.IOException
-
readOptionalTimeZone
public org.joda.time.DateTimeZone readOptionalTimeZone() throws java.io.IOExceptionRead an optional DateTimeZone.- Throws:
java.io.IOException
-
readZoneId
public java.time.ZoneId readZoneId() throws java.io.IOExceptionRead a DateTimeZone.- Throws:
java.io.IOException
-
readOptionalZoneId
public java.time.ZoneId readOptionalZoneId() throws java.io.IOExceptionRead an optional ZoneId.- Throws:
java.io.IOException
-
readIntArray
public int[] readIntArray() throws java.io.IOException- Throws:
java.io.IOException
-
readVIntArray
public int[] readVIntArray() throws java.io.IOException- Throws:
java.io.IOException
-
readLongArray
public long[] readLongArray() throws java.io.IOException- Throws:
java.io.IOException
-
readVLongArray
public long[] readVLongArray() throws java.io.IOException- Throws:
java.io.IOException
-
readFloatArray
public float[] readFloatArray() throws java.io.IOException- Throws:
java.io.IOException
-
readDoubleArray
public double[] readDoubleArray() throws java.io.IOException- Throws:
java.io.IOException
-
readByteArray
public byte[] readByteArray() throws java.io.IOException- Throws:
java.io.IOException
-
readArray
public <T> T[] readArray(Writeable.Reader<T> reader, java.util.function.IntFunction<T[]> arraySupplier) throws java.io.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:
java.io.IOException- if an I/O exception occurs while reading the array
-
readOptionalArray
public <T> T[] readOptionalArray(Writeable.Reader<T> reader, java.util.function.IntFunction<T[]> arraySupplier) throws java.io.IOException
- Throws:
java.io.IOException
-
readOptionalStreamable
@Nullable public <T extends Streamable> T readOptionalStreamable(java.util.function.Supplier<T> supplier) throws java.io.IOException
Serializes a potential null value.- Throws:
java.io.IOException
-
readOptionalWriteable
@Nullable public <T extends Writeable> T readOptionalWriteable(Writeable.Reader<T> reader) throws java.io.IOException
- Throws:
java.io.IOException
-
readException
public <T extends java.lang.Exception> T readException() throws java.io.IOException- Throws:
java.io.IOException
-
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(java.lang.Class<C> categoryClass) throws java.io.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:
java.io.IOException
-
readNamedWriteable
@Nullable public <C extends NamedWriteable> C readNamedWriteable(java.lang.Class<C> categoryClass, java.lang.String name) throws java.io.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:
java.io.IOException
-
readOptionalNamedWriteable
@Nullable public <C extends NamedWriteable> C readOptionalNamedWriteable(java.lang.Class<C> categoryClass) throws java.io.IOException
Reads an optionalNamedWriteable.- Throws:
java.io.IOException
-
readStreamableList
public <T extends Streamable> java.util.List<T> readStreamableList(java.util.function.Supplier<T> constructor) throws java.io.IOException
Read aListofStreamableobjects, using theconstructorto instantiate each instance.This is expected to take the form:
List<MyStreamableClass> list = in.readStreamList(MyStreamableClass::new);- Parameters:
constructor- Streamable instance creator- Returns:
- Never
null. - Throws:
java.io.IOException- if any step fails
-
readList
public <T> java.util.List<T> readList(Writeable.Reader<T> reader) throws java.io.IOException
Reads a list of objects. The list is expected to have been written usingStreamOutput.writeList(List)orStreamOutput.writeStreamableList(List).- Returns:
- the list of objects
- Throws:
java.io.IOException- if an I/O exception occurs reading the list
-
readStringList
public java.util.List<java.lang.String> readStringList() throws java.io.IOExceptionReads a list of strings. The list is expected to have been written usingStreamOutput.writeStringCollection(Collection).- Returns:
- the list of strings
- Throws:
java.io.IOException- if an I/O exception occurs reading the list
-
readSet
public <T> java.util.Set<T> readSet(Writeable.Reader<T> reader) throws java.io.IOException
Reads a set of objects- Throws:
java.io.IOException
-
readNamedWriteableList
public <T extends NamedWriteable> java.util.List<T> readNamedWriteableList(java.lang.Class<T> categoryClass) throws java.io.IOException
Reads a list ofNamedWriteables.- Throws:
java.io.IOException
-
readEnum
public <E extends java.lang.Enum<E>> E readEnum(java.lang.Class<E> enumClass) throws java.io.IOExceptionReads an enum with type E that was serialized based on the value of its ordinal- Throws:
java.io.IOException
-
readEnumSet
public <E extends java.lang.Enum<E>> java.util.EnumSet<E> readEnumSet(java.lang.Class<E> enumClass) throws java.io.IOExceptionReads an enum with type E that was serialized based on the value of it's ordinal- Throws:
java.io.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 java.io.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:
java.io.EOFException
-
readTimeValue
public TimeValue readTimeValue() throws java.io.IOException
Read aTimeValuefrom the stream- Throws:
java.io.IOException
-
-