@Test void decode() { assertEquals(Bytes.of(0x01, 0x02, 0x03, 0x04, 0x05), CompactEncoding.decode(Bytes.of(0x11, 0x23, 0x45))); assertEquals( Bytes.of(0x00, 0x01, 0x02, 0x03, 0x04, 0x05), CompactEncoding.decode(Bytes.of(0x00, 0x01, 0x23, 0x45))); assertEquals( Bytes.of(0x00, 0x0f, 0x01, 0x0c, 0x0b, 0x08, 0x10), CompactEncoding.decode(Bytes.of(0x20, 0x0f, 0x1c, 0xb8))); assertEquals(Bytes.of(0x0f, 0x01, 0x0c, 0x0b, 0x08, 0x10), CompactEncoding.decode(Bytes.of(0x3f, 0x1c, 0xb8))); }
public static Bytes decode(Bytes encoded) { int size = encoded.size(); checkArgument(size > 0); byte magic = encoded.get(0); checkArgument((magic & 0xc0) == 0, "Invalid compact encoding"); boolean isLeaf = (magic & 0x20) != 0; int pathLength = ((size - 1) * 2) + (isLeaf ? 1 : 0); MutableBytes path; int i = 0; if ((magic & 0x10) != 0) { path = MutableBytes.create(pathLength + 1); path.set(i++, (byte) (magic & 0x0f)); } else { path = MutableBytes.create(pathLength); } for (int j = 1; j < size; j++) { byte b = encoded.get(j); path.set(i++, (byte) ((b >>> 4) & 0x0f)); path.set(i++, (byte) (b & 0x0f)); } if (isLeaf) { path.set(i, LEAF_TERMINATOR); } return path; }
CompactEncoding { public static Bytes decode(Bytes encoded) { int size = encoded.size(); checkArgument(size > 0); byte magic = encoded.get(0); checkArgument((magic & 0xc0) == 0, "Invalid compact encoding"); boolean isLeaf = (magic & 0x20) != 0; int pathLength = ((size - 1) * 2) + (isLeaf ? 1 : 0); MutableBytes path; int i = 0; if ((magic & 0x10) != 0) { path = MutableBytes.create(pathLength + 1); path.set(i++, (byte) (magic & 0x0f)); } else { path = MutableBytes.create(pathLength); } for (int j = 1; j < size; j++) { byte b = encoded.get(j); path.set(i++, (byte) ((b >>> 4) & 0x0f)); path.set(i++, (byte) (b & 0x0f)); } if (isLeaf) { path.set(i, LEAF_TERMINATOR); } return path; } }
CompactEncoding { public static Bytes decode(Bytes encoded) { int size = encoded.size(); checkArgument(size > 0); byte magic = encoded.get(0); checkArgument((magic & 0xc0) == 0, "Invalid compact encoding"); boolean isLeaf = (magic & 0x20) != 0; int pathLength = ((size - 1) * 2) + (isLeaf ? 1 : 0); MutableBytes path; int i = 0; if ((magic & 0x10) != 0) { path = MutableBytes.create(pathLength + 1); path.set(i++, (byte) (magic & 0x0f)); } else { path = MutableBytes.create(pathLength); } for (int j = 1; j < size; j++) { byte b = encoded.get(j); path.set(i++, (byte) ((b >>> 4) & 0x0f)); path.set(i++, (byte) (b & 0x0f)); } if (isLeaf) { path.set(i, LEAF_TERMINATOR); } return path; } private  CompactEncoding(); }
CompactEncoding { public static Bytes decode(Bytes encoded) { int size = encoded.size(); checkArgument(size > 0); byte magic = encoded.get(0); checkArgument((magic & 0xc0) == 0, "Invalid compact encoding"); boolean isLeaf = (magic & 0x20) != 0; int pathLength = ((size - 1) * 2) + (isLeaf ? 1 : 0); MutableBytes path; int i = 0; if ((magic & 0x10) != 0) { path = MutableBytes.create(pathLength + 1); path.set(i++, (byte) (magic & 0x0f)); } else { path = MutableBytes.create(pathLength); } for (int j = 1; j < size; j++) { byte b = encoded.get(j); path.set(i++, (byte) ((b >>> 4) & 0x0f)); path.set(i++, (byte) (b & 0x0f)); } if (isLeaf) { path.set(i, LEAF_TERMINATOR); } return path; } private  CompactEncoding(); static Bytes bytesToPath(Bytes bytes); static Bytes encode(Bytes path); static Bytes decode(Bytes encoded); }
CompactEncoding { public static Bytes decode(Bytes encoded) { int size = encoded.size(); checkArgument(size > 0); byte magic = encoded.get(0); checkArgument((magic & 0xc0) == 0, "Invalid compact encoding"); boolean isLeaf = (magic & 0x20) != 0; int pathLength = ((size - 1) * 2) + (isLeaf ? 1 : 0); MutableBytes path; int i = 0; if ((magic & 0x10) != 0) { path = MutableBytes.create(pathLength + 1); path.set(i++, (byte) (magic & 0x0f)); } else { path = MutableBytes.create(pathLength); } for (int j = 1; j < size; j++) { byte b = encoded.get(j); path.set(i++, (byte) ((b >>> 4) & 0x0f)); path.set(i++, (byte) (b & 0x0f)); } if (isLeaf) { path.set(i, LEAF_TERMINATOR); } return path; } private  CompactEncoding(); static Bytes bytesToPath(Bytes bytes); static Bytes encode(Bytes path); static Bytes decode(Bytes encoded); static final byte LEAF_TERMINATOR; }