Skip to content

Commit

Permalink
Fix position bug in NettyDataBuffer::toByteBuffer
Browse files Browse the repository at this point in the history
Closes gh-31605
  • Loading branch information
poutsma committed Nov 15, 2023
1 parent c373f49 commit 8868fe2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) {
Assert.notNull(dest, "Dest must not be null");

dest = dest.duplicate().clear();
dest.put(destPos, this.byteBuf.nioBuffer(), srcPos, length);
dest.put(destPos, this.byteBuf.nioBuffer(srcPos, length), 0, length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,6 @@ void slice(DataBufferFactory bufferFactory) {
if (!(bufferFactory instanceof Netty5DataBufferFactory)) {
assertThat(result).isEqualTo(new byte[]{'b', 'c'});
}
else {
assertThat(result).isEqualTo(new byte[]{'b', 0});
release(slice);
}
release(buffer);
}

Expand Down Expand Up @@ -938,4 +934,15 @@ void getByte(DataBufferFactory bufferFactory) {
release(buffer);
}

@ParameterizedDataBufferAllocatingTest // gh-31605
void shouldHonorSourceBuffersReadPosition(DataBufferFactory bufferFactory) {
DataBuffer dataBuffer = bufferFactory.wrap("ab".getBytes(StandardCharsets.UTF_8));
dataBuffer.readPosition(1);

ByteBuffer byteBuffer = ByteBuffer.allocate(dataBuffer.readableByteCount());
dataBuffer.toByteBuffer(byteBuffer);

assertThat(StandardCharsets.UTF_8.decode(byteBuffer).toString()).isEqualTo("b");
}

}

0 comments on commit 8868fe2

Please sign in to comment.