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 | 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.
|
MessageFormat |
getNextFormat()
Returns format of the next value.
|
long |
getTotalReadBytes()
Returns total number of read bytes.
|
boolean |
hasNext()
Returns true true if this unpacker has more elements.
|
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.
|
MessageBuffer |
readPayloadAsReference(int length)
Reads payload bytes of binary, extension, or raw string types as a reference to internal buffer.
|
MessageBufferInput |
reset(MessageBufferInput in)
Replaces underlying input.
|
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
|
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() |
ImmutableValue |
unpackValue() |
Variable |
unpackValue(Variable var) |
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.
public boolean hasNext() throws java.io.IOException
getNextFormat()
returns an
MessageFormat instance. If false, next getNextFormat()
call will throw an MessageInsufficientBufferException.java.io.IOException
public 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.public void skipValue() throws java.io.IOException
java.io.IOException
public void skipValue(int count) throws java.io.IOException
count
- number of values to skipjava.io.IOException
public ImmutableValue unpackValue() throws java.io.IOException
java.io.IOException
public Variable unpackValue(Variable var) throws java.io.IOException
java.io.IOException
public void unpackNil() throws java.io.IOException
MessageTypeException
- when value is not MessagePack Nil typejava.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 IOExceptionpublic java.lang.String unpackString() throws java.io.IOException
java.io.IOException
public 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.IOException
public int unpackRawStringHeader() throws java.io.IOException
java.io.IOException
public 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 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(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 IOExceptionpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException