BooleanCodec implements BlockCodec<Boolean> { @Override public Boolean decode(byte[] data, int bitLength) throws BlockCodecException { try { return ByteUtils.intValue(data, bitLength, true) == this.trueValue; } catch (Exception ex) { throw new BlockCodecException("boolean codec failure. " + ex.getMessage(), ex); } } BooleanCodec(); String getTrueValue(); void setTrueValue(String value); String getFalseValue(); void setFalseValue(String value); @Override Boolean zeroValue(); @Override Boolean decode(byte[] data, int bitLength); @Override byte[] encode(Boolean data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testDecode() throws BlockCodecException { BooleanCodec codec = new BooleanCodec(); codec.setTrueValue("0"); Assert.assertEquals("0", codec.getTrueValue()); codec.setFalseValue("1"); Assert.assertEquals("1", codec.getFalseValue()); Assert.assertEquals(false, codec.decode(new byte[] { (byte) 0xbf }, 1)); Assert.assertEquals(false, codec.decode(new byte[] { (byte) 0x3f }, 3)); Assert.assertEquals(false, codec.decode(new byte[] { (byte) 0x02 }, 7)); Assert.assertEquals(true, codec.decode(new byte[] { (byte) 0x01 }, 7)); codec.reset(); codec.setTrueValue("1"); Assert.assertEquals("1", codec.getTrueValue()); codec.setFalseValue("0"); Assert.assertEquals("0", codec.getFalseValue()); Assert.assertEquals(true, codec.decode(new byte[] { (byte) 0xbf }, 1)); Assert.assertEquals(true, codec.decode(new byte[] { (byte) 0x3f }, 3)); Assert.assertEquals(true, codec.decode(new byte[] { (byte) 0x02 }, 7)); Assert.assertEquals(false, codec.decode(new byte[] { (byte) 0x01 }, 7)); }