FileDocCategorySizeDatePackage
CopyTest.javaAPI DocExample4506Fri Jun 28 10:28:20 BST 2002tuning.nio

CopyTest

public class CopyTest extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidbufferedInputStreamCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    BufferedInputStream rdr = new BufferedInputStream(new FileInputStream(f1), 16*1024);
    BufferedOutputStream wrtr = new BufferedOutputStream(new FileOutputStream(f2), 16*1024);
    int c;
    while( (c = rdr.read()) != -1)
    {
      wrtr.write(c);
    }
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" bufferedInputStreamCopy time: " + time);
  
public static voidbufferedReaderCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    BufferedReader rdr = new BufferedReader(new FileReader(f1), 16*1024);
    BufferedWriter wrtr = new BufferedWriter(new FileWriter(f2), 16*1024);
    int c;
    while( (c = rdr.read()) != -1)
    {
      wrtr.write(c);
    }
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" bufferedReaderCopy time: " + time);
  
public static voiddirectBufferCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    ByteBuffer buffer = ByteBuffer.allocateDirect(16*1024);
    FileChannel rdr = (new FileInputStream(f1)).getChannel();
    FileChannel wrtr = (new FileOutputStream(f2)).getChannel();
    while( rdr.read(buffer) > 0)
    {
      buffer.flip();
      wrtr.write(buffer);
      buffer.clear();
    }
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" directBufferCopy time: " + time);
  
public static voiddirectTransferCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    FileChannel rdr = (new FileInputStream(f1)).getChannel();
    FileChannel wrtr = (new FileOutputStream(f2)).getChannel();
    rdr.transferTo(0, (int) rdr.size(), wrtr);
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" directTransferCopy time: " + time);
  
public static voidexplicitBufferInputStreamCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    byte[] buffer = new byte[1024*16];
    FileInputStream rdr = new FileInputStream(f1);
    FileOutputStream wrtr = new FileOutputStream(f2);
    int readLen;
    while( (readLen = rdr.read(buffer)) != -1)
    {
      wrtr.write(buffer, 0, readLen);
    }
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" explicitBufferInputStreamCopy time: " + time);
  
public static voidmain(java.lang.String[] args)

    String f1 = args[0];
    String f2 = args[1];
    test(f1,f2);
    test(f1,f2+'a");
  
public static voidmappedBufferCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    FileChannel rdr = (new FileInputStream(f1)).getChannel();
    FileChannel wrtr = (new FileOutputStream(f2)).getChannel();
    ByteBuffer buffer = rdr.map(FileChannel.MapMode.READ_ONLY, 0, (int) rdr.size());
    wrtr.write(buffer);
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" mappedBufferCopy time: " + time);
  
public static voidnonDirectBufferCopy(java.lang.String f1, java.lang.String f2)

    long time = System.currentTimeMillis();
    ByteBuffer buffer = ByteBuffer.allocate(16*1024);
    FileChannel rdr = (new FileInputStream(f1)).getChannel();
    FileChannel wrtr = (new FileOutputStream(f2)).getChannel();
    while( rdr.read(buffer) > 0)
    {
      buffer.flip();
      wrtr.write(buffer);
      buffer.clear();
    }
    rdr.close();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println(" nonDirectBufferCopy time: " + time);
  
public static voidtest(java.lang.String f1, java.lang.String f2)

    bufferedReaderCopy(f1, f2+'1");
    bufferedInputStreamCopy(f1, f2+'2");
    explicitBufferInputStreamCopy(f1, f2+'3");
    nonDirectBufferCopy(f1, f2+'4");
    directBufferCopy(f1, f2+'5");
    mappedBufferCopy(f1, f2+'6");
    directTransferCopy(f1, f2+'7");