// Copyright 2000-2003, FreeHEP.
package hep.graphics.heprep.test;

import java.io.*;
import java.util.*;
import java.util.zip.*;

import org.freehep.xml.util.*;

import hep.graphics.heprep.*;
import hep.graphics.heprep.xml.*;

/**
 * Reads multiple hepreps from one xml file.
 *
 * @author M.Donszelmann
 *
 * @version $Id: MultiReadTest.java,v 1.1 2003/06/28 20:45:39 duns Exp $
 */

public class MultiReadTest {
    public static final String cvsId = "$Id: MultiReadTest.java,v 1.1 2003/06/28 20:45:39 duns Exp $";

    private int read(HepRepFactory factory, String filename) throws IOException {
        InputStream fis = new FileInputStream(filename);
        if (filename.endsWith(".gz")) fis = new GZIPInputStream(fis);
        if (filename.endsWith(".zip")) fis = new ZipInputStream(fis);
        HepRepReader reader = factory.createHepRepReader(fis);
        System.out.println("Sequential: "+reader.hasSequentialAccess());
        System.out.println("Random: "+reader.hasRandomAccess());
        int i=0;
        try {
            while (reader.hasNext()) {
                HepRep heprep = reader.next();
                i++;
                System.out.print(".");
            }
        } catch (EOFException eof) {
        }
        System.out.println();
        reader.close();
        fis.close();
        return i;
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("Usage: MultiReadTest filename");
            System.exit(1);
        }

        long start = System.currentTimeMillis();
        HepRepFactory factory = HepRepFactory.create();
        int n = new MultiReadTest().read(factory, args[0]);
        System.out.println("Read "+n+" events in "+(System.currentTimeMillis()-start)+" ms.");
    }
}
