@ChannelHandler.Sharable public class StringEncoder extends OneToOneEncoder
String into a ChannelBuffer.
A typical setup for a text-based line protocol in a TCP/IP socket would be:
and then you can use aChannelPipelinepipeline = ...; // Decoders pipeline.addLast("frameDecoder", newDelimiterBasedFrameDecoder(Delimiters.lineDelimiter())); pipeline.addLast("stringDecoder", newStringDecoder(CharsetUtil.UTF_8)); // Encoder pipeline.addLast("stringEncoder", newStringEncoder(CharsetUtil.UTF_8));
String instead of a ChannelBuffer
as a message:
void messageReceived(ChannelHandlerContextctx,MessageEvente) { String msg = (String) e.getMessage(); ch.write("Did you say '" + msg + "'?\n"); }
ChannelHandler.Sharable| 构造器和说明 |
|---|
StringEncoder()
Creates a new instance with the current system character set.
|
StringEncoder(java.nio.charset.Charset charset)
Creates a new instance with the specified character set.
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected java.lang.Object |
encode(ChannelHandlerContext ctx,
Channel channel,
java.lang.Object msg)
Transforms the specified message into another message and return the
transformed message.
|
doEncode, handleDownstreampublic StringEncoder()
public StringEncoder(java.nio.charset.Charset charset)
protected java.lang.Object encode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg) throws java.lang.Exception
OneToOneEncodernull, unlike
you can in OneToOneDecoder.decode(ChannelHandlerContext, Channel, Object);
you must return something, at least ChannelBuffers.EMPTY_BUFFER.encode 在类中 OneToOneEncoderjava.lang.Exception