Interface MessageReader
- All Superinterfaces:
AutoCloseable,Closeable
Reads values encoded in MessagePack from a channel, input stream, byte buffer, or MessageSink.Provider.
To create a new message reader, call of(java.nio.channels.ReadableByteChannel).
To read values, call the various readXxx methods.
To determine the next value's type, call nextType().
To close a message reader, call close().
Continued use of a closed message reader can result in data corruption.
For usage examples, see Reading data.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA builder of message reader options. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this message reader.nextType()Reads the next value's type without consuming it.static MessageReaderof(InputStream stream) Returns a new message reader that reads from the given input stream.static MessageReaderof(InputStream stream, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given input stream.static MessageReaderof(ByteBuffer buffer) Returns a new message reader that reads from the given byte buffer.static MessageReaderof(ByteBuffer buffer, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given byte buffer.static MessageReaderof(ReadableByteChannel channel) Returns a new message reader that reads from the given channel.static MessageReaderof(ReadableByteChannel channel, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given channel.static MessageReaderof(LeasedByteBuffer buffer) Returns a new message reader that reads from the given byte buffer.static MessageReaderof(LeasedByteBuffer buffer, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given byte buffer.static MessageReaderof(MessageSource.Provider provider) Returns a new message reader that reads from the given source provider.static MessageReaderof(MessageSource.Provider provider, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given source provider.static MessageReaderofEmpty()Returns a new message reader that will throwEOFExceptionwhen a value is read.<T> Tread(MessageDecoder<T> decoder) Reads and decodes a value.intStarts reading an array value.intStarts reading a binary value.booleanReads a boolean value.bytereadByte()Reads an integer value that fits intobyte.doubleReads a floating point value.Starts reading an extension value.floatReads a floating point value that fits intofloat.Reads a string value that is used as identifier.intreadInt()Reads an integer value that fits intoint.longreadLong()Reads an integer value.intStarts reading a map value.voidreadNil()Reads a nil (null) value.longreadPayload(OutputStream stream, long length) Reads up tolengthbytes into the given output stream.voidreadPayload(ByteBuffer buffer) Reads up toBuffer.remaining()bytes into the given byte buffer.longreadPayload(WritableByteChannel channel, long length) Reads up tolengthbytes into the given channel.shortReads an integer value that fits intoshort.Reads a string value.intStarts reading a string value as a sequence of bytes.Reads a timestamp value.byteReads an integer value that fits intobyteinterpreted as unsigned value.intreadUInt()Reads an integer value that fits intointinterpreted as unsigned value.longReads an integer value that fits into alonginterpreted as unsigned value.shortReads an integer value that fits intoshortinterpreted as unsigned value.voidSkips the next value.voidskipValue(int count) Skips the nextcountvalues.
-
Method Details
-
of
Returns a new message reader that reads from the given channel.- Parameters:
channel- the channel to read from- Returns:
- a new message reader that reads from the given channel
-
of
static MessageReader of(ReadableByteChannel channel, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given channel.- Parameters:
channel- the channel to read fromoptionHandler- a handler that receives anMessageReader.OptionBuilder- Returns:
- a new message reader with the given options that reads from the given channel
-
of
Returns a new message reader that reads from the given input stream.- Parameters:
stream- the input stream to read from- Returns:
- a new message reader that reads from the given input stream
-
of
Returns a new message reader with the given options that reads from the given input stream.- Parameters:
stream- the input stream to read fromoptionHandler- a handler that receives anMessageReader.OptionBuilder- Returns:
- a new message reader with the given options that reads from the given input stream
-
of
Returns a new message reader that reads from the given byte buffer.- Parameters:
buffer- the byte buffer to read from- Returns:
- a new message reader that reads from the given byte buffer
-
of
static MessageReader of(LeasedByteBuffer buffer, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given byte buffer.- Parameters:
buffer- the byte buffer to read fromoptionHandler- a handler that receives anMessageReader.OptionBuilder- Returns:
- a new message reader with the given options that reads from the given byte buffer
-
of
Returns a new message reader that reads from the given byte buffer.- Parameters:
buffer- the byte buffer to read from- Returns:
- a new message reader that reads from the given byte buffer
-
of
Returns a new message reader with the given options that reads from the given byte buffer.- Parameters:
buffer- the byte buffer to read fromoptionHandler- a handler that receives anMessageReader.OptionBuilder- Returns:
- a new message reader with the given options that reads from the given byte buffer
-
of
Returns a new message reader that reads from the given source provider.- Parameters:
provider- the source provider to read from- Returns:
- a new message reader that reads from the given source provider
-
of
static MessageReader of(MessageSource.Provider provider, Consumer<MessageReader.OptionBuilder> optionHandler) Returns a new message reader with the given options that reads from the given source provider.- Parameters:
provider- the source provider to read fromoptionHandler- a handler that receives anMessageReader.OptionBuilder- Returns:
- a new message reader with the given options that reads from the given source provider
-
ofEmpty
Returns a new message reader that will throwEOFExceptionwhen a value is read.- Returns:
- a new message reader that will throw
EOFExceptionwhen a value is read
-
nextType
Reads the next value's type without consuming it.
Consecutive calls to this method will return the same result.
- Returns:
- the next value's type
- Throws:
EOFException- if the end of input is reached before the type has been readIOException- if an I/O error occurs
-
skipValue
Skips the next value.- Throws:
EOFException- if the end of input is reached before the value has been skippedIOException- if an I/O error occurs
-
skipValue
Skips the nextcountvalues.- Parameters:
count- the number of values to skip- Throws:
EOFException- if the end of input is reached before the values have been skippedIOException- if an I/O error occurs
-
readNil
Reads a nil (null) value.- Throws:
EOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readBoolean
Reads a boolean value.- Returns:
- the boolean value read
- Throws:
MxPackException.TypeMismatch- if the value read is not a boolean valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readByte
Reads an integer value that fits intobyte.- Returns:
- the integer value read
- Throws:
MxPackException.TypeMismatch- if the value read is not an integer value or does not fit intobyteEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readShort
Reads an integer value that fits intoshort.- Returns:
- the integer value read
- Throws:
MxPackException.TypeMismatch- if the value read is not an integer value or does not fit intoshortEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readInt
Reads an integer value that fits intoint.- Returns:
- the integer value read
- Throws:
MxPackException.TypeMismatch- if the value read is not an integer value or does not fit intointEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readLong
Reads an integer value.- Returns:
- the integer value read
- Throws:
MxPackException.TypeMismatch- if the value read is not an integer valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readFloat
Reads a floating point value that fits intofloat.- Returns:
- the floating point value read
- Throws:
MxPackException.TypeMismatch- if the value read is not a floating point value or does not fit intofloatEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readDouble
Reads a floating point value.- Returns:
- the floating point value read
- Throws:
MxPackException.TypeMismatch- if the value read is not a floating point valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readTimestamp
Reads a timestamp value.- Returns:
- the timestamp value read
- Throws:
MxPackException.TypeMismatch- if the value read is not a timestamp valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readString
Reads a string value.
To read a string value as a sequence of bytes, call
readStringHeader()followed byreadPayload(java.nio.channels.WritableByteChannel, long).- Returns:
- the string value read
- Throws:
MxPackException.TypeMismatch- if the value read is not a string valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readIdentifier
Reads a string value that is used as identifier.
Calling this method has the same effect as calling
readString()except that it indicates to this message reader that it may want to cache the returned value.- Returns:
- the identifier read
- Throws:
MxPackException.TypeMismatch- if the read value is not a string valueEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
read
Reads and decodes a value.- Type Parameters:
T- the type of value to decode- Parameters:
decoder- the decoder to use- Returns:
- the read and decoded value
- Throws:
MxPackException.TypeMismatch- if the value read cannot be decoded with the given decoderEOFException- if the end of input is reached before the value has been readIOException- if an I/O error occurs
-
readArrayHeader
Starts reading an array value.
A call to this method, returning
n, must be followed bynreadXxxcalls that read the array's elements.- Returns:
- the number of elements in the array
- Throws:
IOException- if an I/O error occurs
-
readMapHeader
Starts reading a map value.
A call to this method, returning
n, must be followed byn * 2readXxxcalls that alternately read the map's keys and values.- Returns:
- the number of entries in the map
- Throws:
IOException- if an I/O error occurs
-
readBinaryHeader
Starts reading a binary value.
A call to this method, returning
n, must be immediately followed by calls toreadPayload(java.nio.channels.WritableByteChannel, long)that readnbytes in total.- Returns:
- the length, in bytes, of the binary value
- Throws:
IOException- if an I/O error occurs
-
readStringHeader
Starts reading a string value as a sequence of bytes.
A call to this method, returning
n, must be immediately followed by calls toreadPayload(java.nio.channels.WritableByteChannel, long)that readnbytes in total.This method is a low-level alternative to
readString().- Returns:
- the length, in bytes, of the string
- Throws:
IOException- if an I/O error occurs
-
readExtensionHeader
Starts reading an extension value.
A call to this method, returning an extension header with length
n, must be immediately followed by calls toreadPayload(java.nio.channels.WritableByteChannel, long)that readnbytes in total.- Returns:
- the header of the extension value
- Throws:
IOException- if an I/O error occurs
-
readPayload
Reads up to
lengthbytes into the given channel.Fewer than
lengthbytes may be read if this reader reaches the end of input.- Parameters:
channel- the channel to write tolength- the number of bytes to read- Returns:
- the actual number of bytes read
- Throws:
IOException- if an I/O error occurs
-
readPayload
Reads up to
lengthbytes into the given output stream.Fewer than
lengthbytes may be read if this reader reaches the end of input.- Parameters:
stream- the output stream to write tolength- the number of bytes to read- Returns:
- the actual number of bytes read
- Throws:
IOException- if an I/O error occursIOException- if an I/O error occurs
-
readPayload
Reads up to
Buffer.remaining()bytes into the given byte buffer.Fewer than
Buffer.remaining()bytes may be read if this reader reaches the end of input.- Parameters:
buffer- the byte buffer to read into- Throws:
IOException- if an I/O error occurs
-
readUByte
Reads an integer value that fits intobyteinterpreted as unsigned value.- Returns:
- the integer value read
- Throws:
IOException- if an I/O error occurs
-
readUShort
Reads an integer value that fits intoshortinterpreted as unsigned value.- Returns:
- the integer value read
- Throws:
IOException- if an I/O error occurs
-
readUInt
Reads an integer value that fits intointinterpreted as unsigned value.- Returns:
- the integer value read
- Throws:
IOException- if an I/O error occurs
-
readULong
Reads an integer value that fits into alonginterpreted as unsigned value.- Returns:
- the integer value read
- Throws:
IOException- if an I/O error occurs
-
close
Closes this message reader.
If this reader was created with
of(ReadableByteChannel), this method callsChannel.close(). If this reader was created withof(InputStream), this method callsInputStream.close(). If this reader was created withof(LeasedByteBuffer), this method callsLeasedByteBuffer.close(). If this reader was created withof(MessageSource.Provider), this method callsMessageSource.Provider.close().Subsequent calls to this method have no effect. Continued use of a closed message reader can result in data corruption and may throw
IllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs
-