From b17edb64d130b5865b69e74880d90eb1ce52c0df Mon Sep 17 00:00:00 2001 From: ThibaultBee <37510686+ThibaultBee@users.noreply.github.com> Date: Fri, 30 Aug 2024 10:07:50 +0200 Subject: [PATCH] fix(core): force ByteBuffer position to limit when array has been read --- .../internal/utils/extensions/ByteBufferExtensions.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/github/thibaultbee/streampack/internal/utils/extensions/ByteBufferExtensions.kt b/core/src/main/java/io/github/thibaultbee/streampack/internal/utils/extensions/ByteBufferExtensions.kt index 1d3d467b2..abc596b50 100644 --- a/core/src/main/java/io/github/thibaultbee/streampack/internal/utils/extensions/ByteBufferExtensions.kt +++ b/core/src/main/java/io/github/thibaultbee/streampack/internal/utils/extensions/ByteBufferExtensions.kt @@ -204,12 +204,14 @@ fun ByteBuffer.startsWith(prefixes: List): Pair { fun ByteBuffer.toByteArray(): ByteArray { return if (this.hasArray() && !isDirect) { val offset = position() + arrayOffset() - val array = array() - if (offset == 0 && array.size == remaining()) { - array + val bufferArray = array() + val array = if (offset == 0 && bufferArray.size == remaining()) { + bufferArray } else { - array.copyOfRange(offset, offset + remaining()) + bufferArray.copyOfRange(offset, offset + remaining()) } + position(limit()) + return array } else { val byteArray = ByteArray(this.remaining()) get(byteArray)