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.
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
.
HttpServletRequest gives you a regular "pull" input stream - don't see how using NIO will help here.
you can't seriously compare performance with tests as short as milliseconds.
disks won't spin any faster because of choice of API. what happened in FileChannel->FileChannel is probably that the call returned before the writes actually committed to the disk.
nio
could save some cpu/memory usage. but not much in your situation. for saving files uploaded by users, usually it's in the format of multipart/form-data, and server must read the stream byte-by-byte to parse the input and extract file content, it cannot just directly dump raw stream to file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With