开发者

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 actuall开发者_JAVA技巧y 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.


I think you're worrying about nothing. The average network speed (~1MB/s) can't catch up the average harddisk speed (~100MB/s) by far (even with the most optimistic averages). The filechannels won't give you any benefits. The "legacy" java.io is more than sufficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜