Class StringDecoder

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler

    @Sharable
    public class StringDecoder
    extends MessageToMessageDecoder<io.netty.buffer.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");
     }
     
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Constructor Summary

      Constructors 
      Constructor Description
      StringDecoder()
      Creates a new instance with the current system character set.
      StringDecoder​(Charset charset)
      Creates a new instance with the specified character set.
    • Constructor Detail

      • 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 Detail

      • decode

        protected void decode​(io.netty.channel.ChannelHandlerContext ctx,
                              io.netty.buffer.ByteBuf msg,
                              List<Object> out)
                       throws Exception
        Description copied from class: MessageToMessageDecoder
        Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.
        Specified by:
        decode in class MessageToMessageDecoder<io.netty.buffer.ByteBuf>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageDecoder belongs to
        msg - the message to decode to an other one
        out - the List to which decoded messages should be added
        Throws:
        Exception - is thrown if an error occurs