org.msgpack.core
Class MessageUnpacker

java.lang.Object
  extended by org.msgpack.core.MessageUnpacker
All Implemented Interfaces:
java.io.Closeable

public class MessageUnpacker
extends java.lang.Object
implements java.io.Closeable

MessageUnpacker lets an application read message-packed values from a data stream. The application needs to call getNextFormat() followed by an appropriate unpackXXX method according to the the returned format type.

 
     MessageUnpacker unpacker = new MessageUnpacker(...);
     while(unpacker.hasNext()) {
         MessageFormat f = unpacker.getNextFormat();
         switch(f) {
             case MessageFormat.POSFIXINT =>
             case MessageFormat.INT8 =>
             case MessageFormat.UINT8 => {
                int v = unpacker.unpackInt();
                break;
             }
             case MessageFormat.STRING => {
                String v = unpacker.unpackString();
                break;
             }
             // ...
       }
     }

 
 


Constructor Summary
MessageUnpacker(byte[] arr)
          Create an MessageUnpacker that reads data from the given byte array.
MessageUnpacker(java.io.InputStream in)
          Create an MessageUnpacker that reads data from the given InputStream.
MessageUnpacker(MessageBufferInput in)
          Create an MessageUnpacker that reads data from the given MessageBufferInput
MessageUnpacker(MessageBufferInput in, MessagePack.Config config)
          Create an MessageUnpacker
MessageUnpacker(java.nio.channels.ReadableByteChannel in)
          Create an MessageUnpacker that reads data from the given ReadableByteChannel.
 
Method Summary
 void close()
           
 Cursor getCursor()
          Get a Cursor for traversing message-packed values
 MessageFormat getNextFormat()
          Returns the next MessageFormat type.
 long getTotalReadBytes()
           
 boolean hasNext()
          Returns true true if this unpacker has more elements.
 void readPayload(byte[] dst)
           
 void readPayload(byte[] dst, int off, int len)
          Read up to len bytes of data into the destination array
 void readPayload(java.nio.ByteBuffer dst)
           
 MessageBuffer readPayloadAsReference(int length)
           
 void skipBytes(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
 int unpackArrayHeader()
           
 java.math.BigInteger unpackBigInteger()
           
 int unpackBinaryHeader()
           
 boolean unpackBoolean()
           
 byte unpackByte()
           
 double unpackDouble()
           
 ExtendedTypeHeader unpackExtendedTypeHeader()
           
 float unpackFloat()
           
 void unpackFloat(FloatHolder holder)
           
 void unpackFloat(ValueHolder holder)
           
 int unpackInt()
           
 void unpackInteger(IntegerHolder holder)
          Unpack an integer, then store the read value to the given holder
 long unpackLong()
           
 int unpackMapHeader()
           
 java.lang.Object unpackNil()
           
 int unpackRawStringHeader()
           
 short unpackShort()
           
 java.lang.String unpackString()
           
 MessageFormat unpackValue(ValueHolder holder)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageUnpacker

public MessageUnpacker(byte[] arr)
Create an MessageUnpacker that reads data from the given byte array.

Parameters:
arr -

MessageUnpacker

public MessageUnpacker(java.io.InputStream in)
Create an MessageUnpacker that reads data from the given InputStream.

Parameters:
in -

MessageUnpacker

public MessageUnpacker(java.nio.channels.ReadableByteChannel in)
Create an MessageUnpacker that reads data from the given ReadableByteChannel.

Parameters:
in -

MessageUnpacker

public MessageUnpacker(MessageBufferInput in)
Create an MessageUnpacker that reads data from the given MessageBufferInput

Parameters:
in -

MessageUnpacker

public MessageUnpacker(MessageBufferInput in,
                       MessagePack.Config config)
Create an MessageUnpacker

Parameters:
in -
config - configuration
Method Detail

getCursor

public Cursor getCursor()
Get a Cursor for traversing message-packed values

Returns:

getTotalReadBytes

public long getTotalReadBytes()

hasNext

public boolean hasNext()
                throws java.io.IOException
Returns true true if this unpacker has more elements. When this returns true, subsequent call to getNextFormat() returns an MessageFormat instance. If false, getNextFormat() will throw an EOFException.

Returns:
true if this unpacker has more elements to read
Throws:
java.io.IOException

getNextFormat

public MessageFormat getNextFormat()
                            throws java.io.IOException
Returns the next MessageFormat type. This method should be called after hasNext() returns true. If hasNext() returns false, calling this method throws EOFException. This method does not proceed the internal cursor.

Returns:
the next MessageFormat
Throws:
java.io.IOException - when failed to read the input data.
java.io.EOFException - when the end of file reached, i.e. hasNext() == false.

skipBytes

public void skipBytes(int numBytes)
               throws java.io.IOException
Skip reading the specified number of bytes. Use this method only if you know skipping data is safe. For simply skipping the next value, use skipValue().

Parameters:
numBytes -
Throws:
java.io.IOException

skipValue

public void skipValue()
               throws java.io.IOException
Skip the next value, then move the cursor at the end of the value

Throws:
java.io.IOException

unpackValue

public MessageFormat unpackValue(ValueHolder holder)
                          throws java.io.IOException
Throws:
java.io.IOException

unpackNil

public java.lang.Object unpackNil()
                           throws java.io.IOException
Throws:
java.io.IOException

unpackBoolean

public boolean unpackBoolean()
                      throws java.io.IOException
Throws:
java.io.IOException

unpackByte

public byte unpackByte()
                throws java.io.IOException
Throws:
java.io.IOException

unpackShort

public short unpackShort()
                  throws java.io.IOException
Throws:
java.io.IOException

unpackInt

public int unpackInt()
              throws java.io.IOException
Throws:
java.io.IOException

unpackLong

public long unpackLong()
                throws java.io.IOException
Throws:
java.io.IOException

unpackBigInteger

public java.math.BigInteger unpackBigInteger()
                                      throws java.io.IOException
Throws:
java.io.IOException

unpackInteger

public void unpackInteger(IntegerHolder holder)
                   throws java.io.IOException
Unpack an integer, then store the read value to the given holder

Parameters:
holder - an integer holder to which the unpacked integer will be set.
Throws:
java.io.IOException

unpackFloat

public float unpackFloat()
                  throws java.io.IOException
Throws:
java.io.IOException

unpackDouble

public double unpackDouble()
                    throws java.io.IOException
Throws:
java.io.IOException

unpackFloat

public void unpackFloat(ValueHolder holder)
                 throws java.io.IOException
Throws:
java.io.IOException

unpackFloat

public void unpackFloat(FloatHolder holder)
                 throws java.io.IOException
Throws:
java.io.IOException

unpackString

public java.lang.String unpackString()
                              throws java.io.IOException
Throws:
java.io.IOException

unpackArrayHeader

public int unpackArrayHeader()
                      throws java.io.IOException
Throws:
java.io.IOException

unpackMapHeader

public int unpackMapHeader()
                    throws java.io.IOException
Throws:
java.io.IOException

unpackExtendedTypeHeader

public ExtendedTypeHeader unpackExtendedTypeHeader()
                                            throws java.io.IOException
Throws:
java.io.IOException

unpackRawStringHeader

public int unpackRawStringHeader()
                          throws java.io.IOException
Throws:
java.io.IOException

unpackBinaryHeader

public int unpackBinaryHeader()
                       throws java.io.IOException
Throws:
java.io.IOException

readPayload

public void readPayload(java.nio.ByteBuffer dst)
                 throws java.io.IOException
Throws:
java.io.IOException

readPayload

public void readPayload(byte[] dst)
                 throws java.io.IOException
Throws:
java.io.IOException

readPayload

public void readPayload(byte[] dst,
                        int off,
                        int len)
                 throws java.io.IOException
Read up to len bytes of data into the destination array

Parameters:
dst - the buffer into which the data is read
off - the offset in the dst array
len - the number of bytes to read
Throws:
java.io.IOException

readPayloadAsReference

public MessageBuffer readPayloadAsReference(int length)
                                     throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException