@Test void encodePath() { assertEquals(Bytes.of(0x11, 0x23, 0x45), CompactEncoding.encode(Bytes.of(0x01, 0x02, 0x03, 0x04, 0x05))); assertEquals( Bytes.of(0x00, 0x01, 0x23, 0x45), CompactEncoding.encode(Bytes.of(0x00, 0x01, 0x02, 0x03, 0x04, 0x05))); assertEquals( Bytes.of(0x20, 0x0f, 0x1c, 0xb8), CompactEncoding.encode(Bytes.of(0x00, 0x0f, 0x01, 0x0c, 0x0b, 0x08, 0x10))); assertEquals(Bytes.of(0x3f, 0x1c, 0xb8), CompactEncoding.encode(Bytes.of(0x0f, 0x01, 0x0c, 0x0b, 0x08, 0x10))); }
public static Bytes encode(Bytes path) { int size = path.size(); boolean isLeaf = size > 0 && path.get(size - 1) == LEAF_TERMINATOR; if (isLeaf) { size = size - 1; } MutableBytes encoded = MutableBytes.create((size + 2) / 2); int i = 0; int j = 0; if (size % 2 == 1) { byte high = (byte) (isLeaf ? 0x03 : 0x01); byte low = path.get(i++); if ((low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } else { byte high = (byte) (isLeaf ? 0x02 : 0x00); encoded.set(j++, (byte) (high << 4)); } while (i < size) { byte high = path.get(i++); byte low = path.get(i++); if ((high & 0xf0) != 0 || (low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } return encoded; }
CompactEncoding { public static Bytes encode(Bytes path) { int size = path.size(); boolean isLeaf = size > 0 && path.get(size - 1) == LEAF_TERMINATOR; if (isLeaf) { size = size - 1; } MutableBytes encoded = MutableBytes.create((size + 2) / 2); int i = 0; int j = 0; if (size % 2 == 1) { byte high = (byte) (isLeaf ? 0x03 : 0x01); byte low = path.get(i++); if ((low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } else { byte high = (byte) (isLeaf ? 0x02 : 0x00); encoded.set(j++, (byte) (high << 4)); } while (i < size) { byte high = path.get(i++); byte low = path.get(i++); if ((high & 0xf0) != 0 || (low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } return encoded; } }
CompactEncoding { public static Bytes encode(Bytes path) { int size = path.size(); boolean isLeaf = size > 0 && path.get(size - 1) == LEAF_TERMINATOR; if (isLeaf) { size = size - 1; } MutableBytes encoded = MutableBytes.create((size + 2) / 2); int i = 0; int j = 0; if (size % 2 == 1) { byte high = (byte) (isLeaf ? 0x03 : 0x01); byte low = path.get(i++); if ((low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } else { byte high = (byte) (isLeaf ? 0x02 : 0x00); encoded.set(j++, (byte) (high << 4)); } while (i < size) { byte high = path.get(i++); byte low = path.get(i++); if ((high & 0xf0) != 0 || (low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } return encoded; } private  CompactEncoding(); }
CompactEncoding { public static Bytes encode(Bytes path) { int size = path.size(); boolean isLeaf = size > 0 && path.get(size - 1) == LEAF_TERMINATOR; if (isLeaf) { size = size - 1; } MutableBytes encoded = MutableBytes.create((size + 2) / 2); int i = 0; int j = 0; if (size % 2 == 1) { byte high = (byte) (isLeaf ? 0x03 : 0x01); byte low = path.get(i++); if ((low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } else { byte high = (byte) (isLeaf ? 0x02 : 0x00); encoded.set(j++, (byte) (high << 4)); } while (i < size) { byte high = path.get(i++); byte low = path.get(i++); if ((high & 0xf0) != 0 || (low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } return encoded; } private  CompactEncoding(); static Bytes bytesToPath(Bytes bytes); static Bytes encode(Bytes path); static Bytes decode(Bytes encoded); }
CompactEncoding { public static Bytes encode(Bytes path) { int size = path.size(); boolean isLeaf = size > 0 && path.get(size - 1) == LEAF_TERMINATOR; if (isLeaf) { size = size - 1; } MutableBytes encoded = MutableBytes.create((size + 2) / 2); int i = 0; int j = 0; if (size % 2 == 1) { byte high = (byte) (isLeaf ? 0x03 : 0x01); byte low = path.get(i++); if ((low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } else { byte high = (byte) (isLeaf ? 0x02 : 0x00); encoded.set(j++, (byte) (high << 4)); } while (i < size) { byte high = path.get(i++); byte low = path.get(i++); if ((high & 0xf0) != 0 || (low & 0xf0) != 0) { throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble"); } encoded.set(j++, (byte) (high << 4 | low)); } return encoded; } private  CompactEncoding(); static Bytes bytesToPath(Bytes bytes); static Bytes encode(Bytes path); static Bytes decode(Bytes encoded); static final byte LEAF_TERMINATOR; }