Class StringDecoder

All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler

@Sharable public class StringDecoder extends MessageToMessageDecoder<ByteBuf>
Decodes a received ByteBuf into a String. Please note that this decoder must be used with a proper ByteToMessageDecoder such as DelimiterBasedFrameDecoder or LineBasedFrameDecoder if you are using a stream-based transport such as TCP/IP. A typical setup for a text-based line protocol in a TCP/IP socket would be:
 ChannelPipeline pipeline = ...;

 // Decoders
 pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(80));
 pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));

 // Encoder
 pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
 
and then you can use a String instead of a ByteBuf as a message:
 void channelRead(ChannelHandlerContext ctx, String msg) {
     ch.write("Did you say '" + msg + "'?\n");
 }
 
  • Field Details

    • charset

      private final Charset charset
  • Constructor Details

    • StringDecoder

      public StringDecoder()
      Creates a new instance with the current system character set.
    • StringDecoder

      public StringDecoder(Charset charset)
      Creates a new instance with the specified character set.
  • Method Details