// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class Read {
    private static void test(String fnm, int expect, int blksiz) throws IOException {
        long t1 = System.currentTimeMillis();
        try(InputStream is = new FileInputStream(fnm)) {
            int n = 0;
            byte[] buf = new byte[blksiz];
            int nb;
            while((nb = is.read(buf)) > 0) {
                for(int i = 0; i < nb; i++) {
                    if(buf[i] == '\n') n++;
                }
            }
            if(n != expect) {
                System.out.printf("%d lines\n", n);
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("Java InputStream.read (block size %d) : %d ms\n", blksiz, t2 - t1);
    }
    public static void main(String[] args) throws IOException {
        int[] blksiz = { 512, 5120, 51200, 512000 };
        for(int j = 0; j < 4; j++) {
            for(int i = 0; i < 10; i++) {
                test(args[0], Integer.parseInt(args[1]), blksiz[j]);
            }
        }
    }
}
