Top Description Fields Constructors Methods
io.netty.handler.codec

public abstract Class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>

extends ChannelDuplexHandler
Class Inheritance
Imports
io.netty.channel.ChannelDuplexHandler, .ChannelHandlerContext, .ChannelPromise, io.netty.util.ReferenceCounted, io.netty.util.internal.TypeParameterMatcher, java.util.List

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 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.

Field Summary

Modifier and TypeField and Description
private final MessageToMessageDecoder<Object>
private final MessageToMessageEncoder<Object>
private final TypeParameterMatcher
private final TypeParameterMatcher

Constructor Summary

AccessConstructor 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>
The type of messages to decode
inboundMessageType
,
Class<? extends OUTBOUND_IN>
The type of messages to encode
outboundMessageType
)

Create a new instance.

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptInboundMessage(Object
the message
msg
)

Returns true if and only if the specified message can be decoded by this codec.

public boolean
acceptOutboundMessage(Object
the message
msg
)

Returns true if and only if the specified message can be encoded by this codec.

public void
protected abstract void
decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)

protected abstract void
encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)

public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

Field Detail

decoderback to summary
private final MessageToMessageDecoder<Object> decoder
encoderback to summary
private final MessageToMessageEncoder<Object> encoder
inboundMsgMatcherback to summary
private final TypeParameterMatcher inboundMsgMatcher
outboundMsgMatcherback to summary
private final TypeParameterMatcher outboundMsgMatcher

Constructor Detail

MessageToMessageCodecback to summary
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.

MessageToMessageCodecback to summary
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)

Create a new instance.

Parameters
inboundMessageType:Class<? extends INBOUND_IN>

The type of messages to decode

outboundMessageType:Class<? extends OUTBOUND_IN>

The type of messages to encode

Method Detail

acceptInboundMessageback to summary
public boolean acceptInboundMessage(Object msg) throws Exception

Returns true if and only if the specified message can be decoded by this codec.

Parameters
msg:Object

the message

acceptOutboundMessageback to summary
public boolean acceptOutboundMessage(Object msg) throws Exception

Returns true if and only if the specified message can be encoded by this codec.

Parameters
msg:Object

the message

channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
decodeback to summary
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) throws Exception
See Also
MessageToMessageDecoder#decode(ChannelHandlerContext, Object, List)
encodeback to summary
protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
See Also
MessageToMessageEncoder#encode(ChannelHandlerContext, Object, List)
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs