View Javadoc

1   package org.lsst.ccs.bus;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.io.NotSerializableException;
7   import java.io.ObjectInputStream;
8   import java.io.ObjectOutputStream;
9   import java.io.Serializable;
10  
11  /**
12   * a Simplified Marshalled Object.
13   * @author bamade
14   */
15  // Date: 05/04/13
16  
17  public class DataCapsule implements Serializable {
18  
19      private static final long serialVersionUID = -2212216649618803537L;
20      private Object data ;
21  
22      public DataCapsule(Serializable data)  {
23          // now we serialize
24          ByteArrayOutputStream bos = new ByteArrayOutputStream();
25          try {
26              ObjectOutputStream oos = new ObjectOutputStream(bos);
27              oos.writeObject(data);
28              oos.flush();
29              this.data = bos.toByteArray();
30              oos.close();
31          } catch (NotSerializableException e) {
32              throw new AssertionError("crystallization in DataCapsule failed: " + e);
33          } catch (IOException e) {
34              throw new AssertionError("crystallization in DataCapsule failed: " + data);
35          }
36      }
37  
38  
39      public Object getData() throws ClassNotFoundException {
40          Object res = null ;
41          ByteArrayInputStream bis = new ByteArrayInputStream((byte[])data) ;
42          try {
43              ObjectInputStream ois = new ObjectInputStream(bis) ;
44              res = ois.readObject() ;
45              ois.close();
46          } catch (IOException e) {
47              throw new AssertionError("unfreezing in DataCapsule failed: " + e);
48          } catch (ClassNotFoundException e) {
49               throw e ;
50          }
51          return res ;
52      }
53  
54      public String toString() {
55          return "crystallized_object" ;
56      }
57  
58  
59  }