Skip to content

Commit

Permalink
Condense Packer a bit.
Browse files Browse the repository at this point in the history
Trying to be smarter than the GraalVM AoT compiler, but failed. Looks nicer nevertheless.
  • Loading branch information
michael-simons committed Dec 16, 2019
1 parent 8f4973f commit cb7647b
Showing 1 changed file with 42 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import static java.lang.Integer.toHexString;
import static java.lang.String.format;
Expand Down Expand Up @@ -409,17 +411,7 @@ public Unpacker( PackInput in )

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( "Expected a struct, but got: " + toHexString( markerByte ));
}
return unpackHeaderImpl( TINY_STRUCT, markerByteAsHexString -> "Expected a struct, but got: " + markerByteAsHexString );
}

public byte unpackStructSignature() throws IOException
Expand All @@ -429,33 +421,32 @@ public byte unpackStructSignature() throws IOException

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();
default: throw new Unexpected( "Expected a list, but got: " + toHexString( markerByte & 0xFF ));
}
return unpackHeaderImpl( TINY_LIST, markerByteAsHexString -> "Expected a list, but got: " + markerByteAsHexString );
}

public long unpackMapHeader() throws IOException
{
return unpackHeaderImpl( TINY_MAP, markerByteAsHexString -> "Expected a map, but got: " + markerByteAsHexString );
}

private long unpackHeaderImpl( byte possibleTinyType, Function<String, String> exceptionmessageSupplier ) throws IOException {

final byte markerByte = in.readByte();
final byte markerHighNibble = (byte) (markerByte & 0xF0);
final byte markerLowNibble = (byte) (markerByte & 0x0F);
final byte markerLowNibble = (byte) (markerByte & 0x0F);

if ( markerHighNibble == TINY_MAP ) { return markerLowNibble; }
if ( markerHighNibble == possibleTinyType ) { return markerLowNibble; }
switch(markerByte)
{
case MAP_8: return unpackUINT8();
case MAP_16: return unpackUINT16();
case MAP_32: return unpackUINT32();
default: throw new Unexpected( "Expected a map, but got: " + toHexString( markerByte ));
case STRUCT_8:
case MAP_8:
case LIST_8: return unpackUINT8();
case STRUCT_16:
case MAP_16:
case LIST_16: return unpackUINT16();
case MAP_32:
case LIST_32: return unpackUINT32();
default: throw new Unexpected( exceptionmessageSupplier.apply( toHexString( markerByte ) ) );
}
}

Expand Down Expand Up @@ -485,24 +476,31 @@ public double unpackDouble() throws IOException

public byte[] unpackBytes() throws IOException
{
final byte markerByte = in.readByte();
return unpackBytesImpl(in.readByte(), markerByteAsHexString -> "Expected bytes, but got: 0x" + markerByteAsHexString );
}

private byte[] unpackBytesImpl( final byte markerByte, Function<String, String> exceptionMessageSupplier ) throws IOException
{
switch(markerByte)
{
case BYTES_8: return unpackRawBytes( unpackUINT8() );
case BYTES_16: return unpackRawBytes( unpackUINT16() );
case BYTES_32:
{
long size = unpackUINT32();
if ( size <= Integer.MAX_VALUE )
{
return unpackRawBytes( (int) size );
}
else
case BYTES_8:
case STRING_8: return unpackRawBytes( unpackUINT8() );
case BYTES_16:
case STRING_16: return unpackRawBytes( unpackUINT16() );
case BYTES_32:
case STRING_32:
{
throw new Overflow( "BYTES_32 too long for Java" );
long size = unpackUINT32();
if ( size <= Integer.MAX_VALUE )
{
return unpackRawBytes( (int) size );
}
else
{
throw new Overflow( markerByte == BYTES_32 ? "BYTES_32 too long for Java" : "STRING_32 too long for Java" );
}
}
}
default: throw new Unexpected( "Expected bytes, but got: 0x" + toHexString( markerByte & 0xFF ));
default: throw new Unexpected( exceptionMessageSupplier.apply( toHexString( markerByte & 0xFF ) ));
}
}

Expand Down Expand Up @@ -540,24 +538,7 @@ private byte[] unpackUtf8(byte markerByte) throws IOException
final byte markerLowNibble = (byte) (markerByte & 0x0F);

if ( markerHighNibble == TINY_STRING ) { return unpackRawBytes( markerLowNibble ); }
switch(markerByte)
{
case STRING_8: return unpackRawBytes( unpackUINT8() );
case STRING_16: return unpackRawBytes( unpackUINT16() );
case STRING_32:
{
long size = unpackUINT32();
if ( size <= Integer.MAX_VALUE )
{
return unpackRawBytes( (int) size );
}
else
{
throw new Overflow( "STRING_32 too long for Java" );
}
}
default: throw new Unexpected( "Expected a string, but got: 0x" + toHexString( markerByte & 0xFF ));
}
return unpackBytesImpl(markerByte, markerByteAsHexString -> "Expected a string, but got: 0x" + markerByteAsHexString );
}

public boolean unpackBoolean() throws IOException
Expand Down

0 comments on commit cb7647b

Please sign in to comment.