Class StringEncoder
- java.lang.Object
-
- org.jboss.netty.handler.codec.oneone.OneToOneEncoder
-
- org.jboss.netty.handler.codec.string.StringEncoder
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
@Sharable public class StringEncoder extends OneToOneEncoder
Encodes the requestedString
into aChannelBuffer
. A typical setup for a text-based line protocol in a TCP/IP socket would be:ChannelPipeline
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 aChannelBuffer
as a message:void messageReceived(
ChannelHandlerContext
ctx,MessageEvent
e) { String msg = (String) e.getMessage(); ch.write("Did you say '" + msg + "'?\n"); }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.Charset
charset
-
Constructor Summary
Constructors Constructor Description 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.-
Methods inherited from class org.jboss.netty.handler.codec.oneone.OneToOneEncoder
doEncode, handleDownstream
-
-
-
-
Method Detail
-
encode
protected java.lang.Object encode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg) throws java.lang.Exception
Description copied from class:OneToOneEncoder
Transforms the specified message into another message and return the transformed message. Note that you can not returnnull
, unlike you can inOneToOneDecoder.decode(ChannelHandlerContext, Channel, Object)
; you must return something, at leastChannelBuffers.EMPTY_BUFFER
.- Specified by:
encode
in classOneToOneEncoder
- Throws:
java.lang.Exception
-
-