Home / Function/ encode() — netty Function Reference

encode() — netty Function Reference

Architecture documentation for the encode() function in Snappy.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  2c81d28d_4011_0232_9bac_669b46d6eed3["encode()"]
  6a7984ff_ded6_5ba2_b4bb_f92d0d3986f8["Snappy"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|defined in| 6a7984ff_ded6_5ba2_b4bb_f92d0d3986f8
  bd5271ad_e0a7_6fc1_fedc_8d83fb7e74b5["getHashTable()"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|calls| bd5271ad_e0a7_6fc1_fedc_8d83fb7e74b5
  499de467_1181_5401_2d71_b697c79fbb1f["hash()"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|calls| 499de467_1181_5401_2d71_b697c79fbb1f
  a68ad8f3_8ce4_d2df_f94a_c25a5795c393["encodeLiteral()"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|calls| a68ad8f3_8ce4_d2df_f94a_c25a5795c393
  70d7f600_24fa_537e_e58f_7a425608478d["findMatchingLength()"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|calls| 70d7f600_24fa_537e_e58f_7a425608478d
  5719cd76_7f65_93ab_f33b_8f7696b21cd0["encodeCopy()"]
  2c81d28d_4011_0232_9bac_669b46d6eed3 -->|calls| 5719cd76_7f65_93ab_f33b_8f7696b21cd0
  style 2c81d28d_4011_0232_9bac_669b46d6eed3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

codec-compression/src/main/java/io/netty/handler/codec/compression/Snappy.java lines 82–165

    public void encode(final ByteBuf in, final ByteBuf out, final int length) {
        // Write the preamble length to the output buffer
        for (int i = 0;; i ++) {
            int b = length >>> i * 7;
            if ((b & 0xFFFFFF80) != 0) {
                out.writeByte(b & 0x7f | 0x80);
            } else {
                out.writeByte(b);
                break;
            }
        }

        int inIndex = in.readerIndex();
        final int baseIndex = inIndex;

        int hashTableSize = MathUtil.findNextPositivePowerOfTwo(length);
        hashTableSize = Math.min(hashTableSize, MAX_HT_SIZE);
        final short[] table = getHashTable(hashTableSize);
        final int shift = Integer.numberOfLeadingZeros(hashTableSize) + 1;

        int nextEmit = inIndex;

        if (length - inIndex >= MIN_COMPRESSIBLE_BYTES) {
            int nextHash = hash(in, ++inIndex, shift);
            outer: while (true) {
                int skip = 32;

                int candidate;
                int nextIndex = inIndex;
                do {
                    inIndex = nextIndex;
                    int hash = nextHash;
                    int bytesBetweenHashLookups = skip++ >> 5;
                    nextIndex = inIndex + bytesBetweenHashLookups;

                    // We need at least 4 remaining bytes to read the hash
                    if (nextIndex > length - 4) {
                        break outer;
                    }

                    nextHash = hash(in, nextIndex, shift);

                    // equivalent to Short.toUnsignedInt
                    // use unsigned short cast to avoid loss precision when 32767 <= length <= 65355
                    candidate = baseIndex + (table[hash] & 0xffff);

                    table[hash] = (short) (inIndex - baseIndex);
                }
                while (in.getInt(inIndex) != in.getInt(candidate));

                encodeLiteral(in, out, inIndex - nextEmit);

                int insertTail;
                do {
                    int base = inIndex;
                    int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, length);
                    inIndex += matched;
                    int offset = base - candidate;
                    encodeCopy(out, offset, matched);
                    in.readerIndex(in.readerIndex() + matched);
                    insertTail = inIndex - 1;
                    nextEmit = inIndex;
                    if (inIndex >= length - 4) {
                        break outer;
                    }

                    int prevHash = hash(in, insertTail, shift);
                    table[prevHash] = (short) (inIndex - baseIndex - 1);
                    int currentHash = hash(in, insertTail + 1, shift);
                    candidate = baseIndex + (table[currentHash] & 0xFFFF);
                    table[currentHash] = (short) (inIndex - baseIndex);
                }
                while (in.getInt(insertTail + 1) == in.getInt(candidate));

                nextHash = hash(in, insertTail + 2, shift);
                ++inIndex;
            }
        }

        // If there are any remaining characters, write them out as a literal
        if (nextEmit < length) {

Domain

Subdomains

Frequently Asked Questions

What does encode() do?
encode() is a function in the netty codebase, defined in codec-compression/src/main/java/io/netty/handler/codec/compression/Snappy.java.
Where is encode() defined?
encode() is defined in codec-compression/src/main/java/io/netty/handler/codec/compression/Snappy.java at line 82.
What does encode() call?
encode() calls 5 function(s): encodeCopy, encodeLiteral, findMatchingLength, getHashTable, hash.

Analyze Your Own Codebase

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

Try Supermodel Free