Class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.codec.MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
- All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler
- Direct Known Subclasses:
Http2StreamFrameToHttpObjectCodec, HttpContentEncoder, SpdyHttpResponseStreamIdHandler
A Codec for on-the-fly encoding/decoding of message.
This can be thought of as a combination of
MessageToMessageDecoder and MessageToMessageEncoder.
Here is an example of a MessageToMessageCodec which just decode from Integer to Long
and encode from Long to Integer.
public class NumberCodec extends
MessageToMessageCodec<Integer, Long> {
@Override
public Long decode(ChannelHandlerContext ctx, Integer msg, List<Object> out)
throws Exception {
out.add(msg.longValue());
}
@Override
public Integer encode(ChannelHandlerContext ctx, Long msg, List<Object> out)
throws Exception {
out.add(msg.intValue());
}
}
Be aware that you need to call ReferenceCounted.retain() on messages that are just passed through if they
are of type ReferenceCounted. This is needed as the MessageToMessageCodec will call
ReferenceCounted.release() on encoded / decoded messages.-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate a new instance which will try to detect the types to decode and encode out of the type parameter of the class.protectedMessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif and only if the specified message can be decoded by this codec.booleanReturnstrueif and only if the specified message can be encoded by this codec.voidchannelRead(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.protected abstract voiddecode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) protected abstract voidencode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) voidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, readMethods inherited from class ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ChannelHandler
handlerAdded, handlerRemoved
-
Constructor Details
-
MessageToMessageCodec
protected MessageToMessageCodec()Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class. -
MessageToMessageCodec
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) Create a new instance.- Parameters:
inboundMessageType- The type of messages to decodeoutboundMessageType- The type of messages to encode
-
-
Method Details
-
channelRead
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
Exception
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein classChannelInboundHandlerAdapter- Throws:
Exception
-
write
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
acceptInboundMessage
-
acceptOutboundMessage
-
encode
protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception - Throws:
Exception- See Also:
-
decode
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) throws Exception - Throws:
Exception- See Also:
-