Browse Source

Fix position bug in NettyDataBuffer::toByteBuffer

Closes gh-31605
6.0.x
Arjen Poutsma 1 year ago
parent
commit
8868fe2ea5
  1. 2
      spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java
  2. 15
      spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

2
spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

@ -313,7 +313,7 @@ public class NettyDataBuffer implements PooledDataBuffer { @@ -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

15
spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

@ -796,10 +796,6 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -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 { @@ -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");
}
}

Loading…
Cancel
Save