I need to return a big file from a Jetty server, and I founded 3 different ways in my company source-code to do that. I am now trying to understand the pros/cons of them performance-wise, memory and time.
Option A:
Path path = <FilePath>;
return path.toFile();
Easiest to write. However, does it cause the entire file to be loaded to the memory before it's being sent? or does jetty stream it?
Option B:
Path path = <FilePath>;
return new FileInputStream(path.toFile());
Is turning the file to stream here have any effect comparing to Option A?
Option C:
Path path = <FilePath>;
return new StreamingOutput() {
@Override
public void write(final OutputStream out) throws IOException {
copy(path, out);
}
};
Is the copy here needed? isn't it redundant, in compare to Option B?
Option D:
Based on: Streaming Large Files In a Java Servlet
Path path = <FilePath>;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path.toFile());
out = response.getOutputStream();
IOUtils.copy(in, out);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
If there are other/better options, please share them.