next next up down toc Memos schedule allInOne PDF PDF mail
all, section 17.10.

17.10.  Size Matters: The second Copy Program

Does the size of the read and write blocks matter?

Src/9_was/InOut_2.java.minusSTART_STOP


import java.io.*;


public class InOut_2 {
  static final int BUFSIZE = 1024;

  public static void copy( String inF , String outF, int bufSize ) {
    DataInputStream in;
    DataOutputStream out;
    byte[]  buffer = new byte[bufSize];
    int     n;

    
    try { 
        in = new DataInputStream(
                                new FileInputStream(inF) );
        out = new DataOutputStream(
                                new FileOutputStream(outF) );
        
        while ( (n = in.read(buffer) ) != -1 ) {
                out.write(buffer, 0, n);
        }

        out.close();
        in.close();

    }
    catch ( FileNotFoundException ef)   {
        System.out.println(ef.getMessage() );
    }
    catch ( IOException ef)     {
        System.out.println(ef.getMessage() );
    }
    catch ( Exception e)        {
        System.out.println("ExceptionType occurred: " + 
                e.getMessage() );
    }
  }


  public static void main( String args[] ) {
    int     bufSize = BUFSIZE;

    if ( args.length < 2 )      {
        System.err.println(
             "Usage: java InOut_1 from to [size]");
        System.exit(1);
    }

    if ( args.length  == 3 )    {
        try {
                bufSize = Integer.parseInt(args[2]);
        }
        catch ( NumberFormatException e )       {
                System.out.println("Can't convert " + args[2] 
                        + " to an integer.");
        }

    }
    System.out.println("BufferSize = " + bufSize);
    copy(args[0], args[1], bufSize);
  }
}

Source Code: Src/9_was/InOut_2.java

Result:

% for i in 1 2 512 1024 10240
> do
> /usr/bin/time java InOut_2 from to $i && diff from to
> done
BufferSize = 1

real       49.8
user       21.3
sys        24.4
BufferSize = 2

real       27.0
user       10.8
sys        12.3
BufferSize = 512

real        0.8
user        0.2
sys         0.2
BufferSize = 1024

real        0.8
user        0.1
sys         0.2
BufferSize = 10240

real        1.0
user        0.2
sys         0.1


back next up down toc Memos schedule allInOne pdf PDF mail

Created by unroff, java2html & & hp-tools. © by csfac. All Rights Reserved (2010).
It is not allowed to print these pages on a CAST printer.
Last modified: 01/April/10 (17:16)