Home / Class/ MessageToByteEncoder Class — netty Architecture

MessageToByteEncoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  3f1af45c_e168_cac3_b1c5_69c45a971df1["MessageToByteEncoder"]
  f412743a_9930_9866_c63c_e021ccec8970["MessageToByteEncoder.java"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|defined in| f412743a_9930_9866_c63c_e021ccec8970
  62528148_55ca_0108_b38a_8540f6cf2b04["MessageToByteEncoder()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| 62528148_55ca_0108_b38a_8540f6cf2b04
  c7e5a693_467a_b60c_89dd_9d29c6288cb6["acceptOutboundMessage()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| c7e5a693_467a_b60c_89dd_9d29c6288cb6
  c5c4c26c_eabe_677a_0caa_4b1c0ac9335e["write()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| c5c4c26c_eabe_677a_0caa_4b1c0ac9335e
  6ad42fc0_00a3_8197_5a78_7612fc3a4a60["ByteBuf()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| 6ad42fc0_00a3_8197_5a78_7612fc3a4a60
  c94dd07c_bb02_cb43_1273_5ad80dd8787c["encode()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| c94dd07c_bb02_cb43_1273_5ad80dd8787c
  91ee38bf_54bc_b8dc_1ca4_9f37d167d5c1["isPreferDirect()"]
  3f1af45c_e168_cac3_b1c5_69c45a971df1 -->|method| 91ee38bf_54bc_b8dc_1ca4_9f37d167d5c1

Relationship Graph

Source Code

codec-base/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java lines 46–160

public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter {

    private final TypeParameterMatcher matcher;
    private final boolean preferDirect;

    /**
     * see {@link #MessageToByteEncoder(boolean)} with {@code true} as boolean parameter.
     */
    protected MessageToByteEncoder() {
        this(true);
    }

    /**
     * see {@link #MessageToByteEncoder(Class, boolean)} with {@code true} as boolean value.
     */
    protected MessageToByteEncoder(Class<? extends I> outboundMessageType) {
        this(outboundMessageType, true);
    }

    /**
     * Create a new instance which will try to detect the types to match out of the type parameter of the class.
     *
     * @param preferDirect          {@code true} if a direct {@link ByteBuf} should be tried to be used as target for
     *                              the encoded messages. If {@code false} is used it will allocate a heap
     *                              {@link ByteBuf}, which is backed by an byte array.
     */
    protected MessageToByteEncoder(boolean preferDirect) {
        matcher = TypeParameterMatcher.find(this, MessageToByteEncoder.class, "I");
        this.preferDirect = preferDirect;
    }

    /**
     * Create a new instance
     *
     * @param outboundMessageType   The type of messages to match
     * @param preferDirect          {@code true} if a direct {@link ByteBuf} should be tried to be used as target for
     *                              the encoded messages. If {@code false} is used it will allocate a heap
     *                              {@link ByteBuf}, which is backed by an byte array.
     */
    protected MessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect) {
        matcher = TypeParameterMatcher.get(outboundMessageType);
        this.preferDirect = preferDirect;
    }

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

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        ByteBuf buf = null;
        try {
            if (acceptOutboundMessage(msg)) {
                @SuppressWarnings("unchecked")
                I cast = (I) msg;
                buf = allocateBuffer(ctx, cast, preferDirect);
                try {
                    encode(ctx, cast, buf);
                } finally {
                    ReferenceCountUtil.release(cast);
                }

                if (buf.isReadable()) {
                    ctx.write(buf, promise);
                } else {
                    buf.release();
                    ctx.write(Unpooled.EMPTY_BUFFER, promise);
                }
                buf = null;
            } else {
                ctx.write(msg, promise);
            }
        } catch (EncoderException e) {
            throw e;
        } catch (Throwable e) {
            throw new EncoderException(e);
        } finally {

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free