Friday, January 29, 2010

Java Large File Transfer using NIO

Most of the applications require large data transfer from one location to other. If we use normal Java IO, it will take a lot of time and resources. Also, we may end up not utilizing the available resources properly.

In one of my current development, I came across such issue and we also require non blockable I/O. So, going through the documentation available by Sun (now Oracle), I've written a simple class and tested it's performance on a local desktop machine.

The basic version of the File Transfer code is given here. I can't paste the code that we're using, because of IP and blah blah blah !!!



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;


/**
* This class is used for Large File Transfer.
*
* @author swapnil
*
*/
public class LargeFileTransfer
{
/**
* @param args
* @throws IOException
*/
public static void main (String [] args) throws IOException
{
File inFile = new File("c:/test/src/src.rar");
File outFile = new File("c:/test/dest/dest.rar");
copyFile (inFile, outFile);
}

/**
* This method is used to copy the Large file from source specified by in to the
* destination specified by out.
*
* @param in
* @param out
* @throws IOException
*/
public static void copyFile (File in, File out) throws IOException
{
FileChannel inChannel = new FileInputStream(in).getChannel ();
FileChannel outChannel = new FileOutputStream(out).getChannel ();

try
{
// For Windows, 64Mb is limit
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size ();
long position = 0;

//copy the File, buffer by buffer
while (position < size)
{
position += inChannel.transferTo (position, maxCount, outChannel);
}
}
catch (IOException e)
{
throw e;
}
finally
{
if (inChannel != null)
{
//close the Input Stream
inChannel.close ();
}

if (outChannel != null)
{
//close the Output Stream
outChannel.close ();
}
}
}
}





Whoilla!!! It's too simple and having really good performance. Check it out yourself. I'm trying to fine tune it for Linux....

No comments:

Post a Comment