Repository: netty/netty
Issue: the result of `ByteBufUtil.indexOf(ByteBuf needle, ByteBuf haystack)`  wrong

### Expected behavior
```
        ByteBuf haystack = Unpooled.copiedBuffer("+PONG\r\n", CharsetUtil.UTF_8);
        ByteBuf needle = Unpooled.copiedBuffer("\r\n", CharsetUtil.UTF_8);
        haystack.readByte();

        int index = ByteBufUtil.indexOf(needle ,haystack);

        System.out.println(index);
```
expected result is 5.

### Actual behavior
actual result is 4.

### Steps to reproduce

### Minimal yet complete reproducer code (or URL to code)

### Netty version
4.1.73.Final-SNAPSHOT

### JVM version (e.g. `java -version`)
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.21.10)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.21.10, mixed mode, sharing)

### OS version (e.g. `uname -a`)
Linux XPS-13-9310 5.13.0-22-generic #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux



Issue comments:
Thanks for the report, preparing a fix.
good
this is `ByteBuf` feature,`readByte()` make readIndex + 1,so the first index is `P` rather than `+`.

Pull request review comments:

Patch that resolved the issue:
diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
index 68a0ab5aaa0..8636d539553 100644
--- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
+++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
@@ -279,7 +279,7 @@ public static int indexOf(ByteBuf needle, ByteBuf haystack) {
                         --i;
                     }
                     if (i <= memory) {
-                        return j;
+                        return j + bStartIndex;
                     }
                     j += per;
                     memory = m - per - 1;
@@ -304,7 +304,7 @@ public static int indexOf(ByteBuf needle, ByteBuf haystack) {
                         --i;
                     }
                     if (i < 0) {
-                        return j;
+                        return j + bStartIndex;
                     }
                     j += per;
                 } else {


Test patch:
diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
index c872b2c4e1d..fbf106c4e2b 100644
--- a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
+++ b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
@@ -132,13 +132,13 @@ public void testIndexOf() {
         final ByteBuf needle = Unpooled.copiedBuffer("abc12", CharsetUtil.UTF_8);
         haystack.readerIndex(1);
         needle.readerIndex(1);
-        assertEquals(0, ByteBufUtil.indexOf(needle, haystack));
+        assertEquals(1, ByteBufUtil.indexOf(needle, haystack));
         haystack.readerIndex(2);
         needle.readerIndex(3);
-        assertEquals(1, ByteBufUtil.indexOf(needle, haystack));
+        assertEquals(3, ByteBufUtil.indexOf(needle, haystack));
         haystack.readerIndex(1);
         needle.readerIndex(2);
-        assertEquals(1, ByteBufUtil.indexOf(needle, haystack));
+        assertEquals(2, ByteBufUtil.indexOf(needle, haystack));
         haystack.release();
 
         haystack = new WrappedByteBuf(Unpooled.copiedBuffer("abc123", CharsetUtil.UTF_8));