Respect ByteBuffer position and limits in ByteUtils.getBytes(ByteBuffer).

We now properly extract the byte array from a ByteBuffer by copying its content respecting the read position and limits.

Closes #2204
Original Pull Request: #2213
This commit is contained in:
Mark Paluch
2021-12-14 11:27:12 +01:00
committed by Christoph Strobl
parent 9a56c38e2c
commit 3ca2191445
2 changed files with 57 additions and 5 deletions

View File

@@ -129,7 +129,8 @@ public final class ByteUtils {
}
/**
* Extract a byte array from {@link ByteBuffer} without consuming it.
* Extract a byte array from {@link ByteBuffer} without consuming it. The resulting {@code byte[]} is a copy of the
* buffer's contents and not updated upon changes within the buffer.
*
* @param byteBuffer must not be {@literal null}.
* @return
@@ -139,10 +140,6 @@ public final class ByteUtils {
Assert.notNull(byteBuffer, "ByteBuffer must not be null!");
if (byteBuffer.hasArray()) {
return byteBuffer.array();
}
ByteBuffer duplicate = byteBuffer.duplicate();
byte[] bytes = new byte[duplicate.remaining()];
duplicate.get(bytes);