Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 311
Gandalf Avatar asked Jun 17 '10 16:06

Gandalf


3 Answers

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.

like image 165
erickson Avatar answered Nov 13 '22 13:11

erickson


HttpServletRequest gives you a regular "pull" input stream - don't see how using NIO will help here.

like image 21
mdma Avatar answered Nov 13 '22 11:11

mdma


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.

like image 1
irreputable Avatar answered Nov 13 '22 11:11

irreputable