NettyDataBuffer uses this.byteBuf.nioBuffer() internally which will allocates fewer bytes if readPosition is greater than 0. The following checks are then failing with an IndexOutOfBoundsException.

https://github.com/spring-projects/spring-framework/blob/2b7a9209b3e6b1213bff3e2b7c893ffc124deadc/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java#L316

Here is a simple repro case (which passes for DefaultDataBufferFactory):

@Test
void shouldHonorSourceBuffersReadPosition() {
  DataBufferFactory bufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);

  DataBuffer sourceBuffer = bufferFactory.wrap("Hello World".getBytes(UTF_8));

  sourceBuffer.readPosition("Hello ".getBytes(UTF_8).length);
  ByteBuffer targetBuffer = ByteBuffer.allocate(sourceBuffer.readableByteCount());

  sourceBuffer.toByteBuffer(targetBuffer);

  assertThat(new String(targetBuffer.array(), UTF_8)).isEqualTo("World");
}

Comment From: poutsma

Thanks for spotting this, fixed!