diff --git a/src/main/java/com/dostalgia/StaticResource.java b/src/main/java/com/dostalgia/StaticResource.java index fdbc6de..071b71d 100644 --- a/src/main/java/com/dostalgia/StaticResource.java +++ b/src/main/java/com/dostalgia/StaticResource.java @@ -159,14 +159,18 @@ public class StaticResource { /** * Streaming file output that avoids loading the whole file into memory. - * Supports partial reads (offset + length) for HTTP Range requests. - * Uses FileChannel.transferTo() for zero-copy sendfile semantics. + * Uses Files.copy for full-file (zero-copy via sendfile) and a position-limited + * transfer for HTTP Range requests. */ private record FileStream(Path file, long offset, long length) implements StreamingOutput { @Override public void write(OutputStream output) throws IOException { - try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) { - if (length > 0) { + 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)) { ch.transferTo(offset, length, java.nio.channels.Channels.newChannel(output)); } }