@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 aChannelPipeline
pipeline = ...; // 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(ChannelHandlerContext
ctx,MessageEvent
e) { String msg = (String) e.getMessage(); ch.write("Did you say '" + msg + "'?\n"); }
ChannelHandler.Sharable
Constructor and Description |
---|
StringEncoder()
Creates a new instance with the current system character set.
|
StringEncoder(Charset charset)
Creates a new instance with the specified character set.
|
StringEncoder(String charsetName)
Deprecated.
Use
StringEncoder(Charset) instead. |
Modifier and Type | Method and Description |
---|---|
protected Object |
encode(ChannelHandlerContext ctx,
Channel channel,
Object msg)
Transforms the specified message into another message and return the
transformed message.
|
doEncode, handleDownstream
public StringEncoder()
public StringEncoder(Charset charset)
@Deprecated public StringEncoder(String charsetName)
StringEncoder(Charset)
instead.protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception
OneToOneEncoder
null
, unlike
you can in OneToOneDecoder.decode(ChannelHandlerContext, Channel, Object)
;
you must return something, at least ChannelBuffers.EMPTY_BUFFER
.encode
in class OneToOneEncoder
Exception
Copyright © 2008-2013 The Netty Project. All Rights Reserved.