Home / Class/ SnappyFrameEncoder Class — netty Architecture

SnappyFrameEncoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  30e5e893_9366_6707_dcb5_d8aa1087057f["SnappyFrameEncoder"]
  5f41273c_974d_fad4_f495_7a891a1218db["SnappyFrameEncoder.java"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|defined in| 5f41273c_974d_fad4_f495_7a891a1218db
  591bb566_9e91_94e4_79b2_432957c185b3["SnappyFrameEncoder()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| 591bb566_9e91_94e4_79b2_432957c185b3
  c98be7cc_8336_cbe5_75fa_58753a9b740c["encode()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| c98be7cc_8336_cbe5_75fa_58753a9b740c
  c9a18e44_b89b_a0f7_ea69_f3aa8d296fef["writeUnencodedChunk()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| c9a18e44_b89b_a0f7_ea69_f3aa8d296fef
  6a3a9fdb_f6b2_f3cc_3d7b_2162896b33dc["setChunkLength()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| 6a3a9fdb_f6b2_f3cc_3d7b_2162896b33dc
  cede5bc0_22e0_7a27_de7c_15c39e399f75["writeChunkLength()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| cede5bc0_22e0_7a27_de7c_15c39e399f75
  6b5c85aa_d986_c90b_1b80_90a76af27668["calculateAndWriteChecksum()"]
  30e5e893_9366_6707_dcb5_d8aa1087057f -->|method| 6b5c85aa_d986_c90b_1b80_90a76af27668

Relationship Graph

Source Code

codec-compression/src/main/java/io/netty/handler/codec/compression/SnappyFrameEncoder.java lines 29–153

public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {

    private static final short SNAPPY_SLICE_SIZE = Short.MAX_VALUE;

    /**
     * Both
     * {@value io.netty.handler.codec.compression.SnappyFrameEncoder#SNAPPY_SLICE_SIZE}
     * and {@value io.netty.handler.codec.compression.SnappyFrameEncoder#SNAPPY_SLICE_JUMBO_SIZE}
     * are valid lengths for the Snappy framing format
     */
    private static final int SNAPPY_SLICE_JUMBO_SIZE = 65535;

    /**
     * The minimum amount that we'll consider actually attempting to compress.
     * This value is preamble + the minimum length our Snappy service will
     * compress (instead of just emitting a literal).
     */
    private static final int MIN_COMPRESSIBLE_LENGTH = 18;

    /**
     * All streams should start with the "Stream identifier", containing chunk
     * type 0xff, a length field of 0x6, and 'sNaPpY' in ASCII.
     */
    private static final byte[] STREAM_START = {
        (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59
    };

    public SnappyFrameEncoder() {
        this(SNAPPY_SLICE_SIZE);
    }

    /**
     * Create a new instance with a
     * {@value io.netty.handler.codec.compression.SnappyFrameEncoder#SNAPPY_SLICE_JUMBO_SIZE}
     * chunk size.
     */
    public static SnappyFrameEncoder snappyEncoderWithJumboFrames() {
        return new SnappyFrameEncoder(SNAPPY_SLICE_JUMBO_SIZE);
    }

    private SnappyFrameEncoder(int sliceSize) {
        super(ByteBuf.class);
        this.sliceSize = sliceSize;
    }

    private final Snappy snappy = new Snappy();
    private boolean started;
    private final int sliceSize;

    @Override
    protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
        if (!in.isReadable()) {
            return;
        }

        if (!started) {
            started = true;
            out.writeBytes(STREAM_START);
        }

        int dataLength = in.readableBytes();
        if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
            for (;;) {
                final int lengthIdx = out.writerIndex() + 1;
                if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
                    ByteBuf slice = in.readSlice(dataLength);
                    writeUnencodedChunk(slice, out, dataLength);
                    break;
                }

                out.writeInt(0);
                if (dataLength > sliceSize) {
                    ByteBuf slice = in.readSlice(sliceSize);
                    calculateAndWriteChecksum(slice, out);
                    snappy.encode(slice, out, sliceSize);
                    setChunkLength(out, lengthIdx);
                    dataLength -= sliceSize;
                } else {
                    ByteBuf slice = in.readSlice(dataLength);
                    calculateAndWriteChecksum(slice, out);
                    snappy.encode(slice, out, dataLength);

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free