Class StringEncoder

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

    @Sharable
    public class StringEncoder
    extends MessageToMessageEncoder<CharSequence>
    Encodes the requested String into a ByteBuf. 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
      StringEncoder()
      Creates a new instance with the current system character set.
      StringEncoder​(Charset charset)
      Creates a new instance with the specified character set.
    • Constructor Detail

      • StringEncoder

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

        public StringEncoder​(Charset charset)
        Creates a new instance with the specified character set.
    • Method Detail

      • encode

        protected void encode​(io.netty.channel.ChannelHandlerContext ctx,
                              CharSequence msg,
                              List<Object> out)
                       throws Exception
        Description copied from class: MessageToMessageEncoder
        Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.
        Specified by:
        encode in class MessageToMessageEncoder<CharSequence>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageEncoder belongs to
        msg - the message to encode to an other one
        out - the List into which the encoded msg should be added needs to do some kind of aggregation
        Throws:
        Exception - is thrown if an error occurs