-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PackStream.java
708 lines (645 loc) · 24.8 KB
/
PackStream.java
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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt.v1.packstream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.neo4j.bolt.v1.packstream.utf8.UTF8Encoder;
/**
* PackStream is a messaging serialisation format heavily inspired by MessagePack.
* The key differences are in the type system itself which (among other things) replaces extensions with structures.
* The Packer and Unpacker implementations are also faster than their MessagePack counterparts.
* <p/>
* Note that several marker byte values are RESERVED for future use.
* Extra markers should <em>not</em> be added casually and such additions must be follow a strict process involving both
* client and server software.
* <p/>
* The table below shows all allocated marker byte values.
* <p/>
* <table>
* <tr><th>Marker</th><th>Binary</th><th>Type</th><th>Description</th></tr>
* <tr><td><code>00..7F</code></td><td><code>0xxxxxxx</code></td><td>+TINY_INT</td><td>Integer 0 to 127</td></tr>
* <tr><td><code>80..8F</code></td><td><code>1000xxxx</code></td><td>TINY_STRING</td><td></td></tr>
* <tr><td><code>90..9F</code></td><td><code>1001xxxx</code></td><td>TINY_LIST</td><td></td></tr>
* <tr><td><code>A0..AF</code></td><td><code>1010xxxx</code></td><td>TINY_MAP</td><td></td></tr>
* <tr><td><code>B0..BF</code></td><td><code>1011xxxx</code></td><td>TINY_STRUCT</td><td></td></tr>
* <tr><td><code>C0</code></td><td><code>11000000</code></td><td>NULL</td><td></td></tr>
* <tr><td><code>C1</code></td><td><code>11000001</code></td><td>FLOAT_64</td><td>64-bit floating point number
* (double)</td></tr>
* <tr><td><code>C2</code></td><td><code>11000010</code></td><td>FALSE</td><td>Boolean false</td></tr>
* <tr><td><code>C3</code></td><td><code>11000011</code></td><td>TRUE</td><td>Boolean true</td></tr>
* <tr><td><code>C4..C7</code></td><td><code>110001xx</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>C8</code></td><td><code>11001000</code></td><td>INT_8</td><td>8-bit signed integer</td></tr>
* <tr><td><code>C9</code></td><td><code>11001001</code></td><td>INT_8</td><td>16-bit signed integer</td></tr>
* <tr><td><code>CA</code></td><td><code>11001010</code></td><td>INT_8</td><td>32-bit signed integer</td></tr>
* <tr><td><code>CB</code></td><td><code>11001011</code></td><td>INT_8</td><td>64-bit signed integer</td></tr>
* <tr><td><code>CC</code></td><td><code>11001100</code></td><td>BYTES_8</td><td>Byte string (fewer than 2<sup>8</sup>
* bytes)</td></tr>
* <tr><td><code>CD</code></td><td><code>11001101</code></td><td>BYTES_16</td><td>Byte string (fewer than 2<sup>16</sup>
* bytes)</td></tr>
* <tr><td><code>CE</code></td><td><code>11001110</code></td><td>BYTES_32</td><td>Byte string (fewer than 2<sup>32</sup>
* bytes)</td></tr>
* <tr><td><code>CF</code></td><td><code>11001111</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>D0</code></td><td><code>11010000</code></td><td>STRING_8</td><td>UTF-8 encoded string (fewer than
* 2<sup>8</sup> bytes)</td></tr>
* <tr><td><code>D1</code></td><td><code>11010001</code></td><td>STRING_16</td><td>UTF-8 encoded string (fewer than
* 2<sup>16</sup> bytes)</td></tr>
* <tr><td><code>D2</code></td><td><code>11010010</code></td><td>STRING_32</td><td>UTF-8 encoded string (fewer than
* 2<sup>32</sup> bytes)</td></tr>
* <tr><td><code>D3</code></td><td><code>11010011</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>D4</code></td><td><code>11010100</code></td><td>LIST_8</td><td>List (fewer than 2<sup>8</sup>
* items)</td></tr>
* <tr><td><code>D5</code></td><td><code>11010101</code></td><td>LIST_16</td><td>List (fewer than 2<sup>16</sup>
* items)</td></tr>
* <tr><td><code>D6</code></td><td><code>11010110</code></td><td>LIST_32</td><td>List (fewer than 2<sup>32</sup>
* items)</td></tr>
* <tr><td><code>D7</code></td><td><code>11010111</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>D8</code></td><td><code>11011000</code></td><td>MAP_8</td><td>Map (fewer than 2<sup>8</sup> key:value
* pairs)</td></tr>
* <tr><td><code>D9</code></td><td><code>11011001</code></td><td>MAP_16</td><td>Map (fewer than 2<sup>16</sup> key:value
* pairs)</td></tr>
* <tr><td><code>DA</code></td><td><code>11011010</code></td><td>MAP_32</td><td>Map (fewer than 2<sup>32</sup> key:value
* pairs)</td></tr>
* <tr><td><code>DB</code></td><td><code>11011011</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>DC</code></td><td><code>11011100</code></td><td>STRUCT_8</td><td>Structure (fewer than 2<sup>8</sup>
* fields)</td></tr>
* <tr><td><code>DD</code></td><td><code>11011101</code></td><td>STRUCT_16</td><td>Structure (fewer than 2<sup>16</sup>
* fields)</td></tr>
* <tr><td><code>DE</code></td><td><code>11011110</code></td><td>STRUCT_32</td><td>Structure (fewer than 2<sup>32</sup>
* fields)</td></tr>
* <tr><td><code>DF</code></td><td><code>11011111</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>E0..EF</code></td><td><code>1110xxxx</code></td><td><em>RESERVED</em></td><td></td></tr>
* <tr><td><code>F0..FF</code></td><td><code>1111xxxx</code></td><td>-TINY_INT</td><td>Integer -1 to -16</td></tr>
* </table>
*/
public class PackStream
{
public static final byte TINY_STRING = (byte) 0x80;
public static final byte TINY_LIST = (byte) 0x90;
public static final byte TINY_MAP = (byte) 0xA0;
public static final byte TINY_STRUCT = (byte) 0xB0;
public static final byte NULL = (byte) 0xC0;
public static final byte FLOAT_64 = (byte) 0xC1;
public static final byte FALSE = (byte) 0xC2;
public static final byte TRUE = (byte) 0xC3;
public static final byte RESERVED_C4 = (byte) 0xC4;
public static final byte RESERVED_C5 = (byte) 0xC5;
public static final byte RESERVED_C6 = (byte) 0xC6;
public static final byte RESERVED_C7 = (byte) 0xC7;
public static final byte INT_8 = (byte) 0xC8;
public static final byte INT_16 = (byte) 0xC9;
public static final byte INT_32 = (byte) 0xCA;
public static final byte INT_64 = (byte) 0xCB;
public static final byte BYTES_8 = (byte) 0xCC;
public static final byte BYTES_16 = (byte) 0xCD;
public static final byte BYTES_32 = (byte) 0xCE;
public static final byte RESERVED_CF = (byte) 0xCF;
public static final byte STRING_8 = (byte) 0xD0;
public static final byte STRING_16 = (byte) 0xD1;
public static final byte STRING_32 = (byte) 0xD2;
public static final byte RESERVED_D3 = (byte) 0xD3;
public static final byte LIST_8 = (byte) 0xD4;
public static final byte LIST_16 = (byte) 0xD5;
public static final byte LIST_32 = (byte) 0xD6;
public static final byte LIST_STREAM = (byte) 0xD7;
public static final byte MAP_8 = (byte) 0xD8;
public static final byte MAP_16 = (byte) 0xD9;
public static final byte MAP_32 = (byte) 0xDA;
public static final byte MAP_STREAM = (byte) 0xDB;
public static final byte STRUCT_8 = (byte) 0xDC;
public static final byte STRUCT_16 = (byte) 0xDD;
public static final byte RESERVED_DE = (byte) 0xDE;
public static final byte END_OF_STREAM = (byte) 0xDF;
public static final byte RESERVED_E0 = (byte) 0xE0;
public static final byte RESERVED_E1 = (byte) 0xE1;
public static final byte RESERVED_E2 = (byte) 0xE2;
public static final byte RESERVED_E3 = (byte) 0xE3;
public static final byte RESERVED_E4 = (byte) 0xE4;
public static final byte RESERVED_E5 = (byte) 0xE5;
public static final byte RESERVED_E6 = (byte) 0xE6;
public static final byte RESERVED_E7 = (byte) 0xE7;
public static final byte RESERVED_E8 = (byte) 0xE8;
public static final byte RESERVED_E9 = (byte) 0xE9;
public static final byte RESERVED_EA = (byte) 0xEA;
public static final byte RESERVED_EB = (byte) 0xEB;
public static final byte RESERVED_EC = (byte) 0xEC;
public static final byte RESERVED_ED = (byte) 0xED;
public static final byte RESERVED_EE = (byte) 0xEE;
public static final byte RESERVED_EF = (byte) 0xEF;
public static final long UNKNOWN_SIZE = -1;
private static final long PLUS_2_TO_THE_31 = 2147483648L;
private static final long PLUS_2_TO_THE_15 = 32768L;
private static final long PLUS_2_TO_THE_7 = 128L;
private static final long MINUS_2_TO_THE_4 = -16L;
private static final long MINUS_2_TO_THE_7 = -128L;
private static final long MINUS_2_TO_THE_15 = -32768L;
private static final long MINUS_2_TO_THE_31 = -2147483648L;
private PackStream()
{
}
public static class Packer
{
private PackOutput out;
private UTF8Encoder utf8 = UTF8Encoder.fastestAvailableEncoder();
public Packer( PackOutput out )
{
this.out = out;
}
public void flush() throws IOException
{
out.flush();
}
public void packNull() throws IOException
{
out.writeByte( NULL );
}
public void pack( boolean value ) throws IOException
{
out.writeByte( value ? TRUE : FALSE );
}
public void pack( long value ) throws IOException
{
if ( value >= MINUS_2_TO_THE_4 && value < PLUS_2_TO_THE_7 )
{
out.writeByte( (byte) value );
}
else if ( value >= MINUS_2_TO_THE_7 && value < MINUS_2_TO_THE_4 )
{
out.writeByte( INT_8 ).writeByte( (byte)value );
}
else if ( value >= MINUS_2_TO_THE_15 && value < PLUS_2_TO_THE_15 )
{
out.writeByte( INT_16 ).writeShort( (short) value );
}
else if ( value >= MINUS_2_TO_THE_31 && value < PLUS_2_TO_THE_31 )
{
out.writeByte( INT_32 ).writeInt( (int) value );
}
else
{
out.writeByte( INT_64 ).writeLong( value );
}
}
public void pack( double value ) throws IOException
{
out.writeByte( FLOAT_64 ).writeDouble( value );
}
public void pack( char character ) throws IOException
{
pack( new String( new char[]{character} ) );
}
public void pack( String value ) throws IOException
{
if ( value == null ) { packNull(); }
else
{
ByteBuffer encoded = utf8.encode( value );
packStringHeader( encoded.remaining() );
out.writeBytes( encoded );
}
}
private void packStringHeader( int size ) throws IOException
{
if ( size < 0x10 )
{
out.writeByte( (byte) (TINY_STRING | size) );
}
else if ( size <= Byte.MAX_VALUE )
{
out.writeShort( (short) (STRING_8 << 8 | size) );
}
else if ( size <= Short.MAX_VALUE )
{
out.writeByte( STRING_16 ).writeShort( (short) size );
}
else
{
out.writeByte( STRING_32 ).writeInt( size );
}
}
public void packListHeader( int size ) throws IOException
{
if ( size < 0x10 )
{
out.writeByte( (byte) (TINY_LIST | size) );
}
else if ( size <= Byte.MAX_VALUE )
{
out.writeShort( (short) (LIST_8 << 8 | size) );
}
else if ( size <= Short.MAX_VALUE )
{
out.writeByte( LIST_16 ).writeShort( (short) size );
}
else
{
out.writeByte( LIST_32 ).writeInt( size );
}
}
public void packListStreamHeader() throws IOException
{
out.writeByte( LIST_STREAM );
}
public void packMapHeader( int size ) throws IOException
{
if ( size < 0x10 )
{
out.writeByte( (byte) (TINY_MAP | size) );
}
else if ( size <= Byte.MAX_VALUE )
{
out.writeShort( (short) (MAP_8 << 8 | size) );
}
else if ( size <= Short.MAX_VALUE )
{
out.writeByte( MAP_16 ).writeShort( (short) size );
}
else
{
out.writeByte( MAP_32 ).writeInt( size );
}
}
public void packMapStreamHeader() throws IOException
{
out.writeByte( MAP_STREAM );
}
public void packStructHeader( int size, byte signature ) throws IOException
{
if ( size < 0x10 )
{
out.writeShort( (short) ((byte) (TINY_STRUCT | size) << 8 | (signature & 0xFF)) );
}
else if ( size <= Byte.MAX_VALUE )
{
out.writeByte( STRUCT_8 ).writeByte( (byte) size ).writeByte( signature );
}
else if ( size <= Short.MAX_VALUE )
{
out.writeByte( STRUCT_16 ).writeShort( (short) size ).writeByte( signature );
}
else
{
throw new Overflow( "Structures cannot have more than " + Short.MAX_VALUE + " fields" );
}
}
public void packEndOfStream() throws IOException
{
out.writeByte( END_OF_STREAM );
}
}
public static class Unpacker
{
private static final byte[] EMPTY_BYTE_ARRAY = {};
private PackInput in;
public Unpacker( PackInput in )
{
this.in = in;
}
public boolean hasNext() throws IOException
{
return in.hasMoreData();
}
// TODO: This currently returns the number of fields in the struct. In 99% of cases we will look at the struct
// signature to determine how to read it, suggest we make that what we return here,
// and have the number of fields available through some alternate optional mechanism.
public long unpackStructHeader() throws IOException
{
final byte markerByte = in.readByte();
final byte markerHighNibble = (byte) (markerByte & 0xF0);
final byte markerLowNibble = (byte) (markerByte & 0x0F);
if ( markerHighNibble == TINY_STRUCT ) { return markerLowNibble; }
switch ( markerByte )
{
case STRUCT_8:
return unpackUINT8();
case STRUCT_16:
return unpackUINT16();
default:
throw new Unexpected( PackType.STRUCT, markerByte );
}
}
public char unpackStructSignature() throws IOException
{
return (char) in.readByte();
}
public long unpackListHeader() throws IOException
{
final byte markerByte = in.readByte();
final byte markerHighNibble = (byte) (markerByte & 0xF0);
final byte markerLowNibble = (byte) (markerByte & 0x0F);
if ( markerHighNibble == TINY_LIST ) { return markerLowNibble; }
switch ( markerByte )
{
case LIST_8:
return unpackUINT8();
case LIST_16:
return unpackUINT16();
case LIST_32:
return unpackUINT32();
case LIST_STREAM:
return UNKNOWN_SIZE;
default:
throw new Unexpected( PackType.LIST, markerByte);
}
}
public long unpackMapHeader() throws IOException
{
final byte markerByte = in.readByte();
final byte markerHighNibble = (byte) (markerByte & 0xF0);
final byte markerLowNibble = (byte) (markerByte & 0x0F);
if ( markerHighNibble == TINY_MAP ) { return markerLowNibble; }
switch ( markerByte )
{
case MAP_8:
return unpackUINT8();
case MAP_16:
return unpackUINT16();
case MAP_32:
return unpackUINT32();
case MAP_STREAM:
return UNKNOWN_SIZE;
default:
throw new Unexpected( PackType.MAP, markerByte);
}
}
public int unpackInteger() throws IOException
{
final byte markerByte = in.readByte();
if ( markerByte >= MINUS_2_TO_THE_4 ) { return markerByte; }
switch ( markerByte )
{
case INT_8:
return in.readByte();
case INT_16:
return in.readShort();
case INT_32:
return in.readInt();
case INT_64:
throw new Overflow(
"Unexpectedly large Integer value unpacked (" + in.readLong() + ")" );
default:
throw new Unexpected( PackType.INTEGER, markerByte);
}
}
public long unpackLong() throws IOException
{
final byte markerByte = in.readByte();
if ( markerByte >= MINUS_2_TO_THE_4 ) { return markerByte; }
switch ( markerByte )
{
case INT_8:
return in.readByte();
case INT_16:
return in.readShort();
case INT_32:
return in.readInt();
case INT_64:
return in.readLong();
default:
throw new Unexpected( PackType.INTEGER, markerByte);
}
}
public double unpackDouble() throws IOException
{
final byte markerByte = in.readByte();
if ( markerByte == FLOAT_64 )
{
return in.readDouble();
}
throw new Unexpected( PackType.FLOAT, markerByte);
}
public String unpackString() throws IOException
{
return new String( unpackUTF8(), StandardCharsets.UTF_8 );
}
public int unpackStringHeader() throws IOException
{
final byte markerByte = in.readByte();
final byte markerHighNibble = (byte) (markerByte & 0xF0);
final byte markerLowNibble = (byte) (markerByte & 0x0F);
int size;
if ( markerHighNibble == TINY_STRING )
{
size = markerLowNibble;
}
else
{
switch ( markerByte )
{
case STRING_8:
size = unpackUINT8();
break;
case STRING_16:
size = unpackUINT16();
break;
case STRING_32:
{
long longSize = unpackUINT32();
if ( longSize <= Integer.MAX_VALUE )
{
size = (int) longSize;
}
else
{
throw new Overflow( "STRING_32 too long for Java" );
}
break;
}
default:
throw new Unexpected( PackType.STRING, markerByte );
}
}
return size;
}
public byte[] unpackUTF8() throws IOException
{
int size = unpackStringHeader();
return unpackRawBytes( size );
}
public boolean unpackBoolean() throws IOException
{
final byte markerByte = in.readByte();
switch ( markerByte )
{
case TRUE:
return true;
case FALSE:
return false;
default:
throw new Unexpected( PackType.BOOLEAN, markerByte);
}
}
public void unpackNull() throws IOException
{
final byte markerByte = in.readByte();
assert markerByte == NULL;
}
private int unpackUINT8() throws IOException
{
return in.readByte() & 0xFF;
}
private int unpackUINT16() throws IOException
{
return in.readShort() & 0xFFFF;
}
private long unpackUINT32() throws IOException
{
return in.readInt() & 0xFFFFFFFFL;
}
public void unpackEndOfStream() throws IOException
{
final byte markerByte = in.readByte();
assert markerByte == END_OF_STREAM;
}
private void discardRawBytes( int size ) throws IOException
{
for ( int i = 0; i < size; i++ )
{
in.readByte();
}
}
private byte[] unpackRawBytes( int size ) throws IOException
{
if ( size == 0 )
{
return EMPTY_BYTE_ARRAY;
}
else
{
byte[] heapBuffer = new byte[size];
unpackRawBytesInto( heapBuffer, 0, heapBuffer.length );
return heapBuffer;
}
}
private void unpackRawBytesInto( byte[] buffer, int offset, int size ) throws IOException
{
if ( size > 0 )
{
in.readBytes( buffer, offset, size );
}
}
public PackType peekNextType() throws IOException
{
final byte markerByte = in.peekByte();
return type( markerByte );
}
}
private static PackType type( byte markerByte )
{
final byte markerHighNibble = (byte) (markerByte & 0xF0);
switch ( markerHighNibble )
{
case TINY_STRING:
return PackType.STRING;
case TINY_LIST:
return PackType.LIST;
case TINY_MAP:
return PackType.MAP;
case TINY_STRUCT:
return PackType.STRUCT;
}
if ( markerByte >= MINUS_2_TO_THE_4 )
{
return PackType.INTEGER;
}
switch ( markerByte )
{
case NULL:
return PackType.NULL;
case TRUE:
case FALSE:
return PackType.BOOLEAN;
case FLOAT_64:
return PackType.FLOAT;
case BYTES_8:
case BYTES_16:
case BYTES_32:
return PackType.BYTES;
case STRING_8:
case STRING_16:
case STRING_32:
return PackType.STRING;
case LIST_8:
case LIST_16:
case LIST_32:
case LIST_STREAM:
return PackType.LIST;
case MAP_8:
case MAP_16:
case MAP_32:
case MAP_STREAM:
return PackType.MAP;
case STRUCT_8:
case STRUCT_16:
return PackType.STRUCT;
case END_OF_STREAM:
return PackType.END_OF_STREAM;
case INT_8:
case INT_16:
case INT_32:
case INT_64:
return PackType.INTEGER;
default:
return PackType.RESERVED;
}
}
public static class PackStreamException extends IOException
{
public PackStreamException( String message )
{
super( message );
}
}
public static class EndOfStream extends PackStreamException
{
public EndOfStream( String message )
{
super( message );
}
}
public static class Overflow extends PackStreamException
{
public Overflow( String message )
{
super( message );
}
}
public static class Unexpected extends PackStreamException
{
public Unexpected( PackType expectedType, byte unexpectedMarkerByte )
{
super( "Wrong type received. Expected " + expectedType + ", received: " + type(unexpectedMarkerByte) + " " +
"(" + toHexString( unexpectedMarkerByte ) + ").");
}
private static String toHexString( byte unexpectedMarkerByte )
{
String s = Integer.toHexString( unexpectedMarkerByte );
if(s.length() > 2)
{
s = s.substring( 0, 2 );
}
else if(s.length() < 2)
{
return "0" + s;
}
return "0x" + s;
}
}
}