Home / Function/ indexOf() — netty Function Reference

indexOf() — netty Function Reference

Architecture documentation for the indexOf() function in ByteBufUtil.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  01cd6f08_cabc_df56_318f_9666e83f47c5["indexOf()"]
  920454f6_25f6_4a9b_3d32_9684c3e11f6c["ByteBufUtil"]
  01cd6f08_cabc_df56_318f_9666e83f47c5 -->|defined in| 920454f6_25f6_4a9b_3d32_9684c3e11f6c
  f163dca2_b01f_aece_57c8_a62f9d5a9b40["maxSuf()"]
  01cd6f08_cabc_df56_318f_9666e83f47c5 -->|calls| f163dca2_b01f_aece_57c8_a62f9d5a9b40
  c05c9b3e_b2a3_295c_5eb2_a427df9e1f52["equals()"]
  01cd6f08_cabc_df56_318f_9666e83f47c5 -->|calls| c05c9b3e_b2a3_295c_5eb2_a427df9e1f52
  style 01cd6f08_cabc_df56_318f_9666e83f47c5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/ByteBufUtil.java lines 242–325

    public static int indexOf(ByteBuf needle, ByteBuf haystack) {
        if (haystack == null || needle == null) {
            return -1;
        }

        if (needle.readableBytes() > haystack.readableBytes()) {
            return -1;
        }

        int n = haystack.readableBytes();
        int m = needle.readableBytes();
        if (m == 0) {
            return 0;
        }

        // When the needle has only one byte that can be read,
        // the ByteBuf.indexOf() can be used
        if (m == 1) {
            return haystack.indexOf(haystack.readerIndex(), haystack.writerIndex(),
                          needle.getByte(needle.readerIndex()));
        }

        int i;
        int j = 0;
        int aStartIndex = needle.readerIndex();
        int bStartIndex = haystack.readerIndex();
        long suffixes =  maxSuf(needle, m, aStartIndex, true);
        long prefixes = maxSuf(needle, m, aStartIndex, false);
        int ell = Math.max((int) (suffixes >> 32), (int) (prefixes >> 32));
        int per = Math.max((int) suffixes, (int) prefixes);
        int memory;
        int length = Math.min(m - per, ell + 1);

        if (equals(needle, aStartIndex, needle, aStartIndex + per,  length)) {
            memory = -1;
            while (j <= n - m) {
                i = Math.max(ell, memory) + 1;
                while (i < m && needle.getByte(i + aStartIndex) == haystack.getByte(i + j + bStartIndex)) {
                    ++i;
                }
                if (i > n) {
                    return -1;
                }
                if (i >= m) {
                    i = ell;
                    while (i > memory && needle.getByte(i + aStartIndex) == haystack.getByte(i + j + bStartIndex)) {
                        --i;
                    }
                    if (i <= memory) {
                        return j + bStartIndex;
                    }
                    j += per;
                    memory = m - per - 1;
                } else {
                    j += i - ell;
                    memory = -1;
                }
            }
        } else {
            per = Math.max(ell + 1, m - ell - 1) + 1;
            while (j <= n - m) {
                i = ell + 1;
                while (i < m && needle.getByte(i + aStartIndex) == haystack.getByte(i + j + bStartIndex)) {
                    ++i;
                }
                if (i > n) {
                    return -1;
                }
                if (i >= m) {
                    i = ell;
                    while (i >= 0 && needle.getByte(i + aStartIndex) == haystack.getByte(i + j + bStartIndex)) {
                        --i;
                    }
                    if (i < 0) {
                        return j + bStartIndex;
                    }
                    j += per;
                } else {
                    j += i - ell;
                }
            }

Domain

Subdomains

Frequently Asked Questions

What does indexOf() do?
indexOf() is a function in the netty codebase, defined in buffer/src/main/java/io/netty/buffer/ByteBufUtil.java.
Where is indexOf() defined?
indexOf() is defined in buffer/src/main/java/io/netty/buffer/ByteBufUtil.java at line 242.
What does indexOf() call?
indexOf() calls 2 function(s): equals, maxSuf.

Analyze Your Own Codebase

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

Try Supermodel Free