public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends ChannelHandlerAdapter
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()); }@Overrride
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.ChannelHandler.Sharable, ChannelHandler.Skip
Modifier | Constructor and 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(Class<? extends INBOUND_IN> inboundMessageType,
Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
acceptInboundMessage(Object msg)
Returns
true if and only if the specified message can be decoded by this codec. |
boolean |
acceptOutboundMessage(Object msg)
Returns
true if and only if the specified message can be encoded by this codec. |
void |
channelRead(ChannelHandlerContext ctx,
Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
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)
Calls
ChannelHandlerContext.write(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
bind, channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, exceptionCaught, flush, handlerAdded, handlerRemoved, isSharable, read, userEventTriggered
protected MessageToMessageCodec()
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)
inboundMessageType
- The type of messages to decodeoutboundMessageType
- The type of messages to encodepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
in interface ChannelHandler
channelRead
in class ChannelHandlerAdapter
Exception
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.write(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelHandler
write
in class ChannelHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error accourpublic boolean acceptInboundMessage(Object msg) throws Exception
true
if and only if the specified message can be decoded by this codec.msg
- the messageException
public boolean acceptOutboundMessage(Object msg) throws Exception
true
if and only if the specified message can be encoded by this codec.msg
- the messageException
protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) throws Exception
Copyright © 2008–2015 The Netty Project. All rights reserved.