import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.geotools.data.shapefile.dbf.DbaseFileHeader;


public class DBFHeaderReader {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        File file = new File("/home/aaime/devel/gisData/foss4g2010/vector2010");
        int fileCount = 0;
        for(File f : file.listFiles()) {
            if(f.getName().endsWith(".dbf")) {
                int count = readHeader(f.getAbsolutePath());
                fileCount++;
                // System.out.println(f.getName() + " - " + count);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(fileCount);
        System.out.println((end - start) / 1000.0);
    }
    
    public static int readHeader(String dbfPath) throws Exception {
        FileChannel channel = null;
        try {
            channel = new FileInputStream(dbfPath).getChannel();
            DbaseFileHeader dheader = new DbaseFileHeader();
            ByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, 2048);
            dheader.readHeader(bb);
            return dheader.getNumRecords();
        } finally {
            if(channel != null) {
                channel.close();
            }
        }
    }
}
