Home / Class/ ByteToMessageDecoder Class — netty Architecture

ByteToMessageDecoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55["ByteToMessageDecoder"]
  d1deb77a_f58f_195b_54ed_13600687ce6a["ByteToMessageDecoder.java"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|defined in| d1deb77a_f58f_195b_54ed_13600687ce6a
  4d865ca3_1ce0_b9c4_90c4_8872b20339bb["ByteToMessageDecoder()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 4d865ca3_1ce0_b9c4_90c4_8872b20339bb
  453476c8_4c60_4af0_cb4e_1c454a7842ca["setSingleDecode()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 453476c8_4c60_4af0_cb4e_1c454a7842ca
  29f844ed_a291_7f4c_f37d_0ff1c0a41a8a["isSingleDecode()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 29f844ed_a291_7f4c_f37d_0ff1c0a41a8a
  9d84d7c8_0a71_e12f_caa1_22127521cd73["setCumulator()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 9d84d7c8_0a71_e12f_caa1_22127521cd73
  1becfeea_c690_fb75_bfa8_f3db2a1be2ba["setDiscardAfterReads()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 1becfeea_c690_fb75_bfa8_f3db2a1be2ba
  0dd6e290_d467_d0a4_9e0b_e1a3e72a93b2["actualReadableBytes()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 0dd6e290_d467_d0a4_9e0b_e1a3e72a93b2
  08180243_3760_e575_25a4_2e09cd8d12b3["ByteBuf()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 08180243_3760_e575_25a4_2e09cd8d12b3
  df62d6fb_323f_5e89_3bf7_8847293a1412["handlerRemoved()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| df62d6fb_323f_5e89_3bf7_8847293a1412
  2fa71007_ea6a_c7bc_da66_51f4480aa368["handlerRemoved0()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 2fa71007_ea6a_c7bc_da66_51f4480aa368
  a22162a0_4ee0_09dc_f31c_e78971c0c57d["channelRead()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| a22162a0_4ee0_09dc_f31c_e78971c0c57d
  07d205b6_501b_721f_b872_e8e378f3c54d["fireChannelRead()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 07d205b6_501b_721f_b872_e8e378f3c54d
  6dc6c900_3a3e_0b9b_82b7_c315a803419b["channelReadComplete()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 6dc6c900_3a3e_0b9b_82b7_c315a803419b
  2aa120bb_dbc1_13a7_0bbd_6e8fb49ab322["discardSomeReadBytes()"]
  efe4959f_3296_dbb1_4a4b_76b9d7ccec55 -->|method| 2aa120bb_dbc1_13a7_0bbd_6e8fb49ab322

Relationship Graph

Source Code

codec-base/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java lines 78–604

public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {

    /**
     * Cumulate {@link ByteBuf}s by merge them into one {@link ByteBuf}'s, using memory copies.
     */
    public static final Cumulator MERGE_CUMULATOR = new Cumulator() {
        @Override
        public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
            if (cumulation == in) {
                // when the in buffer is the same as the cumulation it is doubly retained, release it once
                in.release();
                return cumulation;
            }
            if (!cumulation.isReadable() && in.isContiguous()) {
                // If cumulation is empty and input buffer is contiguous, use it directly
                cumulation.release();
                return in;
            }
            try {
                final int required = in.readableBytes();
                if (required > cumulation.maxWritableBytes() ||
                    required > cumulation.maxFastWritableBytes() && cumulation.refCnt() > 1 ||
                    cumulation.isReadOnly()) {
                    // Expand cumulation (by replacing it) under the following conditions:
                    // - cumulation cannot be resized to accommodate the additional data
                    // - cumulation can be expanded with a reallocation operation to accommodate but the buffer is
                    //   assumed to be shared (e.g. refCnt() > 1) and the reallocation may not be safe.
                    return expandCumulation(alloc, cumulation, in);
                }
                cumulation.writeBytes(in, in.readerIndex(), required);
                in.readerIndex(in.writerIndex());
                return cumulation;
            } finally {
                // We must release in all cases as otherwise it may produce a leak if writeBytes(...) throw
                // for whatever release (for example because of OutOfMemoryError)
                in.release();
            }
        }
    };

    /**
     * Cumulate {@link ByteBuf}s by add them to a {@link CompositeByteBuf} and so do no memory copy whenever possible.
     * Be aware that {@link CompositeByteBuf} use a more complex indexing implementation so depending on your use-case
     * and the decoder implementation this may be slower than just use the {@link #MERGE_CUMULATOR}.
     */
    public static final Cumulator COMPOSITE_CUMULATOR = new Cumulator() {
        @Override
        public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
            if (cumulation == in) {
                // when the in buffer is the same as the cumulation it is doubly retained, release it once
                in.release();
                return cumulation;
            }
            if (!cumulation.isReadable()) {
                cumulation.release();
                return in;
            }
            CompositeByteBuf composite = null;
            try {
                if (cumulation instanceof CompositeByteBuf && cumulation.refCnt() == 1) {
                    composite = (CompositeByteBuf) cumulation;
                    // Writer index must equal capacity if we are going to "write"
                    // new components to the end
                    if (composite.writerIndex() != composite.capacity()) {
                        composite.capacity(composite.writerIndex());
                    }
                } else {
                    composite = alloc.compositeBuffer(Integer.MAX_VALUE).addFlattenedComponents(true, cumulation);
                }
                composite.addFlattenedComponents(true, in);
                in = null;
                return composite;
            } finally {
                if (in != null) {
                    // We must release if the ownership was not transferred as otherwise it may produce a leak
                    in.release();
                    // Also release any new buffer allocated if we're not returning it
                    if (composite != null && composite != cumulation) {
                        composite.release();
                    }
                }

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free