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:
ChannelHandler,ChannelOutboundHandler
- Direct Known Subclasses:
AbstractMemcacheObjectEncoder,Base64Encoder,ByteArrayEncoder,DatagramDnsQueryEncoder,DatagramDnsResponseEncoder,DatagramPacketEncoder,HttpObjectEncoder,LengthFieldPrepender,LineEncoder,MqttEncoder,ProtobufEncoder,ProtobufEncoderNano,RedisEncoder,SctpOutboundByteStreamHandler,SmtpRequestEncoder,SpdyHttpEncoder,StompSubframeEncoder,StringEncoder,TcpDnsResponseEncoder,WebSocket00FrameEncoder,WebSocket08FrameEncoder,WebSocketExtensionEncoder
public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapterwhich 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
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(java.lang.Class<? extends I> outboundMessageType)Create a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(java.lang.Object msg)Returnstrueif the given message should be handled.protected abstract voidencode(ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out)Encode from one message to an other.voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.-
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
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerAdded, handlerRemoved
-
-
-
-
Constructor Detail
-
MessageToMessageEncoder
protected MessageToMessageEncoder()
Create a new instance which will try to detect the types to match out of the type parameter of the class.
-
MessageToMessageEncoder
protected MessageToMessageEncoder(java.lang.Class<? extends I> outboundMessageType)
Create a new instance- Parameters:
outboundMessageType- The type of messages to match and so encode
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.ExceptionReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
encode
protected abstract void encode(ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
Encode 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:
java.lang.Exception- is thrown if an error occurs
-
-