From b1e5db66a71497f6b3151a4eddced90a1dadae77 Mon Sep 17 00:00:00 2001 From: Stanislav Chernichkin Date: Fri, 25 Nov 2022 10:52:48 +0100 Subject: [PATCH] fix readFrom bytebuffer error (#72) --- .../src/main/java/xerial/larray/buffer/LBufferAPI.java | 2 +- .../src/test/scala/xerial/larray/buffer/LBufferTest.scala | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/larray-buffer/src/main/java/xerial/larray/buffer/LBufferAPI.java b/larray-buffer/src/main/java/xerial/larray/buffer/LBufferAPI.java index 2f17f42..20385ef 100644 --- a/larray-buffer/src/main/java/xerial/larray/buffer/LBufferAPI.java +++ b/larray-buffer/src/main/java/xerial/larray/buffer/LBufferAPI.java @@ -383,7 +383,7 @@ public int readFrom(byte[] src, int srcOffset, long destOffset, int length) { * @return the number of bytes read */ public int readFrom(ByteBuffer src, long destOffset) { - if (src.remaining() + destOffset >= size()) + if (src.remaining() + destOffset > size()) throw new BufferOverflowException(); int readLen = src.remaining(); ByteBuffer b = toDirectByteBuffer(destOffset, readLen); diff --git a/larray-buffer/src/test/scala/xerial/larray/buffer/LBufferTest.scala b/larray-buffer/src/test/scala/xerial/larray/buffer/LBufferTest.scala index 7f4b1ea..ac4c8fa 100644 --- a/larray-buffer/src/test/scala/xerial/larray/buffer/LBufferTest.scala +++ b/larray-buffer/src/test/scala/xerial/larray/buffer/LBufferTest.scala @@ -204,5 +204,12 @@ class LBufferTest extends LArraySpec { } + "read from ByteBuffer" in { + val bytes = Array[Byte](1, 2, 3) + val byteBuffer = ByteBuffer.wrap(bytes) + val lbuffer = new LBuffer(3) + lbuffer.readFrom(byteBuffer, 0) + byteBuffer.array() === lbuffer.toArray + } } }