forked from isopov/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePack.java
More file actions
760 lines (675 loc) · 28.1 KB
/
Copy pathMessagePack.java
File metadata and controls
760 lines (675 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.core;
import org.msgpack.core.buffer.ArrayBufferInput;
import org.msgpack.core.buffer.ByteBufferInput;
import org.msgpack.core.buffer.ChannelBufferInput;
import org.msgpack.core.buffer.ChannelBufferOutput;
import org.msgpack.core.buffer.InputStreamBufferInput;
import org.msgpack.core.buffer.MessageBufferInput;
import org.msgpack.core.buffer.MessageBufferOutput;
import org.msgpack.core.buffer.OutputStreamBufferOutput;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
/**
* Convenience class to build packer and unpacker classes.
*
* You can select an appropriate factory method as following.
*
* <p>
* Deserializing objects from binary:
*
* <table>
* <tr><th>Input type</th><th>Factory method</th><th>Return type</th></tr>
* <tr><td>byte[]</td><td>{@link #newDefaultUnpacker(byte[], int, int)}</td><td>{@link MessageUnpacker}</td></tr>
* <tr><td>ByteBuffer</td><td>{@link #newDefaultUnpacker(ByteBuffer)}</td><td>{@link MessageUnpacker}</td></tr>
* <tr><td>InputStream</td><td>{@link #newDefaultUnpacker(InputStream)}</td><td>{@link MessageUnpacker}</td></tr>
* <tr><td>ReadableByteChannel</td><td>{@link #newDefaultUnpacker(ReadableByteChannel)}</td><td>{@link MessageUnpacker}</td></tr>
* <tr><td>{@link org.msgpack.core.buffer.MessageBufferInput}</td><td>{@link #newDefaultUnpacker(MessageBufferInput)}</td><td>{@link MessageUnpacker}</td></tr>
* </table>
*
* <p>
* Serializing objects into binary:
*
* <table>
* <tr><th>Output type</th><th>Factory method</th><th>Return type</th></tr>
* <tr><td>byte[]</td><td>{@link #newDefaultBufferPacker()}</td><td>{@link MessageBufferPacker}</td><tr>
* <tr><td>OutputStream</td><td>{@link #newDefaultPacker(OutputStream)}</td><td>{@link MessagePacker}</td></tr>
* <tr><td>WritableByteChannel</td><td>{@link #newDefaultPacker(WritableByteChannel)}</td><td>{@link MessagePacker}</td></tr>
* <tr><td>{@link org.msgpack.core.buffer.MessageBufferOutput}</td><td>{@link #newDefaultPacker(MessageBufferOutput)}</td><td>{@link MessagePacker}</td></tr>
* </table>
*
*/
public class MessagePack
{
/**
* @exclude
* Applications should use java.nio.charset.StandardCharsets.UTF_8 instead since Java 7.
*/
public static final Charset UTF8 = Charset.forName("UTF-8");
/**
* Configuration of a {@link MessagePacker} used by {@link #newDefaultPacker(MessageBufferOutput)} and {@link #newDefaultBufferPacker()} methods.
*/
public static final PackerConfig DEFAULT_PACKER_CONFIG = new PackerConfig();
/**
* Configuration of a {@link MessageUnpacker} used by {@link #newDefaultUnpacker(MessageBufferInput)} methods.
*/
public static final UnpackerConfig DEFAULT_UNPACKER_CONFIG = new UnpackerConfig();
/**
* The prefix code set of MessagePack format. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details.
*/
public static final class Code
{
public static final boolean isFixInt(byte b)
{
int v = b & 0xFF;
return v <= 0x7f || v >= 0xe0;
}
public static final boolean isPosFixInt(byte b)
{
return (b & POSFIXINT_MASK) == 0;
}
public static final boolean isNegFixInt(byte b)
{
return (b & NEGFIXINT_PREFIX) == NEGFIXINT_PREFIX;
}
public static final boolean isFixStr(byte b)
{
return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX;
}
public static final boolean isFixedArray(byte b)
{
return (b & (byte) 0xf0) == Code.FIXARRAY_PREFIX;
}
public static final boolean isFixedMap(byte b)
{
return (b & (byte) 0xf0) == Code.FIXMAP_PREFIX;
}
public static final boolean isFixedRaw(byte b)
{
return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX;
}
public static final byte POSFIXINT_MASK = (byte) 0x80;
public static final byte FIXMAP_PREFIX = (byte) 0x80;
public static final byte FIXARRAY_PREFIX = (byte) 0x90;
public static final byte FIXSTR_PREFIX = (byte) 0xa0;
public static final byte NIL = (byte) 0xc0;
public static final byte NEVER_USED = (byte) 0xc1;
public static final byte FALSE = (byte) 0xc2;
public static final byte TRUE = (byte) 0xc3;
public static final byte BIN8 = (byte) 0xc4;
public static final byte BIN16 = (byte) 0xc5;
public static final byte BIN32 = (byte) 0xc6;
public static final byte EXT8 = (byte) 0xc7;
public static final byte EXT16 = (byte) 0xc8;
public static final byte EXT32 = (byte) 0xc9;
public static final byte FLOAT32 = (byte) 0xca;
public static final byte FLOAT64 = (byte) 0xcb;
public static final byte UINT8 = (byte) 0xcc;
public static final byte UINT16 = (byte) 0xcd;
public static final byte UINT32 = (byte) 0xce;
public static final byte UINT64 = (byte) 0xcf;
public static final byte INT8 = (byte) 0xd0;
public static final byte INT16 = (byte) 0xd1;
public static final byte INT32 = (byte) 0xd2;
public static final byte INT64 = (byte) 0xd3;
public static final byte FIXEXT1 = (byte) 0xd4;
public static final byte FIXEXT2 = (byte) 0xd5;
public static final byte FIXEXT4 = (byte) 0xd6;
public static final byte FIXEXT8 = (byte) 0xd7;
public static final byte FIXEXT16 = (byte) 0xd8;
public static final byte STR8 = (byte) 0xd9;
public static final byte STR16 = (byte) 0xda;
public static final byte STR32 = (byte) 0xdb;
public static final byte ARRAY16 = (byte) 0xdc;
public static final byte ARRAY32 = (byte) 0xdd;
public static final byte MAP16 = (byte) 0xde;
public static final byte MAP32 = (byte) 0xdf;
public static final byte NEGFIXINT_PREFIX = (byte) 0xe0;
}
private MessagePack()
{
// Prohibit instantiation of this class
}
/**
* Creates a packer that serializes objects into the specified output.
* <p>
* {@link org.msgpack.core.buffer.MessageBufferOutput} is an interface that lets applications customize memory
* allocation of internal buffer of {@link MessagePacker}. You may prefer {@link #newDefaultBufferPacker()},
* {@link #newDefaultPacker(OutputStream)}, or {@link #newDefaultPacker(WritableByteChannel)} methods instead.
* <p>
* This method is equivalent to <code>DEFAULT_PACKER_CONFIG.newPacker(out)</code>.
*
* @param out A MessageBufferOutput that allocates buffer chunks and receives the buffer chunks with packed data filled in them
* @return A new MessagePacker instance
*/
public static MessagePacker newDefaultPacker(MessageBufferOutput out)
{
return DEFAULT_PACKER_CONFIG.newPacker(out);
}
/**
* Creates a packer that serializes objects into the specified output stream.
* <p>
* Note that you don't have to wrap OutputStream in BufferedOutputStream because MessagePacker has buffering
* internally.
* <p>
* This method is equivalent to <code>DEFAULT_PACKER_CONFIG.newPacker(out)</code>.
*
* @param out The output stream that receives sequence of bytes
* @return A new MessagePacker instance
*/
public static MessagePacker newDefaultPacker(OutputStream out)
{
return DEFAULT_PACKER_CONFIG.newPacker(out);
}
/**
* Creates a packer that serializes objects into the specified writable channel.
* <p>
* This method is equivalent to <code>DEFAULT_PACKER_CONFIG.newPacker(channel)</code>.
*
* @param channel The output channel that receives sequence of bytes
* @return A new MessagePacker instance
*/
public static MessagePacker newDefaultPacker(WritableByteChannel channel)
{
return DEFAULT_PACKER_CONFIG.newPacker(channel);
}
/**
* Creates a packer that serializes objects into byte arrays.
* <p>
* This method provides an optimized implementation of <code>newDefaultBufferPacker(new ByteArrayOutputStream())</code>.
*
* This method is equivalent to <code>DEFAULT_PACKER_CONFIG.newBufferPacker()</code>.
*
* @return A new MessageBufferPacker instance
*/
public static MessageBufferPacker newDefaultBufferPacker()
{
return DEFAULT_PACKER_CONFIG.newBufferPacker();
}
/**
* Creates an unpacker that deserializes objects from a specified input.
* <p>
* {@link org.msgpack.core.buffer.MessageBufferInput} is an interface that lets applications customize memory
* allocation of internal buffer of {@link MessageUnpacker}. You may prefer
* {@link #newDefaultUnpacker(InputStream)}, {@link #newDefaultUnpacker(ReadableByteChannel)},
* {@link #newDefaultUnpacker(byte[], int, int)}, or {@link #newDefaultUnpacker(ByteBuffer)} methods instead.
* <p>
* This method is equivalent to <code>DEFAULT_UNPACKER_CONFIG.newDefaultUnpacker(in)</code>.
*
* @param in The input stream that provides sequence of buffer chunks and optionally reuses them when MessageUnpacker consumed one completely
* @return A new MessageUnpacker instance
*/
public static MessageUnpacker newDefaultUnpacker(MessageBufferInput in)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(in);
}
/**
* Creates an unpacker that deserializes objects from a specified input stream.
* <p>
* Note that you don't have to wrap InputStream in BufferedInputStream because MessageUnpacker has buffering
* internally.
* <p>
* This method is equivalent to <code>DEFAULT_UNPACKER_CONFIG.newDefaultUnpacker(in)</code>.
*
* @param in The input stream that provides sequence of bytes
* @return A new MessageUnpacker instance
*/
public static MessageUnpacker newDefaultUnpacker(InputStream in)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(in);
}
/**
* Creates an unpacker that deserializes objects from a specified readable channel.
* <p>
* This method is equivalent to <code>DEFAULT_UNPACKER_CONFIG.newDefaultUnpacker(in)</code>.
*
* @param channel The input channel that provides sequence of bytes
* @return A new MessageUnpacker instance
*/
public static MessageUnpacker newDefaultUnpacker(ReadableByteChannel channel)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(channel);
}
/**
* Creates an unpacker that deserializes objects from a specified byte array.
* <p>
* This method provides an optimized implementation of <code>newDefaultUnpacker(new ByteArrayInputStream(contents))</code>.
* <p>
* This method is equivalent to <code>DEFAULT_UNPACKER_CONFIG.newDefaultUnpacker(contents)</code>.
*
* @param contents The byte array that contains packed objects in MessagePack format
* @return A new MessageUnpacker instance that will never throw IOException
*/
public static MessageUnpacker newDefaultUnpacker(byte[] contents)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(contents);
}
/**
* Creates an unpacker that deserializes objects from subarray of a specified byte array.
* <p>
* This method provides an optimized implementation of <code>newDefaultUnpacker(new ByteArrayInputStream(contents, offset, length))</code>.
* <p>
* This method is equivalent to <code>DEFAULT_UNPACKER_CONFIG.newDefaultUnpacker(contents)</code>.
*
* @param contents The byte array that contains packed objects
* @param offset The index of the first byte
* @param length The number of bytes
* @return A new MessageUnpacker instance that will never throw IOException
*/
public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, int length)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(contents, offset, length);
}
/**
* Creates an unpacker that deserializes objects from a specified ByteBuffer.
* <p>
* Note that the returned unpacker reads data from the current position of the ByteBuffer until its limit.
* However, its position does not change when unpacker reads data. You may use
* {@link MessageUnpacker#getTotalReadBytes()} to get actual amount of bytes used in ByteBuffer.
* <p>
* This method supports both non-direct buffer and direct buffer.
*
* @param contents The byte buffer that contains packed objects
* @return A new MessageUnpacker instance that will never throw IOException
*/
public static MessageUnpacker newDefaultUnpacker(ByteBuffer contents)
{
return DEFAULT_UNPACKER_CONFIG.newUnpacker(contents);
}
/**
* MessagePacker configuration.
*/
public static class PackerConfig
implements Cloneable
{
private int smallStringOptimizationThreshold = 512;
private int bufferFlushThreshold = 8192;
private int bufferSize = 8192;
private boolean str8FormatSupport = true;
public PackerConfig()
{
}
private PackerConfig(PackerConfig copy)
{
this.smallStringOptimizationThreshold = copy.smallStringOptimizationThreshold;
this.bufferFlushThreshold = copy.bufferFlushThreshold;
this.bufferSize = copy.bufferSize;
this.str8FormatSupport = copy.str8FormatSupport;
}
@Override
public PackerConfig clone()
{
return new PackerConfig(this);
}
@Override
public int hashCode()
{
int result = smallStringOptimizationThreshold;
result = 31 * result + bufferFlushThreshold;
result = 31 * result + bufferSize;
result = 31 * result + (str8FormatSupport ? 1 : 0);
return result;
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof PackerConfig)) {
return false;
}
PackerConfig o = (PackerConfig) obj;
return this.smallStringOptimizationThreshold == o.smallStringOptimizationThreshold
&& this.bufferFlushThreshold == o.bufferFlushThreshold
&& this.bufferSize == o.bufferSize
&& this.str8FormatSupport == o.str8FormatSupport;
}
/**
* Creates a packer that serializes objects into the specified output.
* <p>
* {@link org.msgpack.core.buffer.MessageBufferOutput} is an interface that lets applications customize memory
* allocation of internal buffer of {@link MessagePacker}.
*
* @param out A MessageBufferOutput that allocates buffer chunks and receives the buffer chunks with packed data filled in them
* @return A new MessagePacker instance
*/
public MessagePacker newPacker(MessageBufferOutput out)
{
return new MessagePacker(out, this);
}
/**
* Creates a packer that serializes objects into the specified output stream.
* <p>
* Note that you don't have to wrap OutputStream in BufferedOutputStream because MessagePacker has buffering
* internally.
*
* @param out The output stream that receives sequence of bytes
* @return A new MessagePacker instance
*/
public MessagePacker newPacker(OutputStream out)
{
return newPacker(new OutputStreamBufferOutput(out, bufferSize));
}
/**
* Creates a packer that serializes objects into the specified writable channel.
*
* @param channel The output channel that receives sequence of bytes
* @return A new MessagePacker instance
*/
public MessagePacker newPacker(WritableByteChannel channel)
{
return newPacker(new ChannelBufferOutput(channel, bufferSize));
}
/**
* Creates a packer that serializes objects into byte arrays.
* <p>
* This method provides an optimized implementation of <code>newDefaultBufferPacker(new ByteArrayOutputStream())</code>.
*
* @return A new MessageBufferPacker instance
*/
public MessageBufferPacker newBufferPacker()
{
return new MessageBufferPacker(this);
}
/**
* Use String.getBytes() for converting Java Strings that are shorter than this threshold.
* Note that this parameter is subject to change.
*/
public PackerConfig withSmallStringOptimizationThreshold(int length)
{
PackerConfig copy = clone();
copy.smallStringOptimizationThreshold = length;
return copy;
}
public int getSmallStringOptimizationThreshold()
{
return smallStringOptimizationThreshold;
}
/**
* When the next payload size exceeds this threshold, MessagePacker will call
* {@link org.msgpack.core.buffer.MessageBufferOutput#flush()} before writing more data (default: 8192).
*/
public PackerConfig withBufferFlushThreshold(int bytes)
{
PackerConfig copy = clone();
copy.bufferFlushThreshold = bytes;
return copy;
}
public int getBufferFlushThreshold()
{
return bufferFlushThreshold;
}
/**
* When a packer is created with {@link #newPacker(OutputStream)} or {@link #newPacker(WritableByteChannel)}, the stream will be
* buffered with this size of buffer (default: 8192).
*/
public PackerConfig withBufferSize(int bytes)
{
PackerConfig copy = clone();
copy.bufferSize = bytes;
return copy;
}
public int getBufferSize()
{
return bufferSize;
}
/**
* Disable str8 format when needed backward compatibility between
* different msgpack serializer versions.
* default true (str8 supported enabled)
*/
public PackerConfig withStr8FormatSupport(boolean str8FormatSupport)
{
PackerConfig copy = clone();
copy.str8FormatSupport = str8FormatSupport;
return copy;
}
public boolean isStr8FormatSupport()
{
return str8FormatSupport;
}
}
/**
* MessageUnpacker configuration.
*/
public static class UnpackerConfig
implements Cloneable
{
private boolean allowReadingStringAsBinary = true;
private boolean allowReadingBinaryAsString = true;
private CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;
private CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;
private int stringSizeLimit = Integer.MAX_VALUE;
private int bufferSize = 8192;
private int stringDecoderBufferSize = 8192;
public UnpackerConfig()
{
}
private UnpackerConfig(UnpackerConfig copy)
{
this.allowReadingStringAsBinary = copy.allowReadingStringAsBinary;
this.allowReadingBinaryAsString = copy.allowReadingBinaryAsString;
this.actionOnMalformedString = copy.actionOnMalformedString;
this.actionOnUnmappableString = copy.actionOnUnmappableString;
this.stringSizeLimit = copy.stringSizeLimit;
this.bufferSize = copy.bufferSize;
}
@Override
public UnpackerConfig clone()
{
return new UnpackerConfig(this);
}
@Override
public int hashCode()
{
int result = (allowReadingStringAsBinary ? 1 : 0);
result = 31 * result + (allowReadingBinaryAsString ? 1 : 0);
result = 31 * result + (actionOnMalformedString != null ? actionOnMalformedString.hashCode() : 0);
result = 31 * result + (actionOnUnmappableString != null ? actionOnUnmappableString.hashCode() : 0);
result = 31 * result + stringSizeLimit;
result = 31 * result + bufferSize;
result = 31 * result + stringDecoderBufferSize;
return result;
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof UnpackerConfig)) {
return false;
}
UnpackerConfig o = (UnpackerConfig) obj;
return this.allowReadingStringAsBinary == o.allowReadingStringAsBinary
&& this.allowReadingBinaryAsString == o.allowReadingBinaryAsString
&& this.actionOnMalformedString == o.actionOnMalformedString
&& this.actionOnUnmappableString == o.actionOnUnmappableString
&& this.stringSizeLimit == o.stringSizeLimit
&& this.stringDecoderBufferSize == o.stringDecoderBufferSize
&& this.bufferSize == o.bufferSize;
}
/**
* Creates an unpacker that deserializes objects from a specified input.
* <p>
* {@link org.msgpack.core.buffer.MessageBufferInput} is an interface that lets applications customize memory
* allocation of internal buffer of {@link MessageUnpacker}.
*
* @param in The input stream that provides sequence of buffer chunks and optionally reuses them when MessageUnpacker consumed one completely
* @return A new MessageUnpacker instance
*/
public MessageUnpacker newUnpacker(MessageBufferInput in)
{
return new MessageUnpacker(in, this);
}
/**
* Creates an unpacker that deserializes objects from a specified input stream.
* <p>
* Note that you don't have to wrap InputStream in BufferedInputStream because MessageUnpacker has buffering
* internally.
*
* @param in The input stream that provides sequence of bytes
* @return A new MessageUnpacker instance
*/
public MessageUnpacker newUnpacker(InputStream in)
{
return newUnpacker(new InputStreamBufferInput(in, bufferSize));
}
/**
* Creates an unpacker that deserializes objects from a specified readable channel.
*
* @param channel The input channel that provides sequence of bytes
* @return A new MessageUnpacker instance
*/
public MessageUnpacker newUnpacker(ReadableByteChannel channel)
{
return newUnpacker(new ChannelBufferInput(channel, bufferSize));
}
/**
* Creates an unpacker that deserializes objects from a specified byte array.
* <p>
* This method provides an optimized implementation of <code>newDefaultUnpacker(new ByteArrayInputStream(contents))</code>.
*
* @param contents The byte array that contains packed objects in MessagePack format
* @return A new MessageUnpacker instance that will never throw IOException
*/
public MessageUnpacker newUnpacker(byte[] contents)
{
return newUnpacker(new ArrayBufferInput(contents));
}
/**
* Creates an unpacker that deserializes objects from subarray of a specified byte array.
* <p>
* This method provides an optimized implementation of <code>newDefaultUnpacker(new ByteArrayInputStream(contents, offset, length))</code>.
*
* @param contents The byte array that contains packed objects
* @param offset The index of the first byte
* @param length The number of bytes
* @return A new MessageUnpacker instance that will never throw IOException
*/
public MessageUnpacker newUnpacker(byte[] contents, int offset, int length)
{
return newUnpacker(new ArrayBufferInput(contents, offset, length));
}
/**
* Creates an unpacker that deserializes objects from a specified ByteBuffer.
* <p>
* Note that the returned unpacker reads data from the current position of the ByteBuffer until its limit.
* However, its position does not change when unpacker reads data. You may use
* {@link MessageUnpacker#getTotalReadBytes()} to get actual amount of bytes used in ByteBuffer.
*
* @param contents The byte buffer that contains packed objects
* @return A new MessageUnpacker instance that will never throw IOException
*/
public MessageUnpacker newUnpacker(ByteBuffer contents)
{
return newUnpacker(new ByteBufferInput(contents));
}
/**
* Allows unpackBinaryHeader to read str format family (default: true)
*/
public UnpackerConfig withAllowReadingStringAsBinary(boolean enable)
{
UnpackerConfig copy = clone();
copy.allowReadingStringAsBinary = enable;
return copy;
}
public boolean getAllowReadingStringAsBinary()
{
return allowReadingStringAsBinary;
}
/**
* Allows unpackString and unpackRawStringHeader and unpackString to read bin format family (default: true)
*/
public UnpackerConfig withAllowReadingBinaryAsString(boolean enable)
{
UnpackerConfig copy = clone();
copy.allowReadingBinaryAsString = enable;
return copy;
}
public boolean getAllowReadingBinaryAsString()
{
return allowReadingBinaryAsString;
}
/**
* Sets action when encountered a malformed input (default: REPLACE)
*/
public UnpackerConfig withActionOnMalformedString(CodingErrorAction action)
{
UnpackerConfig copy = clone();
copy.actionOnMalformedString = action;
return copy;
}
public CodingErrorAction getActionOnMalformedString()
{
return actionOnMalformedString;
}
/**
* Sets action when an unmappable character is found (default: REPLACE)
*/
public UnpackerConfig withActionOnUnmappableString(CodingErrorAction action)
{
UnpackerConfig copy = clone();
copy.actionOnUnmappableString = action;
return copy;
}
public CodingErrorAction getActionOnUnmappableString()
{
return actionOnUnmappableString;
}
/**
* unpackString size limit (default: Integer.MAX_VALUE).
*/
public UnpackerConfig withStringSizeLimit(int bytes)
{
UnpackerConfig copy = clone();
copy.stringSizeLimit = bytes;
return copy;
}
public int getStringSizeLimit()
{
return stringSizeLimit;
}
/**
*
*/
public UnpackerConfig withStringDecoderBufferSize(int bytes)
{
UnpackerConfig copy = clone();
copy.stringDecoderBufferSize = bytes;
return copy;
}
public int getStringDecoderBufferSize()
{
return stringDecoderBufferSize;
}
/**
* When a packer is created with newUnpacker(OutputStream) or newUnpacker(WritableByteChannel), the stream will be
* buffered with this size of buffer (default: 8192).
*/
public UnpackerConfig withBufferSize(int bytes)
{
UnpackerConfig copy = clone();
copy.bufferSize = bytes;
return copy;
}
public int getBufferSize()
{
return bufferSize;
}
}
}