diff options
Diffstat (limited to 'src/main/java/org/traccar/helper/BufferUtil.java')
-rw-r--r-- | src/main/java/org/traccar/helper/BufferUtil.java | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/org/traccar/helper/BufferUtil.java b/src/main/java/org/traccar/helper/BufferUtil.java index b086f0f9e..bbf12d738 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 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2021 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,19 +28,28 @@ public final class BufferUtil { } public static int indexOf(String needle, ByteBuf haystack) { - ByteBuf needleBuffer = Unpooled.wrappedBuffer(needle.getBytes(StandardCharsets.US_ASCII)); + return indexOf(needle, haystack, haystack.readerIndex(), haystack.writerIndex()); + } + + public static int indexOf(String needle, ByteBuf haystack, int startIndex, int endIndex) { + ByteBuf wrappedNeedle = Unpooled.wrappedBuffer(needle.getBytes(StandardCharsets.US_ASCII)); try { - return ByteBufUtil.indexOf(needleBuffer, haystack); + return indexOf(wrappedNeedle, haystack, startIndex, endIndex); } finally { - needleBuffer.release(); + wrappedNeedle.release(); } } - public static int indexOf(String needle, ByteBuf haystack, int startIndex, int endIndex) { - ByteBuf wrappedHaystack = Unpooled.wrappedBuffer(haystack); - wrappedHaystack.readerIndex(startIndex - haystack.readerIndex()); - wrappedHaystack.writerIndex(endIndex - haystack.readerIndex()); - int result = indexOf(needle, wrappedHaystack); + public static int indexOf(ByteBuf needle, ByteBuf haystack, int startIndex, int endIndex) { + ByteBuf wrappedHaystack; + if (startIndex == haystack.readerIndex() && endIndex == haystack.writerIndex()) { + wrappedHaystack = haystack; + } else { + wrappedHaystack = Unpooled.wrappedBuffer(haystack); + wrappedHaystack.readerIndex(startIndex - haystack.readerIndex()); + wrappedHaystack.writerIndex(endIndex - haystack.readerIndex()); + } + int result = ByteBufUtil.indexOf(needle, wrappedHaystack); return result < 0 ? result : haystack.readerIndex() + startIndex + result; } |