22
22
import java .nio .charset .Charset ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
+ import java .util .function .Function ;
26
+ import java .util .function .Supplier ;
25
27
26
28
import static java .lang .Integer .toHexString ;
27
29
import static java .lang .String .format ;
@@ -409,17 +411,7 @@ public Unpacker( PackInput in )
409
411
410
412
public long unpackStructHeader () throws IOException
411
413
{
412
- final byte markerByte = in .readByte ();
413
- final byte markerHighNibble = (byte ) (markerByte & 0xF0 );
414
- final byte markerLowNibble = (byte ) (markerByte & 0x0F );
415
-
416
- if ( markerHighNibble == TINY_STRUCT ) { return markerLowNibble ; }
417
- switch (markerByte )
418
- {
419
- case STRUCT_8 : return unpackUINT8 ();
420
- case STRUCT_16 : return unpackUINT16 ();
421
- default : throw new Unexpected ( "Expected a struct, but got: " + toHexString ( markerByte ));
422
- }
414
+ return unpackHeaderImpl ( TINY_STRUCT , markerByteAsHexString -> "Expected a struct, but got: " + markerByteAsHexString );
423
415
}
424
416
425
417
public byte unpackStructSignature () throws IOException
@@ -429,33 +421,32 @@ public byte unpackStructSignature() throws IOException
429
421
430
422
public long unpackListHeader () throws IOException
431
423
{
432
- final byte markerByte = in .readByte ();
433
- final byte markerHighNibble = (byte ) (markerByte & 0xF0 );
434
- final byte markerLowNibble = (byte ) (markerByte & 0x0F );
435
-
436
- if ( markerHighNibble == TINY_LIST ) { return markerLowNibble ; }
437
- switch (markerByte )
438
- {
439
- case LIST_8 : return unpackUINT8 ();
440
- case LIST_16 : return unpackUINT16 ();
441
- case LIST_32 : return unpackUINT32 ();
442
- default : throw new Unexpected ( "Expected a list, but got: " + toHexString ( markerByte & 0xFF ));
443
- }
424
+ return unpackHeaderImpl ( TINY_LIST , markerByteAsHexString -> "Expected a list, but got: " + markerByteAsHexString );
444
425
}
445
426
446
427
public long unpackMapHeader () throws IOException
447
428
{
429
+ return unpackHeaderImpl ( TINY_MAP , markerByteAsHexString -> "Expected a map, but got: " + markerByteAsHexString );
430
+ }
431
+
432
+ private long unpackHeaderImpl ( byte possibleTinyType , Function <String , String > exceptionmessageSupplier ) throws IOException {
433
+
448
434
final byte markerByte = in .readByte ();
449
435
final byte markerHighNibble = (byte ) (markerByte & 0xF0 );
450
- final byte markerLowNibble = (byte ) (markerByte & 0x0F );
436
+ final byte markerLowNibble = (byte ) (markerByte & 0x0F );
451
437
452
- if ( markerHighNibble == TINY_MAP ) { return markerLowNibble ; }
438
+ if ( markerHighNibble == possibleTinyType ) { return markerLowNibble ; }
453
439
switch (markerByte )
454
440
{
455
- case MAP_8 : return unpackUINT8 ();
456
- case MAP_16 : return unpackUINT16 ();
457
- case MAP_32 : return unpackUINT32 ();
458
- default : throw new Unexpected ( "Expected a map, but got: " + toHexString ( markerByte ));
441
+ case STRUCT_8 :
442
+ case MAP_8 :
443
+ case LIST_8 : return unpackUINT8 ();
444
+ case STRUCT_16 :
445
+ case MAP_16 :
446
+ case LIST_16 : return unpackUINT16 ();
447
+ case MAP_32 :
448
+ case LIST_32 : return unpackUINT32 ();
449
+ default : throw new Unexpected ( exceptionmessageSupplier .apply ( toHexString ( markerByte ) ) );
459
450
}
460
451
}
461
452
@@ -485,24 +476,31 @@ public double unpackDouble() throws IOException
485
476
486
477
public byte [] unpackBytes () throws IOException
487
478
{
488
- final byte markerByte = in .readByte ();
479
+ return unpackBytesImpl (in .readByte (), markerByteAsHexString -> "Expected bytes, but got: 0x" + markerByteAsHexString );
480
+ }
481
+
482
+ private byte [] unpackBytesImpl ( final byte markerByte , Function <String , String > exceptionMessageSupplier ) throws IOException
483
+ {
489
484
switch (markerByte )
490
485
{
491
- case BYTES_8 : return unpackRawBytes ( unpackUINT8 () );
492
- case BYTES_16 : return unpackRawBytes ( unpackUINT16 () );
493
- case BYTES_32 :
494
- {
495
- long size = unpackUINT32 ();
496
- if ( size <= Integer .MAX_VALUE )
497
- {
498
- return unpackRawBytes ( (int ) size );
499
- }
500
- else
486
+ case BYTES_8 :
487
+ case STRING_8 : return unpackRawBytes ( unpackUINT8 () );
488
+ case BYTES_16 :
489
+ case STRING_16 : return unpackRawBytes ( unpackUINT16 () );
490
+ case BYTES_32 :
491
+ case STRING_32 :
501
492
{
502
- throw new Overflow ( "BYTES_32 too long for Java" );
493
+ long size = unpackUINT32 ();
494
+ if ( size <= Integer .MAX_VALUE )
495
+ {
496
+ return unpackRawBytes ( (int ) size );
497
+ }
498
+ else
499
+ {
500
+ throw new Overflow ( markerByte == BYTES_32 ? "BYTES_32 too long for Java" : "STRING_32 too long for Java" );
501
+ }
503
502
}
504
- }
505
- default : throw new Unexpected ( "Expected bytes, but got: 0x" + toHexString ( markerByte & 0xFF ));
503
+ default : throw new Unexpected ( exceptionMessageSupplier .apply ( toHexString ( markerByte & 0xFF ) ));
506
504
}
507
505
}
508
506
@@ -540,24 +538,7 @@ private byte[] unpackUtf8(byte markerByte) throws IOException
540
538
final byte markerLowNibble = (byte ) (markerByte & 0x0F );
541
539
542
540
if ( markerHighNibble == TINY_STRING ) { return unpackRawBytes ( markerLowNibble ); }
543
- switch (markerByte )
544
- {
545
- case STRING_8 : return unpackRawBytes ( unpackUINT8 () );
546
- case STRING_16 : return unpackRawBytes ( unpackUINT16 () );
547
- case STRING_32 :
548
- {
549
- long size = unpackUINT32 ();
550
- if ( size <= Integer .MAX_VALUE )
551
- {
552
- return unpackRawBytes ( (int ) size );
553
- }
554
- else
555
- {
556
- throw new Overflow ( "STRING_32 too long for Java" );
557
- }
558
- }
559
- default : throw new Unexpected ( "Expected a string, but got: 0x" + toHexString ( markerByte & 0xFF ));
560
- }
541
+ return unpackBytesImpl (markerByte , markerByteAsHexString -> "Expected a string, but got: 0x" + markerByteAsHexString );
561
542
}
562
543
563
544
public boolean unpackBoolean () throws IOException
0 commit comments