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> {
@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.| 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(io.netty.channel.ChannelHandlerContext ctx,
Object msg) |
protected abstract void |
decode(io.netty.channel.ChannelHandlerContext ctx,
INBOUND_IN msg,
List<Object> out) |
protected abstract void |
encode(io.netty.channel.ChannelHandlerContext ctx,
OUTBOUND_IN msg,
List<Object> out) |
void |
write(io.netty.channel.ChannelHandlerContext ctx,
Object msg,
io.netty.channel.ChannelPromise promise) |
bind, close, connect, deregister, disconnect, flush, readchannelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, handlerAdded, handlerRemoved, isSharableprotected 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(io.netty.channel.ChannelHandlerContext ctx,
Object msg)
throws Exception
channelRead in interface io.netty.channel.ChannelInboundHandlerchannelRead in class io.netty.channel.ChannelInboundHandlerAdapterExceptionpublic void write(io.netty.channel.ChannelHandlerContext ctx,
Object msg,
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,
List<Object> out)
throws Exception
ExceptionMessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)protected abstract void decode(io.netty.channel.ChannelHandlerContext ctx,
INBOUND_IN msg,
List<Object> out)
throws Exception
ExceptionMessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)Copyright © 2008–2019 The Netty Project. All rights reserved.