diff --git a/src/main/java/com/dostalgia/StaticResource.java b/src/main/java/com/dostalgia/StaticResource.java index b2b8600..2f5d4fa 100644 --- a/src/main/java/com/dostalgia/StaticResource.java +++ b/src/main/java/com/dostalgia/StaticResource.java @@ -161,24 +161,15 @@ public class StaticResource { /** * Streaming file output that avoids loading the whole file into memory. - * Supports partial reads (seek + limit) for HTTP Range requests. + * Supports partial reads (offset + length) for HTTP Range requests. + * Uses FileChannel.transferTo() for zero-copy sendfile semantics. */ private record FileStream(Path file, long offset, long length) implements StreamingOutput { @Override public void write(OutputStream output) throws IOException { - try (var raf = new RandomAccessFile(file.toFile(), "r"); - var channel = raf.getChannel()) { - if (offset > 0) { - raf.seek(offset); - } - long remaining = length; - byte[] buf = new byte[8192]; - while (remaining > 0) { - int toRead = (int) Math.min(buf.length, remaining); - int read = raf.read(buf, 0, toRead); - if (read < 0) break; - output.write(buf, 0, read); - remaining -= read; + try (FileChannel ch = FileChannel.open(file, StandardOpenOption.READ)) { + if (length > 0) { + ch.transferTo(offset, length, output); } } }