HessianCodec implements Codec { @Override public Payload encode(Message message) { message.setHeader("Crypt", crypt.cryptSign()); ByteArrayOutputStream streamData = new ByteArrayOutputStream(); Hessian2StreamingOutput hessianData = new Hessian2StreamingOutput(streamData); ByteArrayOutputStream streamHeader = new ByteArrayOutputStream(); Hessian2StreamingOutput hessianHeader = new Hessian2StreamingOutput(streamHeader); try { hessianData.writeObject(message.getData()); hessianHeader.writeObject(message.getHeaders()); } catch (IOException e){ throw new RuntimeException(); } byte[] bytesData = crypt.encrypt(streamData.toByteArray()); byte[] bytesHeaders = crypt.encrypt(streamHeader.toByteArray()); return ByteBufPayload.create(bytesData, bytesHeaders); } HessianCodec(Crypt crypt); @Override Payload encode(Message message); @Override Message decode(Payload payload);  }
@Test void encode() { TestData data = new TestData("hello"); Map<String, String> header = new HashMap<>(); header.put("key1", "hi"); Message message = new Message(header, data); Crypt c = CryptFactory.createCrypt("DES", "1"); HessianCodec codec = new HessianCodec(c); Payload payload = codec.encode(message); Message messageNew = codec.decode(payload); assertEquals("hello", ((TestData)messageNew.getData()).getInner()); assertEquals("hi", messageNew.getHeader("key1")); }