View Javadoc

1   package org.lcsim.lcio;
2   
3   import hep.io.sio.SIOBlock;
4   import hep.io.sio.SIOInputStream;
5   import hep.io.sio.SIOOutputStream;
6   import hep.io.sio.SIOWriter;
7   import java.io.IOException;
8   import java.util.List;
9   import java.util.Map;
10  import org.lcsim.event.EventHeader.LCMetaData;
11  
12  import org.lcsim.event.GenericObject;
13  
14  /**
15   *
16   * @author tonyj
17   */
18  class SIOGenericObjectBlockHandler extends AbstractBlockHandler
19  {
20     public String getType() { return "LCGenericObject"; }
21     public Class getClassForType() { return GenericObject.class; }
22  
23     @Override
24     LCIOCallback readCollection(SIOInputStream in, int flags, SIOLCParameters colParameters, LCIOEvent event, SIOBlock block, int version) throws IOException {
25        LCIOCollection collection = new LCIOCollection(getClassForType(), flags, 0, colParameters);
26        event.put(block.getBlockName(), collection);
27        return addCollectionElements(event, collection, in, 0, version);
28     }
29     LCIOCallback addCollectionElements(LCIOEvent event, LCIOCollection collection, SIOInputStream in, int n, int version) throws IOException
30     {
31        int flags = collection.getFlags();
32        if (LCIOUtil.bitTest(flags,LCIOConstants.GOBIT_FIXED))
33        {
34           int nInt = in.readInt();
35           int nFloat = in.readInt();
36           int nDouble = in.readInt();
37           n = in.readInt();
38           for (int i = 0; i < n; i++)
39              collection.add(new SIOGenericObject(in, flags, version, nInt, nFloat, nDouble));
40        }
41        else
42        {
43           n = in.readInt();
44           for (int i = 0; i < n; i++)
45              collection.add(new SIOGenericObject(in, flags, version));
46        }
47        return null;
48     }
49  
50      @Override
51      public void writeBlock(SIOWriter writer, List collection, LCMetaData md) throws IOException {
52        SIOOutputStream out = writer.createBlock(md.getName(), LCIOConstants.MAJORVERSION, LCIOConstants.MINORVERSION);
53  
54        List<GenericObject> goCollection = collection;
55        // Note: treating empty collections as having variable size saves space
56        boolean isFixedSize = !goCollection.isEmpty();
57        if (isFixedSize) {
58            int nInt = goCollection.get(0).getNInt();
59            int nFloat = goCollection.get(0).getNFloat();
60            int nDouble = goCollection.get(0).getNDouble();
61            for (GenericObject object : goCollection) {
62                isFixedSize &= object.getNInt()==nInt && object.getNFloat()==nFloat && object.getNDouble()==nDouble;
63            }
64        }
65        int flags = md.getFlags();
66        flags = LCIOUtil.bitSet(flags, LCIOConstants.GOBIT_FIXED, isFixedSize);
67        out.writeInt(flags);
68        Map<String,int[]> intMap = md.getIntegerParameters();
69        Map<String,float[]> floatMap = md.getFloatParameters();
70        Map<String,String[]> stringMap = md.getStringParameters();
71        SIOLCParameters.write(intMap,floatMap,stringMap,out);
72        if (isFixedSize) {
73            // We know collection is therefore not empty
74            out.writeInt(goCollection.get(0).getNInt());
75            out.writeInt(goCollection.get(0).getNFloat());
76            out.writeInt(goCollection.get(0).getNDouble());
77        }
78        out.writeInt(collection.size());
79        for (Object element : collection)
80        {
81           writeCollectionElement(element,out,flags);
82        }
83        out.close();    }
84     
85     void writeCollectionElement(Object element, SIOOutputStream out, int flags) throws IOException
86     {
87        SIOGenericObject.write((GenericObject) element, out, flags);
88     }
89  }