LongCodec implements BlockCodec<Long> { @Override public byte[] encode(Long data, int bitLength) throws BlockCodecException { long value = data.longValue(); try { byte[] temp = new byte[] { (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32), (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; int byteStart = 8 - (int) Math.ceil((double) bitLength / 8); int bitStart = bitLength % 8; if (bitStart != 0) { bitStart = 8 - bitStart; } return ByteUtils.copyBits(temp, byteStart, bitStart, bitLength); } catch (Exception ex) { throw new BlockCodecException("long encode failure. " + ex.getMessage(), ex); } }  @Override Long zeroValue(); @Override Long decode(byte[] data, int bitLength); @Override byte[] encode(Long data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testEncode() throws Exception { LongCodec codec = new LongCodec(); Assert.assertArrayEquals(new byte[] { (byte) 0x7f }, codec.encode(127L, 8)); Assert.assertArrayEquals(new byte[] { (byte) 0xfe }, codec.encode(127L, 7)); Assert.assertArrayEquals(new byte[] { 0x00, (byte) 0x7f }, codec.encode(127L, 16)); Assert.assertArrayEquals(new byte[] { (byte) 0x80 }, codec.encode(-128L, 8)); Assert.assertArrayEquals(new byte[] { (byte) 0xff, (byte) 0x00 }, codec.encode(-128L, 15)); Assert.assertArrayEquals(new byte[] { (byte) 0xff, (byte) 0x80 }, codec.encode(-128L, 16)); }