Home / Class/ ProtobufDecoder Class — netty Architecture

ProtobufDecoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  ef4ddb0e_b68c_68c0_a9e8_0ceddc13f22d["ProtobufDecoder"]
  d59591c7_25fa_cdd1_6ab7_7212a57cc4ca["ProtobufDecoder.java"]
  ef4ddb0e_b68c_68c0_a9e8_0ceddc13f22d -->|defined in| d59591c7_25fa_cdd1_6ab7_7212a57cc4ca
  ef8d5769_ac00_203d_15a6_4872bbcecb1a["ProtobufDecoder()"]
  ef4ddb0e_b68c_68c0_a9e8_0ceddc13f22d -->|method| ef8d5769_ac00_203d_15a6_4872bbcecb1a
  fc801aa9_27fe_799c_cfe9_4b1340400b98["decode()"]
  ef4ddb0e_b68c_68c0_a9e8_0ceddc13f22d -->|method| fc801aa9_27fe_799c_cfe9_4b1340400b98

Relationship Graph

Source Code

codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufDecoder.java lines 66–134

@Sharable
public class ProtobufDecoder extends MessageToMessageDecoder<ByteBuf> {

    private static final boolean HAS_PARSER;

    static {
        boolean hasParser = false;
        try {
            // MessageLite.getParserForType() is not available until protobuf 2.5.0.
            MessageLite.class.getDeclaredMethod("getParserForType");
            hasParser = true;
        } catch (Throwable t) {
            // Ignore
        }

        HAS_PARSER = hasParser;
    }

    private final MessageLite prototype;
    private final ExtensionRegistryLite extensionRegistry;

    /**
     * Creates a new instance.
     */
    public ProtobufDecoder(MessageLite prototype) {
        this(prototype, null);
    }

    public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
        this(prototype, (ExtensionRegistryLite) extensionRegistry);
    }

    public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry) {
        super(ByteBuf.class);
        this.prototype = ObjectUtil.checkNotNull(prototype, "prototype").getDefaultInstanceForType();
        this.extensionRegistry = extensionRegistry;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
            throws Exception {
        final byte[] array;
        final int offset;
        final int length = msg.readableBytes();
        if (msg.hasArray()) {
            array = msg.array();
            offset = msg.arrayOffset() + msg.readerIndex();
        } else {
            array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false);
            offset = 0;
        }

        if (extensionRegistry == null) {
            if (HAS_PARSER) {
                out.add(prototype.getParserForType().parseFrom(array, offset, length));
            } else {
                out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
            }
        } else {
            if (HAS_PARSER) {
                out.add(prototype.getParserForType().parseFrom(
                        array, offset, length, extensionRegistry));
            } else {
                out.add(prototype.newBuilderForType().mergeFrom(
                        array, offset, length, extensionRegistry).build());
            }
        }
    }
}

Frequently Asked Questions

What is the ProtobufDecoder class?
ProtobufDecoder is a class in the netty codebase, defined in codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufDecoder.java.
Where is ProtobufDecoder defined?
ProtobufDecoder is defined in codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufDecoder.java at line 66.

Analyze Your Own Codebase

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

Try Supermodel Free