public class MessageUnpacker
extends java.lang.Object
implements java.io.Closeable
MessagePack class or MessagePack.UnpackerConfig class to create
an instance.
To read values as statically-typed Java objects, there are two typical use cases.
One use case is to read objects as Value using unpackValue() method. A Value object
contains type of the deserialized value as well as the value itself so that you can inspect type of the
deserialized values later. You can repeat unpackValue() until hasNext() method returns false so
that you can deserialize sequence of MessagePack values.
The other use case is to use getNextFormat() and MessageFormat.getValueType() methods followed
by unpackXxx methods corresponding to returned type. Following code snipet is a typical application code:
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(...);
while(unpacker.hasNext()) {
MessageFormat format = unpacker.getNextFormat();
ValueType type = format.getValueType();
int length;
ExtensionTypeHeader extension;
switch(type) {
case NIL:
unpacker.unpackNil();
break;
case BOOLEAN:
unpacker.unpackBoolean();
break;
case INTEGER:
switch (format) {
case UINT64:
unpacker.unpackBigInteger();
break;
case INT64:
case UINT32:
unpacker.unpackLong();
break;
default:
unpacker.unpackInt();
break;
}
break;
case FLOAT:
unpacker.unpackDouble();
break;
case STRING:
unpacker.unpackString();
break;
case BINARY:
length = unpacker.unpackBinaryHeader();
unpacker.readPayload(new byte[length]);
break;
case ARRAY:
length = unpacker.unpackArrayHeader();
for (int i = 0; i < length; i++) {
readRecursively(unpacker);
}
break;
case MAP:
length = unpacker.unpackMapHeader();
for (int i = 0; i < length; i++) {
readRecursively(unpacker); // key
readRecursively(unpacker); // value
}
break;
case EXTENSION:
extension = unpacker.unpackExtensionTypeHeader();
unpacker.readPayload(new byte[extension.getLength()]);
break;
}
}
}
Following methods correspond to the MessagePack types:
MessagePack type Unpacker method Java type
Nil unpackNil()null
Boolean unpackBoolean()boolean
Integer unpackByte()byte
Integer unpackShort()short
Integer unpackInt()int
Integer unpackLong()long
Integer unpackBigInteger()BigInteger
Float unpackFloat()float
Float unpackDouble()double
Binary unpackBinaryHeader()byte array
String unpackRawStringHeader()String
String unpackString()String
Array unpackArrayHeader()Array
Map unpackMapHeader()Map
Extension unpackExtensionTypeHeader()ExtensionTypeHeader
To read a byte array, first call unpackBinaryHeader() method to get length of the byte array. Then,
call readPayload(int) or readPayloadAsReference(int) method to read the the contents.
To read an Array type, first call unpackArrayHeader() method to get number of elements. Then,
call unpacker methods for each element.
To read a Map, first call unpackMapHeader() method to get number of pairs of the map. Then,
for each pair, call unpacker methods for key first, and then value. will call unpacker methods twice
as many time as the returned count.
| Modifier and Type | Field and Description |
|---|---|
private java.nio.charset.CodingErrorAction |
actionOnMalformedString |
private java.nio.charset.CodingErrorAction |
actionOnUnmappableString |
private boolean |
allowReadingBinaryAsString |
private boolean |
allowReadingStringAsBinary |
private MessageBuffer |
buffer
Points to the current buffer to read
|
private java.nio.CharBuffer |
decodeBuffer
Buffer for decoding strings
|
private java.nio.charset.CharsetDecoder |
decoder
For decoding String in unpackString.
|
private java.lang.StringBuilder |
decodeStringBuffer
For decoding String in unpackString.
|
private static MessageBuffer |
EMPTY_BUFFER |
private static java.lang.String |
EMPTY_STRING |
private MessageBufferInput |
in |
private int |
nextReadPosition
After calling prepareNumberBuffer(), the caller should use this variable to read from the returned MessageBuffer.
|
private MessageBuffer |
numberBuffer
An extra buffer for reading a small number value across the input buffer boundary.
|
private int |
position
Cursor position in the current buffer
|
private int |
stringDecoderBufferSize |
private int |
stringSizeLimit |
private long |
totalReadBytes
Total read byte size
|
| Modifier | Constructor and Description |
|---|---|
protected |
MessageUnpacker(MessageBufferInput in,
MessagePack.UnpackerConfig config)
Create an MessageUnpacker that reads data from the given MessageBufferInput.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes underlying input.
|
private java.lang.String |
decodeStringFastPath(int length) |
private boolean |
ensureBuffer() |
private MessageBuffer |
getNextBuffer()
Get the next buffer without changing the position
|
MessageFormat |
getNextFormat()
Returns format of the next value.
|
long |
getTotalReadBytes()
Returns total number of read bytes.
|
private void |
handleCoderError(java.nio.charset.CoderResult cr) |
boolean |
hasNext()
Returns true if this unpacker has more elements.
|
private void |
nextBuffer() |
private static MessageIntegerOverflowException |
overflowI16(short i16) |
private static MessageIntegerOverflowException |
overflowI32(int i32) |
private static MessageIntegerOverflowException |
overflowI64(long i64) |
private static MessageIntegerOverflowException |
overflowU16(short u16) |
private static MessageIntegerOverflowException |
overflowU32(int u32) |
private static MessageSizeException |
overflowU32Size(int u32) |
private static MessageIntegerOverflowException |
overflowU64(long u64) |
private static MessageIntegerOverflowException |
overflowU8(byte u8) |
private MessageBuffer |
prepareNumberBuffer(int readLength)
Returns a short size buffer (upto 8 bytes) to read a number value
|
private byte |
readByte()
Read a byte value at the cursor and proceed the cursor.
|
private double |
readDouble() |
private float |
readFloat() |
private int |
readInt() |
private long |
readLong() |
private int |
readNextLength16() |
private int |
readNextLength32() |
private int |
readNextLength8() |
void |
readPayload(byte[] dst)
Reads payload bytes of binary, extension, or raw string types.
|
void |
readPayload(byte[] dst,
int off,
int len)
Reads payload bytes of binary, extension, or raw string types.
|
void |
readPayload(java.nio.ByteBuffer dst)
Reads payload bytes of binary, extension, or raw string types.
|
byte[] |
readPayload(int length)
Reads payload bytes of binary, extension, or raw string types.
|
void |
readPayload(MessageBuffer dst,
int off,
int len)
Reads payload bytes of binary, extension, or raw string types.
|
MessageBuffer |
readPayloadAsReference(int length)
Reads payload bytes of binary, extension, or raw string types as a reference to internal buffer.
|
private short |
readShort() |
MessageBufferInput |
reset(MessageBufferInput in)
Replaces underlying input.
|
private void |
resetDecoder() |
private void |
skipPayload(int numBytes)
Skip reading the specified number of bytes.
|
void |
skipValue()
Skip the next value, then move the cursor at the end of the value
|
void |
skipValue(int count)
Skip next values, then move the cursor at the end of the value
|
private int |
tryReadBinaryHeader(byte b) |
private int |
tryReadStringHeader(byte b) |
boolean |
tryUnpackNil()
Peeks a Nil byte and reads it if next byte is a nil value.
|
private static MessagePackException |
unexpected(java.lang.String expected,
byte b)
Create an exception for the case when an unexpected byte value is read
|
private static MessagePackException |
unexpectedExtension(java.lang.String expected,
int expectedType,
int actualType) |
int |
unpackArrayHeader()
Reads header of an array.
|
java.math.BigInteger |
unpackBigInteger()
Reads a BigInteger.
|
int |
unpackBinaryHeader()
Reads header of a binary.
|
boolean |
unpackBoolean()
Reads true or false.
|
byte |
unpackByte()
Reads a byte.
|
double |
unpackDouble()
Reads a double.
|
ExtensionTypeHeader |
unpackExtensionTypeHeader() |
float |
unpackFloat()
Reads a float.
|
int |
unpackInt()
Reads a int.
|
long |
unpackLong()
Reads a long.
|
int |
unpackMapHeader()
Reads header of a map.
|
void |
unpackNil()
Reads a Nil byte.
|
int |
unpackRawStringHeader() |
short |
unpackShort()
Reads a short.
|
java.lang.String |
unpackString() |
java.time.Instant |
unpackTimestamp() |
java.time.Instant |
unpackTimestamp(ExtensionTypeHeader ext)
Unpack timestamp that can be used after reading the extension type header with unpackExtensionTypeHeader.
|
ImmutableValue |
unpackValue() |
Variable |
unpackValue(Variable var) |
private static int |
utf8MultibyteCharacterSize(byte firstByte) |
private static final MessageBuffer EMPTY_BUFFER
private final boolean allowReadingStringAsBinary
private final boolean allowReadingBinaryAsString
private final java.nio.charset.CodingErrorAction actionOnMalformedString
private final java.nio.charset.CodingErrorAction actionOnUnmappableString
private final int stringSizeLimit
private final int stringDecoderBufferSize
private MessageBufferInput in
private MessageBuffer buffer
private int position
private long totalReadBytes
private final MessageBuffer numberBuffer
private int nextReadPosition
private java.lang.StringBuilder decodeStringBuffer
private java.nio.charset.CharsetDecoder decoder
private java.nio.CharBuffer decodeBuffer
private static final java.lang.String EMPTY_STRING
protected MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig config)
in - public MessageBufferInput reset(MessageBufferInput in) throws java.io.IOException
This method clears internal buffer, swaps the underlying input with the new given input, then returns the old input.
This method doesn't close the old input.
in - new inputjava.io.IOException - never happens unless a subclass overrides this methodjava.lang.NullPointerException - the given input is nullpublic long getTotalReadBytes()
This method returns total of amount of data consumed from the underlying input minus size of data remained still unused in the current internal buffer.
Calling reset(MessageBufferInput) resets this number to 0.
private MessageBuffer getNextBuffer() throws java.io.IOException
java.io.IOExceptionprivate void nextBuffer()
throws java.io.IOException
java.io.IOExceptionprivate MessageBuffer prepareNumberBuffer(int readLength) throws java.io.IOException
readLength - java.io.IOExceptionMessageInsufficientBufferException - If no more buffer can be acquired from the input source for reading the specified data lengthprivate static int utf8MultibyteCharacterSize(byte firstByte)
public boolean hasNext()
throws java.io.IOException
getNextFormat() returns an
MessageFormat instance. If false, next getNextFormat() call will throw an MessageInsufficientBufferException.java.io.IOExceptionprivate boolean ensureBuffer()
throws java.io.IOException
java.io.IOExceptionpublic MessageFormat getNextFormat() throws java.io.IOException
Note that this method doesn't consume data from the internal buffer unlike the other unpack methods. Calling this method twice will return the same value.
To not throw MessageInsufficientBufferException, this method should be called only when
hasNext() returns true.
java.io.IOException - when underlying input throws IOExceptionMessageInsufficientBufferException - when the end of file reached, i.e. hasNext() == false.private byte readByte()
throws java.io.IOException
java.io.IOExceptionprivate short readShort()
throws java.io.IOException
java.io.IOExceptionprivate int readInt()
throws java.io.IOException
java.io.IOExceptionprivate long readLong()
throws java.io.IOException
java.io.IOExceptionprivate float readFloat()
throws java.io.IOException
java.io.IOExceptionprivate double readDouble()
throws java.io.IOException
java.io.IOExceptionpublic void skipValue()
throws java.io.IOException
java.io.IOExceptionpublic void skipValue(int count)
throws java.io.IOException
count - number of values to skipjava.io.IOExceptionprivate static MessagePackException unexpected(java.lang.String expected, byte b)
expected - b - MessageFormatExceptionprivate static MessagePackException unexpectedExtension(java.lang.String expected, int expectedType, int actualType)
public ImmutableValue unpackValue() throws java.io.IOException
java.io.IOExceptionpublic Variable unpackValue(Variable var) throws java.io.IOException
java.io.IOExceptionpublic void unpackNil()
throws java.io.IOException
MessageTypeException - when value is not MessagePack Nil typejava.io.IOException - when underlying input throws IOExceptionpublic boolean tryUnpackNil()
throws java.io.IOException
unpackNil() is that unpackNil throws an exception if the next byte is not nil value
while this tryUnpackNil method returns false without changing position.MessageInsufficientBufferException - when the end of file reachedjava.io.IOException - when underlying input throws IOExceptionpublic boolean unpackBoolean()
throws java.io.IOException
MessageTypeException - when value is not MessagePack Boolean typejava.io.IOException - when underlying input throws IOExceptionpublic byte unpackByte()
throws java.io.IOException
MessageIntegerOverflowException if the value doesn't fit in the range of byte. This may happen when getNextFormat() returns UINT8, INT16, or larger integer formats.MessageIntegerOverflowException - when value doesn't fit in the range of byteMessageTypeException - when value is not MessagePack Integer typejava.io.IOException - when underlying input throws IOExceptionpublic short unpackShort()
throws java.io.IOException
MessageIntegerOverflowException if the value doesn't fit in the range of short. This may happen when getNextFormat() returns UINT16, INT32, or larger integer formats.MessageIntegerOverflowException - when value doesn't fit in the range of shortMessageTypeException - when value is not MessagePack Integer typejava.io.IOException - when underlying input throws IOExceptionpublic int unpackInt()
throws java.io.IOException
MessageIntegerOverflowException if the value doesn't fit in the range of int. This may happen when getNextFormat() returns UINT32, INT64, or larger integer formats.MessageIntegerOverflowException - when value doesn't fit in the range of intMessageTypeException - when value is not MessagePack Integer typejava.io.IOException - when underlying input throws IOExceptionpublic long unpackLong()
throws java.io.IOException
MessageIntegerOverflowException if the value doesn't fit in the range of long. This may happen when getNextFormat() returns UINT64.MessageIntegerOverflowException - when value doesn't fit in the range of longMessageTypeException - when value is not MessagePack Integer typejava.io.IOException - when underlying input throws IOExceptionpublic java.math.BigInteger unpackBigInteger()
throws java.io.IOException
MessageTypeException - when value is not MessagePack Integer typejava.io.IOException - when underlying input throws IOExceptionpublic float unpackFloat()
throws java.io.IOException
getNextFormat() returns FLOAT64.MessageTypeException - when value is not MessagePack Float typejava.io.IOException - when underlying input throws IOExceptionpublic double unpackDouble()
throws java.io.IOException
MessageTypeException - when value is not MessagePack Float typejava.io.IOException - when underlying input throws IOExceptionprivate void resetDecoder()
public java.lang.String unpackString()
throws java.io.IOException
java.io.IOExceptionprivate void handleCoderError(java.nio.charset.CoderResult cr)
throws java.nio.charset.CharacterCodingException
java.nio.charset.CharacterCodingExceptionprivate java.lang.String decodeStringFastPath(int length)
public java.time.Instant unpackTimestamp()
throws java.io.IOException
java.io.IOExceptionpublic java.time.Instant unpackTimestamp(ExtensionTypeHeader ext) throws java.io.IOException
java.io.IOExceptionpublic int unpackArrayHeader()
throws java.io.IOException
This method returns number of elements to be read. After this method call, you call unpacker methods for each element. You don't have to call anything at the end of iteration.
MessageTypeException - when value is not MessagePack Array typeMessageSizeException - when size of the array is larger than 2^31 - 1java.io.IOException - when underlying input throws IOExceptionpublic int unpackMapHeader()
throws java.io.IOException
This method returns number of pairs to be read. After this method call, for each pair, you call unpacker methods for key first, and then value. You will call unpacker methods twice as many time as the returned count. You don't have to call anything at the end of iteration.
MessageTypeException - when value is not MessagePack Map typeMessageSizeException - when size of the map is larger than 2^31 - 1java.io.IOException - when underlying input throws IOExceptionpublic ExtensionTypeHeader unpackExtensionTypeHeader() throws java.io.IOException
java.io.IOExceptionprivate int tryReadStringHeader(byte b)
throws java.io.IOException
java.io.IOExceptionprivate int tryReadBinaryHeader(byte b)
throws java.io.IOException
java.io.IOExceptionpublic int unpackRawStringHeader()
throws java.io.IOException
java.io.IOExceptionpublic int unpackBinaryHeader()
throws java.io.IOException
This method returns number of bytes to be read. After this method call, you call a readPayload method such as
readPayload(int) with the returned count.
You can divide readPayload method into multiple calls. In this case, you must repeat readPayload methods until total amount of bytes becomes equal to the returned count.
MessageTypeException - when value is not MessagePack Map typeMessageSizeException - when size of the map is larger than 2^31 - 1java.io.IOException - when underlying input throws IOExceptionprivate void skipPayload(int numBytes)
throws java.io.IOException
skipValue().numBytes - java.io.IOExceptionpublic void readPayload(java.nio.ByteBuffer dst)
throws java.io.IOException
This consumes bytes, copies them to the specified buffer, and moves forward position of the byte buffer until ByteBuffer.remaining() returns 0.
dst - the byte buffer into which the data is readjava.io.IOException - when underlying input throws IOExceptionpublic void readPayload(MessageBuffer dst, int off, int len) throws java.io.IOException
This consumes bytes, copies them to the specified buffer This is usually faster than readPayload(ByteBuffer) by using unsafe.copyMemory
dst - the Message buffer into which the data is readoff - the offset in the Message bufferlen - the number of bytes to readjava.io.IOException - when underlying input throws IOExceptionpublic void readPayload(byte[] dst)
throws java.io.IOException
This method is equivalent to readPayload(dst, 0, dst.length).
dst - the byte array into which the data is readjava.io.IOException - when underlying input throws IOExceptionpublic byte[] readPayload(int length)
throws java.io.IOException
This method is equivalent to readPayload(new byte[length]).
length - number of bytes to be readjava.io.IOException - when underlying input throws IOExceptionpublic void readPayload(byte[] dst,
int off,
int len)
throws java.io.IOException
dst - the byte array into which the data is readoff - the offset in the dst arraylen - the number of bytes to readjava.io.IOException - when underlying input throws IOExceptionpublic MessageBuffer readPayloadAsReference(int length) throws java.io.IOException
This consumes specified amount of bytes and returns its reference or copy. This method tries to return reference as much as possible because it is faster. However, it may copy data to a newly allocated buffer if reference is not applicable.
length - number of bytes to be readjava.io.IOException - when underlying input throws IOExceptionprivate int readNextLength8()
throws java.io.IOException
java.io.IOExceptionprivate int readNextLength16()
throws java.io.IOException
java.io.IOExceptionprivate int readNextLength32()
throws java.io.IOException
java.io.IOExceptionpublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOExceptionprivate static MessageIntegerOverflowException overflowU8(byte u8)
private static MessageIntegerOverflowException overflowU16(short u16)
private static MessageIntegerOverflowException overflowU32(int u32)
private static MessageIntegerOverflowException overflowU64(long u64)
private static MessageIntegerOverflowException overflowI16(short i16)
private static MessageIntegerOverflowException overflowI32(int i32)
private static MessageIntegerOverflowException overflowI64(long i64)
private static MessageSizeException overflowU32Size(int u32)