Top Description Fields Constructors Methods
io.netty5.handler.codec.string

public Class LineEncoder

extends MessageToMessageEncoder<CharSequence>
Class Inheritance
Imports
io.netty5.buffer.Buffer, io.netty5.channel.ChannelHandlerContext, .ChannelPipeline, io.netty5.handler.codec.LineBasedFrameDecoder, .MessageToMessageEncoder, io.netty5.util.CharsetUtil, io.netty5.util.internal.SilentDispose, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, .CharsetEncoder, .CoderResult, .StandardCharsets, java.util.List

Apply a line separator to the requested String and encode it into a Buffer. 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 Buffer as a message:
void channelRead(ChannelHandlerContext ctx, String msg) {
    ch.write("Did you say '" + msg + "'?");
}

Field Summary

Modifier and TypeField and Description
private final Charset
private final byte[]
private static final InternalLogger

Constructor Summary

AccessConstructor and Description
public
LineEncoder()

Creates a new instance with the current system line separator and UTF-8 charset encoding.

public
LineEncoder(LineSeparator lineSeparator)

Creates a new instance with the specified line separator and UTF-8 charset encoding.

public
LineEncoder(Charset charset)

Creates a new instance with the specified character set.

public
LineEncoder(LineSeparator lineSeparator, Charset charset)

Creates a new instance with the specified line separator and character set.

Method Summary

Modifier and TypeMethod and Description
protected void
encode(ChannelHandlerContext ctx, CharSequence
the message to encode to another one.
msg
,
List<Object>
the List into which produced output messages should be added.
out
)

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to another.
public boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
Inherited from io.netty5.handler.codec.MessageToMessageEncoder:
acceptOutboundMessageencodeAndClosewrite

Field Detail

charsetback to summary
private final Charset charset
lineSeparatorback to summary
private final byte[] lineSeparator
loggerback to summary
private static final InternalLogger logger

Constructor Detail

LineEncoderback to summary
public LineEncoder()

Creates a new instance with the current system line separator and UTF-8 charset encoding.

LineEncoderback to summary
public LineEncoder(LineSeparator lineSeparator)

Creates a new instance with the specified line separator and UTF-8 charset encoding.

LineEncoderback to summary
public LineEncoder(Charset charset)

Creates a new instance with the specified character set.

LineEncoderback to summary
public LineEncoder(LineSeparator lineSeparator, Charset charset)

Creates a new instance with the specified line separator and character set.

Method Detail

encodeback to summary
protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encode.

Doc from io.netty5.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to another. This method will be called for each written message that can be handled by this encoder.

The message will be disposed of after this call.

Subclasses that wish to sometimes pass messages through, should instead override the encodeAndClose(ChannelHandlerContext, Object, List) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to.

msg:CharSequence

the message to encode to another one.

out:List<Object>

the List into which produced output messages should be added.

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs.
isSharableback to summary
public boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override