1515//
1616package org .msgpack .core .example ;
1717
18- import org .msgpack .core .MessageFormat ;
19- import org .msgpack .core .MessagePack ;
18+ import org .msgpack .core .*;
2019import org .msgpack .core .MessagePack .PackerConfig ;
2120import org .msgpack .core .MessagePack .UnpackerConfig ;
22- import org .msgpack .core .MessagePacker ;
23- import org .msgpack .core .MessageUnpacker ;
2421import org .msgpack .value .ArrayValue ;
2522import org .msgpack .value .ExtensionValue ;
2623import org .msgpack .value .FloatValue ;
2724import org .msgpack .value .IntegerValue ;
2825import org .msgpack .value .Value ;
2926
30- import java .io .ByteArrayOutputStream ;
3127import java .io .File ;
3228import java .io .FileInputStream ;
3329import java .io .FileOutputStream ;
3430import java .io .IOException ;
3531import java .math .BigInteger ;
3632
3733/**
38- * This class describes the usage of MessagePack v07
34+ * This class describes the usage of MessagePack
3935 */
4036public class MessagePackExample
4137{
@@ -51,19 +47,19 @@ private MessagePackExample()
5147 public static void basicUsage ()
5248 throws IOException
5349 {
54- // Serialize with MessagePacker
55- ByteArrayOutputStream out = new ByteArrayOutputStream ();
56- MessagePacker packer = MessagePack .newDefaultPacker ( out );
50+ // Serialize with MessagePacker.
51+ // MessageBufferPacker is an optimized version of MessagePacker for packing data into a byte array
52+ MessageBufferPacker packer = MessagePack .newDefaultBufferPacker ( );
5753 packer
5854 .packInt (1 )
5955 .packString ("leo" )
6056 .packArrayHeader (2 )
6157 .packString ("xxx-xxxx" )
6258 .packString ("yyy-yyyy" );
63- packer .close ();
59+ packer .close (); // Never forget to close (or flush) the buffer
6460
6561 // Deserialize with MessageUnpacker
66- MessageUnpacker unpacker = MessagePack .newDefaultUnpacker (out .toByteArray ());
62+ MessageUnpacker unpacker = MessagePack .newDefaultUnpacker (packer .toByteArray ());
6763 int id = unpacker .unpackInt (); // 1
6864 String name = unpacker .unpackString (); // "leo"
6965 int numPhones = unpacker .unpackArrayHeader (); // 2
@@ -97,8 +93,7 @@ public static void packer()
9793 throws IOException
9894 {
9995 // Create a MesagePacker (encoder) instance
100- ByteArrayOutputStream out = new ByteArrayOutputStream ();
101- MessagePacker packer = MessagePack .newDefaultPacker (out );
96+ MessageBufferPacker packer = MessagePack .newDefaultBufferPacker ();
10297
10398 // pack (encode) primitive values in message pack format
10499 packer .packBoolean (true );
@@ -181,8 +176,7 @@ public static void readAndWriteFile()
181176 // Here is a list of message pack data format: https://github.com/msgpack/msgpack/blob/master/spec.md#overview
182177 MessageFormat format = unpacker .getNextFormat ();
183178
184- // Alternatively you can use ValueHolder to extract a value of any type
185- // NOTE: Value interface is in a preliminary state, so the following code might change in future releases
179+ // You can also use unpackValue to extract a value of any type
186180 Value v = unpacker .unpackValue ();
187181 switch (v .getValueType ()) {
188182 case NIL :
@@ -245,17 +239,16 @@ else if (iv.isInLongRange()) {
245239 public static void configuration ()
246240 throws IOException
247241 {
248- ByteArrayOutputStream out = new ByteArrayOutputStream ();
249- MessagePacker packer = new PackerConfig ()
242+ MessageBufferPacker packer = new PackerConfig ()
250243 .withSmallStringOptimizationThreshold (256 ) // String
251- .newPacker ( out );
244+ .newBufferPacker ( );
252245
253246 packer .packInt (10 );
254247 packer .packBoolean (true );
255248 packer .close ();
256249
257250 // Unpack data
258- byte [] packedData = out .toByteArray ();
251+ byte [] packedData = packer .toByteArray ();
259252 MessageUnpacker unpacker = new UnpackerConfig ()
260253 .withStringDecoderBufferSize (16 * 1024 ) // If your data contains many large strings (the default is 8k)
261254 .newUnpacker (packedData );
0 commit comments