### Question:
StringEncoder extends MessageToMessageEncoder<CharSequence> { @Override protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception { if (msg.length() == 0) { return; } out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), charset)); } StringEncoder();  StringEncoder(Charset charset);   }### Answer:
@Test public void testEncode() { String msg = "Test"; EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder()); Assert.assertTrue(channel.writeOutbound(msg)); Assert.assertTrue(channel.finish()); ByteBuf buf = (ByteBuf) channel.readOutbound(); byte[] data = new byte[buf.readableBytes()]; buf.readBytes(data); Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); Assert.assertNull(channel.readOutbound()); buf.release(); }
@Test public void testEncode() { String msg = "Test"; EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder()); Assert.assertTrue(channel.writeOutbound(msg)); Assert.assertTrue(channel.finish()); ByteBuf buf = channel.readOutbound(); byte[] data = new byte[buf.readableBytes()]; buf.readBytes(data); Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); Assert.assertNull(channel.readOutbound()); buf.release(); }