import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufTest {
    static final int NUM_RECORDS = 128 * 1024;
    static final int REC_SIZE = 64;

    public static void main( String[] args ) throws Exception {
	int bufSize = 4096;
	if ( args.length >= 1 ) {
	    try {
		bufSize = Integer.parseInt( args[0] );
	    }
	    catch( NumberFormatException ex ) {
		// ignore
	    }
	}
	System.out.println("buffer size " + bufSize );

	byte[] buf = new byte[REC_SIZE];
	for( int i = 0 ; i < buf.length; i++ ) buf[i] = (byte)i;

	File saveFile = new File("speed1.dat");
	FileOutputStream save = new FileOutputStream( saveFile );
	long dt = writeTest( buf, save );
	saveFile.delete();
	System.out.println("nonbuffered " + dt );

	File bufFile = new File("speed2.dat");
	BufferedOutputStream buffered =
	    new BufferedOutputStream(
		new FileOutputStream( bufFile ), bufSize );
	dt = writeTest( buf, buffered );
	bufFile.delete();
	System.out.println("buffered " + dt );
    }

    public static long writeTest( byte[] buf, OutputStream out )
	throws IOException
    {
	long t = System.currentTimeMillis();
	for( int i = 0 ; i < NUM_RECORDS; i++ ) {
	    out.write( buf );
	}
	out.close();
	return System.currentTimeMillis() - t;
    }
}
