CompactEncoder { public static byte[] unpackToNibbles(byte[] str) { byte[] base = binToNibbles(str); base = copyOf(base, base.length - 1); if (base[0] >= 2) { base = appendByte(base, TERMINATOR); } if (base[0] % 2 == 1) { base = copyOfRange(base, 1, base.length); } else { base = copyOfRange(base, 2, base.length); } return base; }  static byte[] packNibbles(byte[] nibbles); static byte[] unpackToNibbles(byte[] str); static byte[] binToNibbles(byte[] str); static byte[] binToNibblesNoTerminator(byte[] str);  }
@Test public void testCompactDecodeOddCompact() { byte[] test = new byte[]{0x11, 0x23, 0x45}; byte[] expected = new byte[]{1, 2, 3, 4, 5}; assertArrayEquals("odd compact decode fail", expected, CompactEncoder.unpackToNibbles(test)); }
@Test public void testCompactDecodeEvenCompact() { byte[] test = new byte[]{0x00, 0x01, 0x23, 0x45}; byte[] expected = new byte[]{0, 1, 2, 3, 4, 5}; assertArrayEquals("even compact decode fail", expected, CompactEncoder.unpackToNibbles(test)); }
@Test public void testCompactDecodeEvenTerminated() { byte[] test = new byte[]{0x20, 0x0f, 0x1c, (byte) 0xb8}; byte[] expected = new byte[]{0, 15, 1, 12, 11, 8, T}; assertArrayEquals("even terminated compact decode fail", expected, CompactEncoder.unpackToNibbles(test)); }
@Test public void testCompactDecodeOddTerminated() { byte[] test = new byte[]{0x3f, 0x1c, (byte) 0xb8}; byte[] expected = new byte[]{15, 1, 12, 11, 8, T}; assertArrayEquals("odd terminated compact decode fail", expected, CompactEncoder.unpackToNibbles(test)); }