Dear Martin,
> > Wouldn't NIO come with some performance improvements
> especially for large
> > files?
>
> I don't know a whole lot about NIO, but I doubt that it would make any
> difference. Since a given request is guaranteed to be
> serviced entirely by a
> single thread, there's not much opportunity for getting
> clever. But then
> again, even if there was, that's part of the container, and
> not something
> Commons FileUpload could do anything about.
>
As NIO works directly on OS Pipes I used it in order to get around on the
byte buffer
in-VM memory problem. It's fairly easy to wrap ServletOutputStream with NIO
Channels:
/**
* Writes the file from server to client using NIO over HTTP.
*
* @param out stream from tomcat to client browser
* @param fileName the file to write over the wire
* @exception FileNotFoundException if the request file does not
exists
* @exception IOException if the sending process fails
*/
private void sendFile(ServletOutputStream out, String fileName)
throws FileNotFoundException, IOException {
RandomAccessFile file = null;
FileChannel fileChannel = null;
WritableByteChannel servletOutChannel = null;
MappedByteBuffer buffer = null;
try {
file = new RandomAccessFile(fileName, "r");
fileChannel = file.getChannel();
servletOutChannel = Channels.newChannel(out);
LOGGER.debug("Begin to write out:" +
fileChannel.size());
fileChannel.transferTo(0L, fileChannel.size(),
servletOutChannel);
} finally {
if (buffer != null)
buffer = null;
if (fileChannel != null)
fileChannel.close();
if (servletOutChannel != null)
servletOutChannel.close();
if (file != null)
file.close();
}
}
Using this approach vice versa (on InputStreams may) may work as well into a
MulitPartStream using
java.nio.ByteBuffer and Channels to transfer byte bulks. (Unfortunatly this
works only on
1.4+ which is not very portable.) I actually begin porting your module to
NIO but as I don't
understand _really_ what going on there (not the NIO stuff but the RFC is
not really a good guide) so if you interested into that clumsy code, fell
free to ask me for a copy. The main benifit of NIO for Upload would be that
you don't need to configure a max file size any more due to complete
transfer could be managed outside the JVM head memory.
Bye
Toby
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]