title: Can I call an underlying nio channel for a response in Jetty web app?
body: <p>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 <code>java.io.OutputStream</code> of the <code>HttpServletResponse</code>.</p>
  
  <pre><code>File file = ...
  response.setContentLength((int)file.length());
  FileInputStream in = new FileInputStream(file);            
  IOUtils.copy(in, response.getOutputStream());  
  </code></pre>
  
  <p>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 <a href="http://download.eclipse.org/jetty/stable-8/apidocs/index.html?org/eclipse/jetty/server/nio/SelectChannelConnector.html" rel="nofollow">org.eclipse.jetty.server.nio.SelectChannelConnector</a>. </p>
  
  <p>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 <code>java.nio</code> instead of <code>java.io</code>?</p>
  
  <p>Their docs show a <a href="http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Hello_World_Handler" rel="nofollow">Jetty "hello world" handler</a>, but that is also using <code>HttpServletResponse</code> and <code>java.io</code> streams.</p>