IntegerCoder { public static ByteBuffer encodeLong(long value, int minBytes) { assert minBytes >= 1 && minBytes <= 8; byte[] buf = new byte[9]; BitOps.putLongAsLittleEndian(buf, 1, value); int count = buf.length - 1; while (count > minBytes && buf[count] == 0) { count--; } int firstByteValue = 0xFF & (1 << (7 - count + minBytes)); if ((0xFF & buf[count]) >= firstByteValue) { buf[0] = (byte) ~ (firstByteValue - 1); count++; } else if (count > minBytes) { buf[0] = (byte) ((~ (firstByteValue * 2 - 1)) | buf[count]); } else { buf[0] = buf[count]; } return ByteBuffer.wrap(buf, 0, count); } private  IntegerCoder(); static ByteBuffer encodeLong(long value, int minBytes); static long decodeLong(Readable src, int minBytes);  }
@Test public void testEncodingCorrectness() { ByteBuffer actual = IntegerCoder.encodeLong(_input, _minBytes); assertEquals(String.format("encode %d -> %s, expected %s (minBytes=%d)", _input, Text.byteBufferToString(actual), Text.byteBufferToString(_expected), _minBytes), _expected, actual); }