OptionalParameter { public byte[] serialize() { byte[] value = serializeValue(); ByteBuffer buffer = ByteBuffer.allocate(value.length + 4); buffer.putShort(tag); buffer.putShort((short)value.length); buffer.put(value); return buffer.array(); } OptionalParameter(short tag); byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); final short tag; }
@Test(groups="checkintest") public void stringParameterSerialization() { OptionalParameter param = new OptionalParameter.OctetString(Tag.DEST_SUBADDRESS, "jeah"); byte[] serialised = param.serialize(); assertEquals(serialised.length, 2 + 2 + 4); assertEquals(OctetUtil.bytesToShort(serialised, 0), Tag.DEST_SUBADDRESS.code()); assertEquals(OctetUtil.bytesToShort(serialised, 2), "jeah".getBytes().length); assertEquals(new String(serialised, 4, serialised.length - 4), "jeah"); }