views:

141

answers:

4

Is there an efficient mechanism in Java for transferring large amounts of data (~10 GB) back and forth between the disk and the network on a low-end machine? Low-end machine meaning a machine with much less than 10 GB of main memory. I want to efficiently transfer ~10GB portions of a file from one machine to another.

+1  A: 

The most efficient means of I/O in Java is the NIO libraries. If your data is appropriate applying a compression stream filter would help as well.

fuzzy lollipop
also, compression is only worthwhile if you are network-bound. If the net is fast enough, the benefit is much smaller (no saved time).
tucuxi
that is why I said if your data is appropriate, text in particular compresses very well and does save time, even with a fast connection. Things like JPG's which are already compressed would not. Regardless of network speeds compression when appropriate __always__ save time.
fuzzy lollipop
A: 

traditional socket and file io would be fine.

in java 6, there is no direct file-socket io optimization as far as I know.

even if there is, as rumored in java 7, it won't improve performance very much. your code would be either disk bound or network bound. mem copy shouldn't matter.

irreputable
I concur - you are sure to be bound by network or disk bandwidth. Processing is genearally orders of magnitude faster than I/O.
tucuxi
The transferTo() method has been there since Java 1.4 so this answer is wrong.
EJP
+1  A: 

I think you need to just copy an InputStream to an OutputStream. I'd start off with using somebody else's implementation commons-io:IOUtils.copyLarge(InputStream,OutputStream)

InputStream is = null;
OutputStream os = null;
try{
    InputStream is = new FileInputStream("yourfile.bin");
    OutputStream os = remoteSocket.getOutputStream();
    IOUtils.copyLarge(is,os);
}finally{
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(out);
}

This should be a good starter for ten. If you need higher through put, you can start my putting the read and write actions in separate threads, this in theory should ensure you completely saturate the slowest of the links, but it may be a better choice to use the FileChannel#transferTo method if using traditional stuff just isn't enough.

Gareth Davis
A: 

FYI, transferTo() only is only optimized between files, not file-socket, in java 6

irreputable