Package io.netty.handler.codec
Class MessageToMessageEncoder<I>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageEncoder<I>
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelOutboundHandler
- Direct Known Subclasses:
Base64Encoder,ByteArrayEncoder,DatagramPacketEncoder,LengthFieldPrepender,LineEncoder,StringEncoder
public abstract class MessageToMessageEncoder<I> extends io.netty.channel.ChannelOutboundHandlerAdapterChannelOutboundHandlerAdapterwhich encodes from one message to an other message For example here is an implementation which decodes anIntegerto anString.public class IntegerToStringEncoder extendsBe aware that you need to callMessageToMessageEncoder<Integer> {@Overridepublic void encode(ChannelHandlerContextctx,Integermessage, List<Object> out) throwsException{ out.add(message.toString()); } }ReferenceCounted.retain()on messages that are just passed through if they are of typeReferenceCounted. This is needed as theMessageToMessageEncoderwill callReferenceCounted.release()on encoded messages.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageToMessageEncoder()Create a new instance which will try to detect the types to match out of the type parameter of the class.protectedMessageToMessageEncoder(Class<? extends I> outboundMessageType)Create a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(Object msg)Returnstrueif the given message should be handled.protected abstract voidencode(io.netty.channel.ChannelHandlerContext ctx, I msg, List<Object> out)Encode from one message to an other.voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Throws:
Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Overrides:
writein classio.netty.channel.ChannelOutboundHandlerAdapter- Throws:
Exception
-
encode
protected abstract void encode(io.netty.channel.ChannelHandlerContext ctx, I msg, List<Object> out) throws ExceptionEncode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageEncoderbelongs tomsg- the message to encode to an other oneout- theListinto which the encoded msg should be added needs to do some kind of aggregation- Throws:
Exception- is thrown if an error occurs
-
-