From 10a11e03fbb45506973c85ae49d0fd1443d0f9f1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 5 Nov 2021 16:48:27 -0700 Subject: Support new Xexun protocol (fix #4727) --- src/main/java/org/traccar/helper/BufferUtil.java | 27 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/traccar/helper') 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; } -- cgit v1.2.3