CodecUtils { static Buf encode(SWContextCarrier swContextCarrier) { if (StringUtil.isNotEmpty(swContextCarrier.getOperationName()) && swContextCarrier.getCarrier() != null) { ByteArrayOutputStream bos = getBos(); try (DataOutputStream dos = new DataOutputStream(bos)) { putString(dos, swContextCarrier.getOperationName()); CarrierItem next = swContextCarrier.getCarrier().items(); while (next.hasNext()) { next = next.next(); if (next.getHeadKey() != null && next.getHeadValue() != null) { putString(dos, next.getHeadKey()); putString(dos, next.getHeadValue()); } } bos.flush(); return Bufs.ownedBuf(bos.toByteArray()); } catch (Exception e) { LOGGER.error("encode swContextCarrier exception.", e); } } return Bufs.EMPTY; }    }
@Test public void encode() { SWContextCarrier swContextCarrier = makeSWContextCarrier(); assertSwContextCarrier(swContextCarrier, CodecUtils.decode(CodecUtils.encode(swContextCarrier))); swContextCarrier = makeSWContextCarrier(); assertSwContextCarrier(swContextCarrier, CodecUtils.decode(CodecUtils.encode(swContextCarrier))); swContextCarrier = makeSWContextCarrier(); assertSwContextCarrier(swContextCarrier, CodecUtils.decode(CodecUtils.encode(swContextCarrier))); swContextCarrier = new SWContextCarrier(); assertSwContextCarrier(swContextCarrier, CodecUtils.decode(Bufs.EMPTY)); }