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

public Class StringEncoder

extends MessageToMessageEncoder<CharSequence>
Class Inheritance
Annotations
@Sharable
Imports
io.netty.buffer.ByteBuf, .ByteBufUtil, io.netty.channel.ChannelHandler.Sharable, .ChannelHandlerContext, .ChannelPipeline, io.netty.handler.codec.LineBasedFrameDecoder, .MessageToMessageEncoder, io.netty.util.internal.ObjectUtil, java.nio.CharBuffer, java.nio.charset.Charset, java.util.List

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");
}

Field Summary

Modifier and TypeField and Description
private final Charset

Constructor Summary

AccessConstructor and Description
public
StringEncoder()

Creates a new instance with the current system character set.

public
StringEncoder(Charset charset)

Creates a new instance with the specified character set.

Method Summary

Modifier and TypeMethod and Description
protected void
encode(ChannelHandlerContext ctx, CharSequence
the message to encode to an other one
msg
,
List<Object>
the List into which the encoded msg should be added needs to do some kind of aggregation
out
)

Implements abstract io.netty.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to an other.
Inherited from io.netty.handler.codec.MessageToMessageEncoder:
acceptOutboundMessagewrite

Field Detail

charsetback to summary
private final Charset charset

Constructor Detail

StringEncoderback to summary
public StringEncoder()

Creates a new instance with the current system character set.

StringEncoderback to summary
public StringEncoder(Charset charset)

Creates a new instance with the specified character set.

Method Detail

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

Implements abstract io.netty.handler.codec.MessageToMessageEncoder.encode.

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

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

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to

msg:CharSequence

the message to encode to an other one

out:List<Object>

the List into which the encoded msg should be added needs to do some kind of aggregation

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs