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
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 extendsBe 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 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 io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final MessageToMessageDecoder
<Object> private final MessageToMessageEncoder
<Object> private final TypeParameterMatcher
private final TypeParameterMatcher
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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
(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if and only if the specified message can be decoded by this codec.boolean
Returnstrue
if and only if the specified message can be encoded by this codec.void
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected abstract void
decode
(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) protected abstract void
encode
(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) void
write
(ChannelHandlerContext ctx, 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, channelReadComplete, 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
-
Field Details
-
encoder
-
decoder
-
inboundMsgMatcher
-
outboundMsgMatcher
-
-
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: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:
Exception
-
write
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:
Exception
- thrown if an error occurs
-
acceptInboundMessage
Returnstrue
if and only if the specified message can be decoded by this codec.- Parameters:
msg
- the message- Throws:
Exception
-
acceptOutboundMessage
Returnstrue
if and only if the specified message can be encoded by this codec.- Parameters:
msg
- the message- Throws:
Exception
-
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:
-