public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
extends io.netty.channel.ChannelDuplexHandler
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, Long, Integer> {
@Override
public Long decode(ChannelHandlerContext ctx, Integer msg)
throws Exception {
return msg.longValue();
}
@Overrride
public Integer encode(ChannelHandlerContext ctx, Long msg)
throws Exception {
return 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.| Modifier | Constructor and Description |
|---|---|
protected |
MessageToMessageCodec() |
protected |
MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType,
Class<? extends OUTBOUND_IN> outboundMessageType) |
| 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. |
protected abstract void |
decode(io.netty.channel.ChannelHandlerContext ctx,
INBOUND_IN msg,
io.netty.channel.MessageList<Object> out) |
protected abstract void |
encode(io.netty.channel.ChannelHandlerContext ctx,
OUTBOUND_IN msg,
io.netty.channel.MessageList<Object> out) |
void |
messageReceived(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs) |
void |
write(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs,
io.netty.channel.ChannelPromise promise) |
bind, close, connect, deregister, disconnect, readchannelActive, channelInactive, channelReadSuspended, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredexceptionCaught, handlerAdded, handlerRemovedprotected MessageToMessageCodec()
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)
public void messageReceived(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs)
throws Exception
messageReceived in interface io.netty.channel.ChannelInboundHandlermessageReceived in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void write(io.netty.channel.ChannelHandlerContext ctx,
io.netty.channel.MessageList<Object> msgs,
io.netty.channel.ChannelPromise promise)
throws Exception
write in interface io.netty.channel.ChannelOutboundHandlerwrite in class io.netty.channel.ChannelDuplexHandlerExceptionpublic boolean acceptInboundMessage(Object msg) throws Exception
true if and only if the specified message can be decoded by this codec.msg - the messageExceptionpublic boolean acceptOutboundMessage(Object msg) throws Exception
true if and only if the specified message can be encoded by this codec.msg - the messageExceptionprotected abstract void encode(io.netty.channel.ChannelHandlerContext ctx,
OUTBOUND_IN msg,
io.netty.channel.MessageList<Object> out)
throws Exception
Exceptionprotected abstract void decode(io.netty.channel.ChannelHandlerContext ctx,
INBOUND_IN msg,
io.netty.channel.MessageList<Object> out)
throws Exception
ExceptionCopyright © 2008-2013 The Netty Project. All Rights Reserved.