Package io.netty.handler.codec
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
public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends ChannelDuplexHandler
A Codec for on-the-fly encoding/decoding of message. This can be thought of as a combination ofMessageToMessageDecoder
andMessageToMessageEncoder
. Here is an example of aMessageToMessageCodec
which just decode fromInteger
toLong
and encode fromLong
toInteger
.public class NumberCodec extends
Be aware that you need to callMessageToMessageCodec
<Integer
,Long
> {@Override
publicLong
decode(ChannelHandlerContext
ctx,Integer
msg, List<Object> out) throwsException
{ out.add(msg.longValue()); }@Override
publicInteger
encode(ChannelHandlerContext
ctx,Long
msg, List<Object> out) throwsException
{ out.add(msg.intValue()); } }ReferenceCounted.retain()
on messages that are just passed through if they are of typeReferenceCounted
. This is needed as theMessageToMessageCodec
will callReferenceCounted.release()
on encoded / decoded messages.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Constructor Summary
Constructors Modifier Constructor Description 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.protected
MessageToMessageCodec(java.lang.Class<? extends INBOUND_IN> inboundMessageType, java.lang.Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptInboundMessage(java.lang.Object msg)
Returnstrue
if and only if the specified message can be decoded by this codec.boolean
acceptOutboundMessage(java.lang.Object msg)
Returnstrue
if and only if the specified message can be encoded by this codec.void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelReadComplete(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected abstract void
decode(ChannelHandlerContext ctx, INBOUND_IN msg, java.util.List<java.lang.Object> out)
protected abstract void
encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, java.util.List<java.lang.Object> out)
void
write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, 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
handlerAdded, handlerRemoved
-
-
-
-
Constructor Detail
-
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(java.lang.Class<? extends INBOUND_IN> inboundMessageType, java.lang.Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.- Parameters:
inboundMessageType
- The type of messages to decodeoutboundMessageType
- The type of messages to encode
-
-
Method Detail
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
java.lang.Exception
- thrown if an error occurs
-
acceptInboundMessage
public boolean acceptInboundMessage(java.lang.Object msg) throws java.lang.Exception
Returnstrue
if and only if the specified message can be decoded by this codec.- Parameters:
msg
- the message- Throws:
java.lang.Exception
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.Exception
Returnstrue
if and only if the specified message can be encoded by this codec.- Parameters:
msg
- the message- Throws:
java.lang.Exception
-
encode
protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
decode
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
-
-