Skip to content

Commit

Permalink
fix(core): force ByteBuffer position to limit when array has been read
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Aug 30, 2024
1 parent ab7784c commit b17edb6
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ fun ByteBuffer.startsWith(prefixes: List<ByteBuffer>): Pair<Boolean, Int> {
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)
Expand Down

0 comments on commit b17edb6

Please sign in to comment.