IntegerStringCodec implements BlockCodec<Integer> { @Override public byte[] encode(Integer data, int bitLength) throws BlockCodecException { try { Integer _data = data == null ? 0 : data; byte[] str = _data.toString().getBytes(); byte[] result = new byte[bitLength / 8]; int len = Math.min(str.length, result.length); Arrays.fill(result, (byte) '0'); System.arraycopy(str, 0, result, result.length - len, len); return result; } catch (Exception ex) { throw new BlockCodecException("integerString encode failure. " + ex.getMessage(), ex); } }  @Override Integer decode(byte[] data, int bitLength); @Override Integer zeroValue(); @Override byte[] encode(Integer data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testEncode() throws Exception { IntegerStringCodec codec = new IntegerStringCodec(); Assert.assertArrayEquals(new byte[] { 0x30, 0x30, 0x30 }, codec.encode(null, 24)); Assert.assertArrayEquals(new byte[] { 0x31, 0x32, 0x33 }, codec.encode(123, 24)); Assert.assertArrayEquals(new byte[] { 0x30, 0x32, 0x33 }, codec.encode(23, 24)); Assert.assertArrayEquals(new byte[] { 0x30, 0x30, 0x33 }, codec.encode(3, 24)); codec.reset(); }