Home / Class/ SnappyFrameDecoder Class — netty Architecture

SnappyFrameDecoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0["SnappyFrameDecoder"]
  02940bc3_3f9f_d133_f9cc_2e813fdda124["SnappyFrameDecoder.java"]
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0 -->|defined in| 02940bc3_3f9f_d133_f9cc_2e813fdda124
  e4378966_0059_e79f_2d58_0503c856fcc0["SnappyFrameDecoder()"]
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0 -->|method| e4378966_0059_e79f_2d58_0503c856fcc0
  0bffb48f_aaf2_5805_e639_4882f253554e["decode()"]
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0 -->|method| 0bffb48f_aaf2_5805_e639_4882f253554e
  befef7cb_c88c_6738_dff4_0c81602c4e0f["checkByte()"]
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0 -->|method| befef7cb_c88c_6738_dff4_0c81602c4e0f
  69ebca01_32ef_3ec5_d3f8_40806cb5e9dc["ChunkType()"]
  5f033f0f_5da6_662f_fbd6_9f67efbcc2e0 -->|method| 69ebca01_32ef_3ec5_d3f8_40806cb5e9dc

Relationship Graph

Source Code

codec-compression/src/main/java/io/netty/handler/codec/compression/SnappyFrameDecoder.java lines 37–259

public class SnappyFrameDecoder extends ByteToMessageDecoder {

    private enum ChunkType {
        STREAM_IDENTIFIER,
        COMPRESSED_DATA,
        UNCOMPRESSED_DATA,
        RESERVED_UNSKIPPABLE,
        RESERVED_SKIPPABLE
    }

    private static final int SNAPPY_IDENTIFIER_LEN = 6;
    // See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L95
    private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65536 + 4;
    // See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
    private static final int MAX_DECOMPRESSED_DATA_SIZE = 65536;
    // See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
    private static final int MAX_COMPRESSED_CHUNK_SIZE = 16777216 - 1;

    private final Snappy snappy = new Snappy();
    private final boolean validateChecksums;

    private boolean started;
    private boolean corrupted;
    private int numBytesToSkip;

    /**
     * Creates a new snappy-framed decoder with validation of checksums
     * turned OFF. To turn checksum validation on, please use the alternate
     * {@link #SnappyFrameDecoder(boolean)} constructor.
     */
    public SnappyFrameDecoder() {
        this(false);
    }

    /**
     * Creates a new snappy-framed decoder with validation of checksums
     * as specified.
     *
     * @param validateChecksums
     *        If true, the checksum field will be validated against the actual
     *        uncompressed data, and if the checksums do not match, a suitable
     *        {@link DecompressionException} will be thrown
     */
    public SnappyFrameDecoder(boolean validateChecksums) {
        this.validateChecksums = validateChecksums;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (corrupted) {
            in.skipBytes(in.readableBytes());
            return;
        }

        if (numBytesToSkip != 0) {
            // The last chunkType we detected was RESERVED_SKIPPABLE and we still have some bytes to skip.
            int skipBytes = Math.min(numBytesToSkip, in.readableBytes());
            in.skipBytes(skipBytes);
            numBytesToSkip -= skipBytes;

            // Let's return and try again.
            return;
        }

        try {
            int idx = in.readerIndex();
            final int inSize = in.readableBytes();
            if (inSize < 4) {
                // We need to be at least able to read the chunk type identifier (one byte),
                // and the length of the chunk (3 bytes) in order to proceed
                return;
            }

            final int chunkTypeVal = in.getUnsignedByte(idx);
            final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
            final int chunkLength = in.getUnsignedMediumLE(idx + 1);

            switch (chunkType) {
                case STREAM_IDENTIFIER:
                    if (chunkLength != SNAPPY_IDENTIFIER_LEN) {
                        throw new DecompressionException("Unexpected length of stream identifier: " + chunkLength);

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free