I'm writing a web app that is sending some straight files in response to some requests. I want to handle this in Java, not nginx, et al. In a standard servlet, the only option is to use the java.io.OutputStream
of the HttpServletResponse
.
File file = ...
response.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
IOUtils.copy(in, response.getOutputStream());
This copies byte buffers more than is necessary. I'd like to see if I can improve performance by using NIO buffers and channels. I know Jetty is using NIO under the hood because the "connector" in my server is of class org.eclipse.jetty.server.nio.SelectChannelConnector.
Is there a way to get at the underlying channels from the servlet? Or is there a way to define a Jetty-specific handler that uses java.nio
instead of java.io
?
Their docs show a Jetty "hello world" handler, but that is also using HttpServletResponse
and java.io
streams.