-
- All Implemented Interfaces:
-
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class MessagePacker implements Closeable, Flushable
MessagePack serializer that converts objects into binary. You can use factory methods of MessagePack class or MessagePack.PackerConfig class to create an instance.
This class provides following primitive methods to write MessagePack values. These primitive methods write short bytes (1 to 7 bytes) to the internal buffer at once. There are also some utility methods for convenience.
Primitive methods:
Utility methods:
To write a byte array, first you call packBinaryHeader method with length of the byte array. Then, you call writePayload or addPayload method to write the contents.
To write a List, Collection or array, first you call packArrayHeader method with the number of elements. Then, you call packer methods for each element. iteration.
To write a Map, first you call packMapHeader method with size of the map. Then, for each pair, you call packer methods for key first, and then value. You will call packer methods twice as many time as the size of the map.
Note that packXxxHeader methods don't validate number of elements. You must call packer methods for correct number of times to produce valid MessagePack data.
When IOException is thrown, primitive methods guarantee that all data is written to the internal buffer or no data is written. This is convenient behavior when you use a non-blocking output channel that may not be writable immediately.
-
-
Method Summary
Modifier and Type Method Description MessageBufferOutputreset(MessageBufferOutput out)Replaces underlying output. longgetTotalWrittenBytes()Returns total number of written bytes. voidclear()Clears the written data. voidflush()Flushes internal buffer to the underlying output. voidclose()Closes underlying output. MessagePackerpackNil()Writes a Nil value. MessagePackerpackBoolean(boolean b)Writes a Boolean value. MessagePackerpackByte(byte b)Writes an Integer value. MessagePackerpackShort(short v)Writes an Integer value. MessagePackerpackInt(int r)Writes an Integer value. MessagePackerpackLong(long v)Writes an Integer value. MessagePackerpackBigInteger(BigInteger bi)Writes an Integer value. MessagePackerpackFloat(float v)Writes a Float value. MessagePackerpackDouble(double v)Writes a Float value. MessagePackerpackString(String s)Writes a String vlaue in UTF-8 encoding. MessagePackerpackArrayHeader(int arraySize)Writes header of an Array value. MessagePackerpackMapHeader(int mapSize)Writes header of a Map value. MessagePackerpackValue(Value v)Writes a dynamically typed value. MessagePackerpackExtensionTypeHeader(byte extType, int payloadLen)Writes header of an Extension value. MessagePackerpackBinaryHeader(int len)Writes header of a Binary value. MessagePackerpackRawStringHeader(int len)Writes header of a String value. MessagePackerwritePayload(Array<byte> src)Writes a byte array to the output. MessagePackerwritePayload(Array<byte> src, int off, int len)Writes a byte array to the output. MessagePackeraddPayload(Array<byte> src)Writes a byte array to the output. MessagePackeraddPayload(Array<byte> src, int off, int len)Writes a byte array to the output. -
-
Method Detail
-
reset
MessageBufferOutput reset(MessageBufferOutput out)
Replaces underlying output.
This method flushes current internal buffer to the output, swaps it with the new given output, then returnsthe old output.
This method doesn't close the old output.
- Parameters:
out- new output
-
getTotalWrittenBytes
long getTotalWrittenBytes()
Returns total number of written bytes.
This method returns total of amount of data flushed to the underlying output plus size of currentinternal buffer.
Calling reset resets this number to 0.
-
clear
void clear()
Clears the written data.
-
flush
void flush()
Flushes internal buffer to the underlying output.
This method also calls flush method of the underlying output after writing internal buffer.
-
close
void close()
Closes underlying output.
This method flushes internal buffer before closing.
-
packNil
MessagePacker packNil()
Writes a Nil value.
This method writes a nil byte.
-
packBoolean
MessagePacker packBoolean(boolean b)
Writes a Boolean value.
This method writes a true byte or a false byte.
-
packByte
MessagePacker packByte(byte b)
Writes an Integer value.
This method writes an integer using the smallest format from the int format family.
- Parameters:
b- the integer to be written
-
packShort
MessagePacker packShort(short v)
Writes an Integer value.
This method writes an integer using the smallest format from the int format family.
- Parameters:
v- the integer to be written
-
packInt
MessagePacker packInt(int r)
Writes an Integer value.
This method writes an integer using the smallest format from the int format family.
- Parameters:
r- the integer to be written
-
packLong
MessagePacker packLong(long v)
Writes an Integer value.
This method writes an integer using the smallest format from the int format family.
- Parameters:
v- the integer to be written
-
packBigInteger
MessagePacker packBigInteger(BigInteger bi)
Writes an Integer value.
This method writes an integer using the smallest format from the int format family.
- Parameters:
bi- the integer to be written
-
packFloat
MessagePacker packFloat(float v)
Writes a Float value.
This method writes a float value using float format family.
- Parameters:
v- the value to be written
-
packDouble
MessagePacker packDouble(double v)
Writes a Float value.
This method writes a float value using float format family.
- Parameters:
v- the value to be written
-
packString
MessagePacker packString(String s)
Writes a String vlaue in UTF-8 encoding.
This method writes a UTF-8 string using the smallest format from the str format family by default. If withStr8FormatSupport is set to false, smallest format from the str format family excepting str8 format.
- Parameters:
s- the string to be written
-
packArrayHeader
MessagePacker packArrayHeader(int arraySize)
Writes header of an Array value.
You will call other packer methods for each element after this method call.
You don't have to call anything at the end of iteration.
- Parameters:
arraySize- number of elements to be written
-
packMapHeader
MessagePacker packMapHeader(int mapSize)
Writes header of a Map value.
After this method call, for each key-value pair, you will call packer methods for key first, and then value.You will call packer methods twice as many time as the size of the map.
You don't have to call anything at the end of iteration.
- Parameters:
mapSize- number of pairs to be written
-
packValue
MessagePacker packValue(Value v)
Writes a dynamically typed value.
- Parameters:
v- the value to be written
-
packExtensionTypeHeader
MessagePacker packExtensionTypeHeader(byte extType, int payloadLen)
Writes header of an Extension value.
You MUST call writePayload or addPayload method to write body binary.
- Parameters:
extType- the extension type tag to be writtenpayloadLen- number of bytes of a payload binary to be written
-
packBinaryHeader
MessagePacker packBinaryHeader(int len)
Writes header of a Binary value.
You MUST call writePayload or addPayload method to write body binary.
- Parameters:
len- number of bytes of a binary to be written
-
packRawStringHeader
MessagePacker packRawStringHeader(int len)
Writes header of a String value.
Length must be number of bytes of a string in UTF-8 encoding.
You MUST call writePayload or addPayload method to write body of theUTF-8 encoded string.
- Parameters:
len- number of bytes of a UTF-8 string to be written
-
writePayload
MessagePacker writePayload(Array<byte> src)
Writes a byte array to the output.
This method is used with packRawStringHeader or packBinaryHeader methods.
- Parameters:
src- the data to add
-
writePayload
MessagePacker writePayload(Array<byte> src, int off, int len)
Writes a byte array to the output.
This method is used with packRawStringHeader or packBinaryHeader methods.
- Parameters:
src- the data to addoff- the start offset in the datalen- the number of bytes to add
-
addPayload
MessagePacker addPayload(Array<byte> src)
Writes a byte array to the output.
This method is used with packRawStringHeader or packBinaryHeader methods.
Unlike writePayload method, this method does not make a defensive copy of the given bytearray, even if it is shorter than withBufferFlushThreshold. This isfaster than writePayload method but caller must not modify the byte array after callingthis method.
- Parameters:
src- the data to add
-
addPayload
MessagePacker addPayload(Array<byte> src, int off, int len)
Writes a byte array to the output.
This method is used with packRawStringHeader or packBinaryHeader methods.
Unlike writePayload method, this method does not make a defensive copy of thegiven byte array, even if it is shorter than withBufferFlushThreshold.This is faster than writePayload method but caller must not modify the byte array aftercalling this method.
- Parameters:
src- the data to addoff- the start offset in the datalen- the number of bytes to add
-
-
-
-