### Question:
RedisReplyEncoder extends MessageToByteEncoder<IRedisReply> { @Override protected void encode(ChannelHandlerContext ctx, IRedisReply msg, ByteBuf out) throws Exception { msg.encode(out); if(logger.isDebugEnabled()){ logger.debug("解码前协议文本内容:{}",out.copy().toString(Charset.forName("UTF-8")).replaceAll("\r\n","\\\\r\\\\n")); } }    }### Answer:
@Test public void encode() { IRedisReply redisReply = new BulkRedisReply(); ((BulkRedisReply) redisReply).setLength(6); ((BulkRedisReply) redisReply).setValue("foobar".getBytes()); EmbeddedChannel channel = new EmbeddedChannel( new RedisReplyEncoder()); assertTrue(channel.writeOutbound(redisReply)); assertTrue(channel.finish()); ByteBuf buf = channel.readOutbound(); assertNotNull(buf); assertEquals(Type.BULK,redisReply.getType()); assertTrue(buf.isReadable()); assertEquals('$',(char)buf.readByte()); assertEquals('6',(char)buf.readByte()); assertEquals(RedisConstants.CR_BYTE,buf.readByte()); assertEquals(RedisConstants.LF_BYTE,buf.readByte()); assertEquals("foobar",readToStr(buf,6)); assertEquals(RedisConstants.CR_BYTE,buf.readByte()); assertEquals(RedisConstants.LF_BYTE,buf.readByte()); assertFalse(buf.isReadable()); assertNull(channel.readInbound()); }