Home / Class/ ZstdDecoder Class — netty Architecture

ZstdDecoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  ff420528_d442_7376_2907_d6bc00ce06b0["ZstdDecoder"]
  f2b12ecc_cbd9_db9e_f95e_e66779dd1aa4["ZstdDecoder.java"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|defined in| f2b12ecc_cbd9_db9e_f95e_e66779dd1aa4
  84acab65_8ca8_4622_79fb_362737b95203["ZstdDecoder()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| 84acab65_8ca8_4622_79fb_362737b95203
  f555e463_8e43_f330_5dfd_60bfd5ae66b3["decode()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| f555e463_8e43_f330_5dfd_60bfd5ae66b3
  e320d008_5a73_ed13_ceb0_361a0db5a673["channelReadComplete()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| e320d008_5a73_ed13_ceb0_361a0db5a673
  3fbd8e62_bada_e0dd_35cb_6b5136a74366["handlerAdded()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| 3fbd8e62_bada_e0dd_35cb_6b5136a74366
  a7b66673_65f4_d6b6_6b4a_eefa9bb47d5e["handlerRemoved0()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| a7b66673_65f4_d6b6_6b4a_eefa9bb47d5e
  83ff5da5_3165_b015_1be5_6741a7562cd1["closeSilently()"]
  ff420528_d442_7376_2907_d6bc00ce06b0 -->|method| 83ff5da5_3165_b015_1be5_6741a7562cd1

Relationship Graph

Source Code

codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java lines 34–189

public final class ZstdDecoder extends ByteToMessageDecoder {
    // Don't use static here as we want to still allow to load the classes.
    {
        try {
            Zstd.ensureAvailability();
        } catch (Throwable throwable) {
            throw new ExceptionInInitializerError(throwable);
        }
    }

    private final int maximumAllocationSize;
    private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream();
    private ZstdInputStreamNoFinalizer zstdIs;

    private boolean needsRead;
    private State currentState = State.DECOMPRESS_DATA;

    /**
     * Current state of stream.
     */
    private enum State {
        DECOMPRESS_DATA,
        CORRUPTED
    }

    public ZstdDecoder() {
        this(4 * 1024 * 1024);
    }

    public ZstdDecoder(int maximumAllocationSize) {
        this.maximumAllocationSize = ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize");
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        needsRead = true;
        try {
            if (currentState == State.CORRUPTED) {
                in.skipBytes(in.readableBytes());

                return;
            }
            inputStream.current = in;

            ByteBuf outBuffer = null;

            final int compressedLength = in.readableBytes();
            try {
                long uncompressedLength;
                if (in.isDirect()) {
                    uncompressedLength = com.github.luben.zstd.Zstd.getFrameContentSize(
                            CompressionUtil.safeNioBuffer(in, in.readerIndex(), in.readableBytes()));
                } else {
                    uncompressedLength = com.github.luben.zstd.Zstd.getFrameContentSize(
                            in.array(), in.readerIndex() + in.arrayOffset(), in.readableBytes());
                }
                if (uncompressedLength <= 0) {
                    // Let's start with the compressedLength * 2 as often we will not have everything
                    // we need in the in buffer and don't want to reserve too much memory.
                    uncompressedLength = compressedLength * 2L;
                }

                int w;
                do {
                    if (outBuffer == null) {
                        outBuffer = ctx.alloc().heapBuffer((int) (maximumAllocationSize == 0 ?
                                uncompressedLength : Math.min(maximumAllocationSize, uncompressedLength)));
                    }
                    do {
                        w = outBuffer.writeBytes(zstdIs, outBuffer.writableBytes());
                    } while (w != -1 && outBuffer.isWritable());
                    if (outBuffer.isReadable()) {
                        needsRead = false;
                        ctx.fireChannelRead(outBuffer);
                        outBuffer = null;
                    }
                } while (w != -1);
            } finally {
                if (outBuffer != null) {
                    outBuffer.release();
                }

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free