// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadTest2 {
    public static void main(String[] args) {
        (new NormalSingleReadBufferedTest()).test();
        (new SpecialSingleReadBufferedTest()).test();
    }
}

abstract class OneTest2 {
    protected static final String FILE_NAME = "C:\\log4j-1.2.9.jar";
    private static final int N = 10;
    protected int sum;
    private String name;
    OneTest2(String name) {
        this.name = name;
        sum = 0;
    }
    void test() {
        try {
            long t1 = System.currentTimeMillis();
            for (int i = 0; i < N; i++) {
                dotest();
            }
            long t2 = System.currentTimeMillis();
            System.out.println(name + " : result = " + sum + " time = " + (t2 - t1));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    abstract void dotest() throws IOException;
}

class NormalSingleReadBufferedTest extends OneTest2 {
    NormalSingleReadBufferedTest() {
        super("normal single byte read buffered stream");
    }
    void dotest() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
        int c;
        while ((c = bis.read()) >= 0) {
            sum += (byte) c;
        }
        bis.close();
   }
}

class SpecialSingleReadBufferedTest extends OneTest2 {
    SpecialSingleReadBufferedTest() {
        super("special single byte read buffered stream");
    }
    void dotest() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
        while (bis.available() > 0) {
            sum += (byte) bis.read();
        }
        bis.close();
    }
}
