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

public abstract Class MessageToMessageEncoder<I>

extends ChannelOutboundHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty.handler.codec.base64.Base64Encoder, io.netty.handler.codec.bytes.ByteArrayEncoder, io.netty.handler.codec.protobuf.ProtobufEncoder, io.netty.handler.codec.protobuf.ProtobufEncoderNano, io.netty.handler.codec.string.LineEncoder, io.netty.handler.codec.string.StringEncoder, io.netty.handler.codec.DatagramPacketEncoder, io.netty.handler.codec.LengthFieldPrepender
Imports
io.netty.channel.ChannelHandlerContext, .ChannelOutboundHandler, .ChannelOutboundHandlerAdapter, .ChannelPipeline, .ChannelPromise, io.netty.util.ReferenceCountUtil, .ReferenceCounted, io.netty.util.concurrent.PromiseCombiner, io.netty.util.internal.PlatformDependent, .StringUtil, .TypeParameterMatcher, java.util.List

ChannelOutboundHandlerAdapter which encodes from one message to an other message For example here is an implementation which decodes an Integer to an String.
    public class IntegerToStringEncoder extends
            MessageToMessageEncoder<Integer> {

        @Override
        public void encode(ChannelHandlerContext ctx, Integer message, List<Object> out)
                throws Exception {
            out.add(message.toString());
        }
    }
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 MessageToMessageEncoder will call ReferenceCounted#release() on encoded messages.

Field Summary

Modifier and TypeField and Description
private final TypeParameterMatcher

Constructor Summary

AccessConstructor and Description
protected
MessageToMessageEncoder()

Create a new instance which will try to detect the types to match out of the type parameter of the class.

protected
MessageToMessageEncoder(Class<? extends I>
The type of messages to match and so encode
outboundMessageType
)

Create a new instance

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptOutboundMessage(Object msg)

Returns true if the given message should be handled.

protected abstract void
encode(ChannelHandlerContext ctx, I
the message to encode to an other one
msg
,
List<Object>
the List into which the encoded msg should be added needs to do some kind of aggregation
out
)

Encode from one message to an other.

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

Implements io.netty.channel.ChannelOutboundHandler.write.

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

Field Detail

matcherback to summary
private final TypeParameterMatcher matcher

Constructor Detail

MessageToMessageEncoderback to summary
protected MessageToMessageEncoder()

Create a new instance which will try to detect the types to match out of the type parameter of the class.

MessageToMessageEncoderback to summary
protected MessageToMessageEncoder(Class<? extends I> outboundMessageType)

Create a new instance

Parameters
outboundMessageType:Class<? extends I>

The type of messages to match and so encode

Method Detail

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

Returns true if the given message should be handled. If false it will be passed to the next ChannelOutboundHandler in the ChannelPipeline.

encodeback to summary
protected abstract void encode(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception

Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to

msg:I

the message to encode to an other one

out:List<Object>

the List into which the encoded msg should be added needs to do some kind of aggregation

Exceptions
Exception:
is thrown if an error occurs
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.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

writePromiseCombinerback to summary
private static void writePromiseCombiner(ChannelHandlerContext ctx, CodecOutputList out, ChannelPromise promise)
writeVoidPromiseback to summary
private static void writeVoidPromise(ChannelHandlerContext ctx, CodecOutputList out)