From 352726f7409fb23f0d42cfc858bed59ee73d3fd6 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 3 Jun 2026 15:56:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20loop=20FileChannel.transferTo()=20?= =?UTF-8?q?=E2=80=94=20it=20can=20transfer=20fewer=20bytes=20than=20reques?= =?UTF-8?q?ted=20without=20looping,=20silently=20truncating=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/dostalgia/StaticResource.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/dostalgia/StaticResource.java b/src/main/java/com/dostalgia/StaticResource.java index 071b71d..b1935a8 100644 --- a/src/main/java/com/dostalgia/StaticResource.java +++ b/src/main/java/com/dostalgia/StaticResource.java @@ -171,7 +171,15 @@ public class StaticResource { } else { // Partial content (Range request) — transfer only the requested bytes try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) { - ch.transferTo(offset, length, java.nio.channels.Channels.newChannel(output)); + long remaining = length; + long pos = offset; + while (remaining > 0) { + long written = ch.transferTo(pos, remaining, + java.nio.channels.Channels.newChannel(output)); + if (written <= 0) break; + pos += written; + remaining -= written; + } } } }