package com.tagtraum.perf.gcviewer;

import com.tagtraum.perf.gcviewer.imp.*;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ResourceBundle;

/**
 *
 * Date: Jan 30, 2002
 * Time: 6:47:45 PM
 * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
 * @version $Id: $
 */
public class DataReaderFactory {

    private static ResourceBundle localStrings = ResourceBundle.getBundle("com.tagtraum.perf.gcviewer.localStrings");

    public DataReader getDataReader(InputStream inStream) throws IOException {
        BufferedInputStream in = new BufferedInputStream(inStream, 1024 * 4);
        in.mark(1024 * 4);
        byte[] buf = new byte[1024 * 3];
        int length = in.read(buf);
        in.reset();
        String s = new String(buf, 0, length, "ASCII");
        if (s.indexOf("GC cycle started") != -1) {
            return new DataReaderIBM1_3_1(in);
        } else if (s.indexOf("<AF") != -1) {
            // this should be an IBM JDK < 1.3.0
            return new DataReaderIBM1_3_0(in);
        } else if (s.indexOf(": [") != -1) {
            return new DataReaderSun1_4_0(in);
        } else if (s.indexOf("[GC") != -1 || s.indexOf("[Full GC") != -1 || s.indexOf("[Inc GC")!=-1) {
            return new DataReaderSun1_3_1(in);
        } else if (s.indexOf("<GC: managing allocation failure: need ") != -1) {
            return new DataReaderSun1_2_2(in);
        } else
            throw new IOException(localStrings.getString("datareaderfactory_instantiation_failed"));
    }
}
