From 8868fe2ea5ff0254fa1598a510644908fd09af52 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 15 Nov 2023 12:34:32 +0100 Subject: [PATCH] Fix position bug in NettyDataBuffer::toByteBuffer Closes gh-31605 --- .../core/io/buffer/NettyDataBuffer.java | 2 +- .../core/io/buffer/DataBufferTests.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java index 42a73c3050..ddcc6c6f9b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java @@ -313,7 +313,7 @@ public class NettyDataBuffer implements PooledDataBuffer { 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 diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java index e83286584e..cd3dd3c579 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java @@ -796,10 +796,6 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { if (!(bufferFactory instanceof Netty5DataBufferFactory)) { assertThat(result).isEqualTo(new byte[]{'b', 'c'}); } - else { - assertThat(result).isEqualTo(new byte[]{'b', 0}); - release(slice); - } release(buffer); } @@ -938,4 +934,15 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { 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"); + } + }