XorEncoder extends MessageToByteEncoder<ByteBuf> { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { if (key == 0) { out.writeBytes(msg); return; } while (msg.isReadable()) { out.writeByte(msg.readByte() ^ key); } }  byte getKey(); void setKey(byte key);  }
@Test public void testEncode() throws Exception { ByteBuf buf = Unpooled.buffer(1); buf.markWriterIndex(); buf.writeByte(0xff); XorEncoder encoder = new XorEncoder(); encoder.setKey((byte) 0x1); ByteBuf out = Unpooled.buffer(1); encoder.encode(null, buf, out); byte encoded = out.readByte(); Assert.assertEquals((Byte) (byte) 0xfe, (Byte) encoded); }