Home / Class/ ByteToMessageCodec Class — netty Architecture

ByteToMessageCodec Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1["ByteToMessageCodec"]
  cfcaab9c_8f2a_3eeb_c859_bcbdd35c7d2d["ByteToMessageCodec.java"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|defined in| cfcaab9c_8f2a_3eeb_c859_bcbdd35c7d2d
  387c4cbf_8136_4ece_4437_7d1c0727a9b8["ByteToMessageCodec()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 387c4cbf_8136_4ece_4437_7d1c0727a9b8
  b2957c40_e032_fd18_8055_0bef972ebef4["acceptOutboundMessage()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| b2957c40_e032_fd18_8055_0bef972ebef4
  aa9e09a6_683f_251f_591e_92f931adf828["channelRead()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| aa9e09a6_683f_251f_591e_92f931adf828
  e4dea2aa_693e_6cae_11d1_b1f38ef8ee49["write()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| e4dea2aa_693e_6cae_11d1_b1f38ef8ee49
  03769f06_811f_11b6_db6f_456c737714fe["channelReadComplete()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 03769f06_811f_11b6_db6f_456c737714fe
  6cb1b211_4b22_627c_60b3_c6748ebe25ea["channelInactive()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 6cb1b211_4b22_627c_60b3_c6748ebe25ea
  b04d6293_a310_e6dc_dff6_607fa10f81ca["handlerAdded()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| b04d6293_a310_e6dc_dff6_607fa10f81ca
  1fe1d017_9580_0cd5_ee7b_4880ff61cd82["handlerRemoved()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 1fe1d017_9580_0cd5_ee7b_4880ff61cd82
  077a18a8_8d75_2884_6b60_64038de916a0["encode()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 077a18a8_8d75_2884_6b60_64038de916a0
  9abbaaed_b362_2a0c_d15b_08650a6dc437["decode()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| 9abbaaed_b362_2a0c_d15b_08650a6dc437
  ccdea5f0_9975_a92f_ccb8_3db90935d5dc["decodeLast()"]
  9f07e8cd_a4a5_23ec_9f33_00770a2701b1 -->|method| ccdea5f0_9975_a92f_ccb8_3db90935d5dc

Relationship Graph

Source Code

codec-base/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java lines 34–179

public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {

    private final TypeParameterMatcher outboundMsgMatcher;
    private final MessageToByteEncoder<I> encoder;

    private final ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
        @Override
        public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            ByteToMessageCodec.this.decode(ctx, in, out);
        }

        @Override
        protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            ByteToMessageCodec.this.decodeLast(ctx, in, out);
        }
    };

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

    /**
     * see {@link #ByteToMessageCodec(Class, boolean)} with {@code true} as boolean value.
     */
    protected ByteToMessageCodec(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 ByteToMessageCodec(boolean preferDirect) {
        ensureNotSharable();
        outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
        encoder = new Encoder(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 ByteToMessageCodec(Class<? extends I> outboundMessageType, boolean preferDirect) {
        ensureNotSharable();
        outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
        encoder = new Encoder(preferDirect, outboundMessageType);
    }

    /**
     * Returns {@code true} if and only if the specified message can be encoded by this codec.
     *
     * @param msg the message
     */
    public boolean acceptOutboundMessage(Object msg) throws Exception {
        return outboundMsgMatcher.match(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        decoder.channelRead(ctx, msg);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        encoder.write(ctx, msg, promise);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        decoder.channelReadComplete(ctx);
    }

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free