// Copyright 2000-2003, FreeHEP.

package hep.graphics.heprep.ref;

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

import hep.graphics.heprep.*;
import hep.graphics.heprep.util.*;

/**
 * Read in java serialized IO
 *
 * @author M.Donszelmann
 *
 * @version $Id: DefaultHepRepReader.java,v 1.3 2004/06/03 17:39:29 duns Exp $
 */

public class DefaultHepRepReader extends AbstractHepRepReader {
    public static final String cvsId = "$Id: DefaultHepRepReader.java,v 1.3 2004/06/03 17:39:29 duns Exp $";

    protected DefaultHepRepReader(InputStream in) throws IOException {
        super(in);
        reset();        
    }

    protected DefaultHepRepReader(String fileName) throws IOException {
        super(fileName);
        reset();
    }

    public void reset() throws IOException, UnsupportedOperationException {
        if (name != null) {
            if (name.toLowerCase().endsWith(".gz")) {
                input = new GZIPInputStream(new FileInputStream(name));
            } else {
                input = new FileInputStream(name);
            }
        } else {
            super.reset();
        }
    }

    public HepRep readHepRep(InputStream input) throws IOException {
        try {
            ObjectInputStream oin = new ObjectInputStream(input);
            return (HepRep)oin.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException(getClass()+" Class not found: "+e.getMessage());
        } catch (NoClassDefFoundError e) {
            throw new IOException(getClass()+" ClassDef not found: "+e.getMessage());
        }
    }
}

