IntegerLSBCodec implements BlockCodec<Integer> { @Override public Integer decode(byte[] data, int bitLength) throws BlockCodecException { try { byte[] value = ByteUtils.copyBits(data, 0, bitLength); value = ByteUtils.reverse(value); int bitStart = bitLength % 8; if (bitStart != 0) { bitStart = 8 - bitStart; value[0] = (byte) (value[0] >>> bitStart); value = ByteUtils.copyBits(value, 0, bitStart, bitLength); } return new Integer(ByteUtils.intValue(value, bitLength, this.unsigned)); } catch (Exception ex) { throw new BlockCodecException("integer(LSB) decode failure. " + ex.getMessage(), ex); } } IntegerLSBCodec();  IntegerLSBCodec(boolean unsigned); void setUnsigned(String yn); String getUnsigned(); @Override Integer zeroValue(); @Override Integer decode(byte[] data, int bitLength); @Override byte[] encode(Integer data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testDecode() throws Exception { IntegerLSBCodec codec = new IntegerLSBCodec(); Assert.assertEquals(-1, codec.decode(new byte[] { (byte) 0xfe }, 7), 0); Assert.assertEquals(-2, codec.decode(new byte[] { (byte) 0xfe, (byte) 0xff }, 16), 0); Assert.assertEquals(-255, codec.decode(new byte[] { (byte) 0x01, (byte) 0xcf }, 10), 0); Assert.assertEquals(-4, codec.decode(new byte[] { (byte) 0xfc, (byte) 0xff, (byte) 0xff }, 24), 0); codec.reset(); }