diff --git a/loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedByteChannel.java b/loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedByteChannel.java index 7d4e79ab741..be30b852967 100644 --- a/loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedByteChannel.java +++ b/loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedByteChannel.java @@ -85,7 +85,7 @@ class NestedByteChannel implements SeekableByteChannel { while (dst.remaining() > 0) { int count = this.resources.getData().read(dst, this.position); if (count <= 0) { - return (total != 0) ? 0 : count; + return (total != 0) ? total : count; } total += count; this.position += count; diff --git a/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/nio/file/NestedByteChannelTests.java b/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/nio/file/NestedByteChannelTests.java index ad6d0a6a129..a14ae6d6325 100644 --- a/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/nio/file/NestedByteChannelTests.java +++ b/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/nio/file/NestedByteChannelTests.java @@ -118,11 +118,19 @@ class NestedByteChannelTests { void readReadsBytesAndIncrementsPosition() throws IOException { ByteBuffer dst = ByteBuffer.allocate(10); assertThat(this.channel.position()).isZero(); - this.channel.read(dst); + assertThat(this.channel.read(dst)).isEqualTo(10); assertThat(this.channel.position()).isEqualTo(10L); assertThat(dst.array()).isNotEqualTo(ByteBuffer.allocate(10).array()); } + @Test + void whenDataCannotFillBufferReadReturnsBytesRead() throws IOException { + ByteBuffer dst = ByteBuffer.allocate(8192); + assertThat(this.channel.position()).isZero(); + assertThat(this.channel.read(dst)).isEqualTo(638); + assertThat(this.channel.position()).isEqualTo(638L); + } + @Test // gh-38592 void readReadsAsManyBytesAsPossible() throws Exception { // ZipFileSystem checks that the number of bytes read matches an expected value