From 0738af216e7b31926e05d3de64e14e5b7f0b214c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 4 Feb 2022 23:55:53 -0800 Subject: Fix buffer search --- src/main/java/org/traccar/helper/BufferUtil.java | 4 +-- .../java/org/traccar/helper/BufferUtilTest.java | 35 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/traccar/helper/BufferUtilTest.java (limited to 'src') diff --git a/src/main/java/org/traccar/helper/BufferUtil.java b/src/main/java/org/traccar/helper/BufferUtil.java index bbf12d738..9485c17c6 100644 --- a/src/main/java/org/traccar/helper/BufferUtil.java +++ b/src/main/java/org/traccar/helper/BufferUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 - 2021 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,7 +50,7 @@ public final class BufferUtil { wrappedHaystack.writerIndex(endIndex - haystack.readerIndex()); } int result = ByteBufUtil.indexOf(needle, wrappedHaystack); - return result < 0 ? result : haystack.readerIndex() + startIndex + result; + return result < 0 ? result : startIndex + result; } } diff --git a/src/test/java/org/traccar/helper/BufferUtilTest.java b/src/test/java/org/traccar/helper/BufferUtilTest.java new file mode 100644 index 000000000..b539b5b28 --- /dev/null +++ b/src/test/java/org/traccar/helper/BufferUtilTest.java @@ -0,0 +1,35 @@ +package org.traccar.helper; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; + +public class BufferUtilTest { + + @Test + public void test1() { + ByteBuf buf = Unpooled.copiedBuffer("abcdef", StandardCharsets.US_ASCII); + assertEquals(2, BufferUtil.indexOf("cd", buf)); + } + + @Test + public void test2() { + ByteBuf buf = Unpooled.copiedBuffer("abcdef", StandardCharsets.US_ASCII); + buf.readerIndex(1); + buf.writerIndex(5); + assertEquals(2, BufferUtil.indexOf("cd", buf)); + } + + @Test + public void test3() { + ByteBuf buf = Unpooled.copiedBuffer("abcdefgh", StandardCharsets.US_ASCII); + buf.readerIndex(1); + buf.writerIndex(7); + assertEquals(3, BufferUtil.indexOf("de", buf, 2, 6)); + } + +} -- cgit v1.2.3