import java.io.File; import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * Compares performance of different IO APIs * by copying the content of a file. */ public class FileCopyTimer { private File inFile; private File outFile; private long startTime; /** * Constructs a class that copies the content of inFile * to outFile. */ public FileCopyTimer(File inFile, File outFile) { this.inFile = inFile; this.outFile = outFile; } /** * Uses the InputStream/OutputStream API in java.io */ public long testStreamAPI() { startTimer(); try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile)); int c = 0; while ((c = bis.read()) != -1) { bos.write(c); } bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } return stopTimer(); } /** * Uses the Reader/Writer API in java.io */ public long testReaderWriterAPI() { startTimer(); try { BufferedReader br = new BufferedReader(new FileReader(inFile)); BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); int c = 0; while ((c = br.read()) != -1) { bw.write(c); } br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } return stopTimer(); } /** * Uses an indirect buffer in java.nio */ public long testIndirectNio() { startTimer(); try { FileInputStream fis = new FileInputStream(inFile); FileOutputStream fos = new FileOutputStream(outFile); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int c = 0; while ((c = inChannel.read(buffer)) != -1) { buffer.flip(); outChannel.write(buffer); buffer.clear(); } fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } return stopTimer(); } /** * Uses a direct buffer in java.nio */ public long testDirectNio() { startTimer(); try { FileInputStream fis = new FileInputStream(inFile); FileOutputStream fos = new FileOutputStream(outFile); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int c = 0; while ((c = inChannel.read(buffer)) != -1) { buffer.flip(); outChannel.write(buffer); buffer.clear(); } fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } return stopTimer(); } private void startTimer() { startTime = System.currentTimeMillis(); } private long stopTimer() { long time = System.currentTimeMillis() - startTime; startTime = 0; return time; } public static void main(String[] args) { FileCopyTimer fct = new FileCopyTimer(new File(args[0]), new File(args[1])); System.out.println("Using the Stream API:" + fct.testStreamAPI() + " ms"); System.out.println("Using the ReaderWriter API:" + fct.testReaderWriterAPI() + " ms"); System.out.println("Using the indirect nio API:" + fct.testIndirectNio() + " ms"); System.out.println("Using the direct nio API:" + fct.testDirectNio() + " ms"); } }