public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends 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.ChannelHandler.Sharable| 限定符 | 构造器和说明 |
|---|---|
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(java.lang.Class<? extends INBOUND_IN> inboundMessageType,
java.lang.Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
acceptInboundMessage(java.lang.Object msg)
Returns
true if and only if the specified message can be decoded by this codec. |
boolean |
acceptOutboundMessage(java.lang.Object msg)
Returns
true if and only if the specified message can be encoded by this codec. |
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
protected abstract void |
decode(ChannelHandlerContext ctx,
INBOUND_IN msg,
java.util.List<java.lang.Object> out) |
protected abstract void |
encode(ChannelHandlerContext ctx,
OUTBOUND_IN msg,
java.util.List<java.lang.Object> out) |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
bind, close, connect, deregister, disconnect, flush, readchannelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAdded, handlerRemovedprotected MessageToMessageCodec()
protected MessageToMessageCodec(java.lang.Class<? extends INBOUND_IN> inboundMessageType, java.lang.Class<? extends OUTBOUND_IN> outboundMessageType)
inboundMessageType - The type of messages to decodeoutboundMessageType - The type of messages to encodepublic void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelRead 在接口中 ChannelInboundHandlerchannelRead 在类中 ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandlerChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.write 在接口中 ChannelOutboundHandlerwrite 在类中 ChannelDuplexHandlerctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic boolean acceptInboundMessage(java.lang.Object msg)
throws java.lang.Exception
true if and only if the specified message can be decoded by this codec.msg - the messagejava.lang.Exceptionpublic boolean acceptOutboundMessage(java.lang.Object msg)
throws java.lang.Exception
true if and only if the specified message can be encoded by this codec.msg - the messagejava.lang.Exceptionprotected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
java.lang.ExceptionMessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
java.lang.ExceptionMessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)