// 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.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class MapFile {
    private static final int OFFSET = 0;
    private static final int SIZE = 102400000;
    private static void test(String fnm, int expect) throws IOException {
        long t1 = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(fnm);
        FileChannel fc = fis.getChannel();
        ByteBuffer bb = fc.map(MapMode.READ_ONLY, OFFSET, SIZE);
        bb.rewind();
        int n = 0;
        for(int ix = 0; ix < SIZE; ix++) {
            byte b = bb.get(ix);
            if(b == '\n') {
                n++;
            }
        }
        if(n != expect) {
            System.out.printf("%d lines\n", n);
        }
        fc.close();
        long t2 = System.currentTimeMillis();
        System.out.printf("Java FileChannel.map file : %d ms\n", t2 - t1);
    }
    public static void main(String[] args) throws IOException {
        for(int i = 0; i < 10; i++) {
            test(args[0], Integer.parseInt(args[1]));
        }
    }
}
