Skip to content

Commit 6d8b948

Browse files
committed
Merge pull request msgpack#350 from xerial/fix-example
Fix msgpack core usage example
2 parents 0df9e35 + 8248f62 commit 6d8b948

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,23 @@
1515
//
1616
package org.msgpack.core.example;
1717

18-
import org.msgpack.core.MessageFormat;
19-
import org.msgpack.core.MessagePack;
18+
import org.msgpack.core.*;
2019
import org.msgpack.core.MessagePack.PackerConfig;
2120
import org.msgpack.core.MessagePack.UnpackerConfig;
22-
import org.msgpack.core.MessagePacker;
23-
import org.msgpack.core.MessageUnpacker;
2421
import org.msgpack.value.ArrayValue;
2522
import org.msgpack.value.ExtensionValue;
2623
import org.msgpack.value.FloatValue;
2724
import org.msgpack.value.IntegerValue;
2825
import org.msgpack.value.Value;
2926

30-
import java.io.ByteArrayOutputStream;
3127
import java.io.File;
3228
import java.io.FileInputStream;
3329
import java.io.FileOutputStream;
3430
import java.io.IOException;
3531
import java.math.BigInteger;
3632

3733
/**
38-
* This class describes the usage of MessagePack v07
34+
* This class describes the usage of MessagePack
3935
*/
4036
public 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

Comments
 (0)