Search code examples
javaperformanceservletsnio

Java NIO Servlet to File


Is there a way (without buffering the whole Inputstream) to take the HttpServletRequest from a Java Servlet and write it out to a file using all NIO? Is it even worth trying? Will it be any faster reading from a normal java.io stream and writing to a java.nio Channel or do they both really need to be pure NIO to see a benefit? Thanks.

EDIT:

So I just did a quick and dirty benchmark, reading a file from one disk and writing to a different disk (so I'm actually testing the code and not the disk).

Averages:
InputStream -> OutputStream : 321 ms.
FileChannel -> FileChannel  :   3 ms.
InputStream -> FileChannel  : 600 ms.

I actually got worse performance trying to use a hybrid java.io -> java.nio. The nio->nio was faster by A LOT, but I'm stuck with the Servlet InputStream.


Solution

  • The primary benefit of a pure NIO solution is that you may be able to avoid copying data from kernel to user and back to kernel space. When you use a transferTo() or transferFrom() operation, this overhead can be avoided and transfers between channels can be very fast (depending on the underlying implementation).

    However, the Servlet API doesn't allow you to access a source Channel; by the time your servlet sees the data, they're in user space. So I wouldn't expect a performance boost from writing to a Channel.