Class LineEncoder

All Implemented Interfaces:
ChannelHandler, ChannelOutboundHandler

@Sharable public class LineEncoder extends MessageToMessageEncoder<CharSequence>
Apply a line separator to the requested String and encode it 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("lineEncoder", new LineEncoder(LineSeparator.UNIX, 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 + "'?");
 }
 
  • Field Details

    • charset

      private final Charset charset
    • lineSeparator

      private final byte[] lineSeparator
  • Constructor Details

    • LineEncoder

      public LineEncoder()
      Creates a new instance with the current system line separator and UTF-8 charset encoding.
    • LineEncoder

      public LineEncoder(LineSeparator lineSeparator)
      Creates a new instance with the specified line separator and UTF-8 charset encoding.
    • LineEncoder

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

      public LineEncoder(LineSeparator lineSeparator, Charset charset)
      Creates a new instance with the specified line separator and character set.
  • Method Details