- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageEncoder<CharSequence>
-
- io.netty5.handler.codec.string.StringEncoder
-
- All Implemented Interfaces:
ChannelHandler
public class StringEncoder extends MessageToMessageEncoder<CharSequence>
Encodes the requestedStringinto aBuffer. 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", newLineBasedFrameDecoder(80)); pipeline.addLast("stringDecoder", newStringDecoder(CharsetUtil.UTF_8)); // Encoder pipeline.addLast("stringEncoder", newStringEncoder(CharsetUtil.UTF_8));Stringinstead of aBufferas a message:void channelRead(
ChannelHandlerContextctx,Stringmsg) { ch.write("Did you say '" + msg + "'?\n"); }
-
-
Constructor Summary
Constructors Constructor Description StringEncoder()Creates a new instance with the current system character set.StringEncoder(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 voidencode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out)Encode from one message to another.booleanisSharable()Returnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline.-
Methods inherited from class io.netty5.handler.codec.MessageToMessageEncoder
acceptOutboundMessage, encodeAndClose, write
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, handlerRemoved, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
StringEncoder
public StringEncoder()
Creates a new instance with the current system character set.
-
StringEncoder
public StringEncoder(Charset charset)
Creates a new instance with the specified character set.
-
-
Method Detail
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandlerReturnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline. By default, this method returnsfalse. If this method returnsfalse, 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.
-
encode
protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception
Description copied from class:MessageToMessageEncoderEncode 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
MessageToMessageEncoder.encodeAndClose(ChannelHandlerContext, Object, List)method.- Overrides:
encodein classMessageToMessageEncoder<CharSequence>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageEncoderbelongs to.msg- the message to encode to another one.out- theListinto which produced output messages should be added.- Throws:
Exception- is thrown if an error occurs.
-
-