fix: use Files.copy for full-file responses (proven working), FileChannel.transferTo only for Range requests
Build & Deploy / build-and-deploy (push) Successful in 41s

This commit is contained in:
Hermes Agent
2026-06-03 15:56:08 +02:00
parent 7f534689fb
commit 113f79d755
@@ -159,14 +159,18 @@ public class StaticResource {
/** /**
* Streaming file output that avoids loading the whole file into memory. * Streaming file output that avoids loading the whole file into memory.
* Supports partial reads (offset + length) for HTTP Range requests. * Uses Files.copy for full-file (zero-copy via sendfile) and a position-limited
* Uses FileChannel.transferTo() for zero-copy sendfile semantics. * transfer for HTTP Range requests.
*/ */
private record FileStream(Path file, long offset, long length) implements StreamingOutput { private record FileStream(Path file, long offset, long length) implements StreamingOutput {
@Override @Override
public void write(OutputStream output) throws IOException { public void write(OutputStream output) throws IOException {
if (offset == 0 && length == Files.size(file)) {
// Full file — use simple Files.copy (proven working)
Files.copy(file, output);
} else {
// 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)) {
if (length > 0) {
ch.transferTo(offset, length, java.nio.channels.Channels.newChannel(output)); ch.transferTo(offset, length, java.nio.channels.Channels.newChannel(output));
} }
} }