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
Build & Deploy / build-and-deploy (push) Successful in 41s
This commit is contained in:
@@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user