fix: loop FileChannel.transferTo() — it can transfer fewer bytes than requested without looping, silently truncating files
Build & Deploy / build-and-deploy (push) Successful in 43s

This commit is contained in:
Hermes Agent
2026-06-03 15:56:27 +02:00
parent 113f79d755
commit 352726f740
@@ -171,7 +171,15 @@ public class StaticResource {
} else { } else {
// Partial content (Range request) — transfer only the requested bytes // Partial content (Range request) — transfer only the requested bytes
try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) { 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;
}
} }
} }
} }