package hep.rootio.reps;

import hep.rootio.*;
import hep.rootio.implementation.*;

import java.io.*;
import java.lang.ref.SoftReference;
import java.util.zip.Inflater;

public class TKey extends SpecificRootObject
{

    public TKey()
    {
    }

    public void read(RootInput in)
        throws IOException
    {
        put("fNbytes", fNbytes = in.readInt());
        int v = in.readVersion();
        put("fObjlen", fObjlen = in.readInt());
        put("fDatime", in.readObject("TDatime"));
        put("fKeylen", fKeylen = in.readShort());
        put("fCycle", in.readShort());
        put("fSeekKey", fSeekKey = in.readInt());
        put("fSeekPdir", in.readInt());
        put("fClassName", in.readObject("TString"));
        put("fName", in.readObject("TString"));
        put("fTitle", in.readObject("TString"));
        raf = in.getRAF();
    }

    public RootObjectRepresentation getObject()
        throws RootClassNotFound, IOException
    {
        object = soft != null ? (RootObjectRepresentation)soft.get() : null;
        if(object == null)
        {
            object = createObject(getObjectClass());
            raf.seek(fSeekKey + fKeylen);
            if(fObjlen > fNbytes - fKeylen)
            {
                RootInput in = getData();
                object.read(in);
            } else
            {
                raf.setMap(fKeylen);
                object.read(raf);
                raf.clearMap();
            }
            soft = new SoftReference(object);
        }
        return object;
    }

    RootInputStream getData()
        throws IOException
    {
        byte buf[] = new byte[fNbytes - fKeylen];
        int rc = raf.read(buf);
        if(rc != buf.length)
            throw new IOException("Unexpected buffer length, expected " + buf.length + " got " + rc);
        if(rc < fObjlen)
        {
            byte out[] = new byte[fObjlen];
            int nout = 0;
            int nin = 0;
            while(nout < fObjlen) 
            {
                Inflater inf = new Inflater(true);
                try
                {
                    nin += 9;
                    inf.setInput(buf, nin, buf.length - nin);
                    rc = inf.inflate(out, nout, out.length - nout);
                    nout += rc;
                    nin += inf.getTotalIn();
                }
                catch(Exception exception)
                {
                    throw new IOException("Error during decompression");
                }
                finally
                {
                    inf.end();
                }
            }
            return new RootInputStream(new RootByteArrayInputStream(out, fKeylen), raf);
        } else
        {
            return new RootInputStream(new RootByteArrayInputStream(buf, fKeylen), raf);
        }
    }

    private RootObjectRepresentation createObject(RootClass k)
        throws RootClassNotFound
    {
        return k.newInstance();
    }

    public int seekKey()
    {
        return fSeekKey;
    }

    public int keyLen()
    {
        return fKeylen;
    }

    public RootClass getObjectClass()
        throws RootClassNotFound
    {
        return raf.getFactory().create(get("fClassName").toString());
    }

    public String getName()
    {
        return get("fName").toString();
    }

    public String getTitle()
    {
        return get("fTitle").toString();
    }

    private int fNbytes;
    private int fObjlen;
    private int fKeylen;
    private int fSeekKey;
    private SoftReference soft;
    private RootObjectRepresentation object;
    private RootRandomAccessFile raf;
}

 

Ads help maintain this website.