Home / Class/ MessageToMessageDecoder Class — netty Architecture

MessageToMessageDecoder Class — netty Architecture

Architecture documentation for the MessageToMessageDecoder class in MessageToMessageDecoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9["MessageToMessageDecoder"]
  ea4b4073_8f02_433e_4a2f_5c558f087281["MessageToMessageDecoder.java"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|defined in| ea4b4073_8f02_433e_4a2f_5c558f087281
  ca11369a_e514_0fa8_b533_6061b3586cd9["MessageToMessageDecoder()"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|method| ca11369a_e514_0fa8_b533_6061b3586cd9
  095ef3ed_2575_aae4_4a81_0edeb0c150e4["acceptInboundMessage()"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|method| 095ef3ed_2575_aae4_4a81_0edeb0c150e4
  7ed6d4ea_1d23_84d2_c2a8_695d44953576["channelRead()"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|method| 7ed6d4ea_1d23_84d2_c2a8_695d44953576
  62a688e3_0cfd_dc4b_1c4b_8f3b3ef70b76["channelReadComplete()"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|method| 62a688e3_0cfd_dc4b_1c4b_8f3b3ef70b76
  fff8cb3d_c690_9cfb_7f27_6efff322946f["decode()"]
  bd1eaa52_3883_2c3e_1720_8a64708c4ba9 -->|method| fff8cb3d_c690_9cfb_7f27_6efff322946f

Relationship Graph

Source Code

codec-base/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java lines 52–138

public abstract class MessageToMessageDecoder<I> extends ChannelInboundHandlerAdapter {

    private final TypeParameterMatcher matcher;
    private boolean decodeCalled;
    private boolean messageProduced;

    /**
     * Create a new instance which will try to detect the types to match out of the type parameter of the class.
     */
    protected MessageToMessageDecoder() {
        matcher = TypeParameterMatcher.find(this, MessageToMessageDecoder.class, "I");
    }

    /**
     * Create a new instance
     *
     * @param inboundMessageType    The type of messages to match and so decode
     */
    protected MessageToMessageDecoder(Class<? extends I> inboundMessageType) {
        matcher = TypeParameterMatcher.get(inboundMessageType);
    }

    /**
     * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next
     * {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
     */
    public boolean acceptInboundMessage(Object msg) throws Exception {
        return matcher.match(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        decodeCalled = true;
        CodecOutputList out = CodecOutputList.newInstance();
        try {
            if (acceptInboundMessage(msg)) {
                @SuppressWarnings("unchecked")
                I cast = (I) msg;
                try {
                    decode(ctx, cast, out);
                } finally {
                    ReferenceCountUtil.release(cast);
                }
            } else {
                out.add(msg);
            }
        } catch (DecoderException e) {
            throw e;
        } catch (Exception e) {
            throw new DecoderException(e);
        } finally {
            try {
                int size = out.size();
                messageProduced |= size > 0;
                for (int i = 0; i < size; i++) {
                    ctx.fireChannelRead(out.getUnsafe(i));
                }
            } finally {
                out.recycle();
            }
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        if (!isSharable()) {
            // Only use local vars if this decoder is not sharable as otherwise this is not safe to do.
            if (decodeCalled && !messageProduced && !ctx.channel().config().isAutoRead()) {
                ctx.read();
            }
            decodeCalled = false;
            messageProduced = false;
        }
        ctx.fireChannelReadComplete();
    }

    /**
     * Decode from one message to an other. This method will be called for each written message that can be handled
     * by this decoder.
     *
     * @param ctx           the {@link ChannelHandlerContext} which this {@link MessageToMessageDecoder} belongs to

Frequently Asked Questions

What is the MessageToMessageDecoder class?
MessageToMessageDecoder is a class in the netty codebase, defined in codec-base/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java.
Where is MessageToMessageDecoder defined?
MessageToMessageDecoder is defined in codec-base/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java at line 52.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free