KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > context > exe > converter > SerializableToByteArrayConverter


1 package org.jbpm.context.exe.converter;
2
3 import java.io.*;
4
5 import org.jbpm.bytes.ByteArray;
6 import org.jbpm.context.exe.*;
7
8 public class SerializableToByteArrayConverter implements Converter {
9
10   private static final long serialVersionUID = 1L;
11   
12   public boolean supports(Class JavaDoc clazz) {
13     return Serializable.class.isAssignableFrom(clazz);
14   }
15
16   public Object JavaDoc convert(Object JavaDoc o) {
17     byte[] bytes = null;
18     try {
19       ByteArrayOutputStream baos = new ByteArrayOutputStream();
20       ObjectOutputStream oos = new ObjectOutputStream(baos);
21       oos.writeObject(o);
22       oos.flush();
23       bytes = baos.toByteArray();
24     } catch (IOException e) {
25       throw new RuntimeException JavaDoc("couldn't serialize '"+o+"'", e);
26     }
27     
28     return new ByteArray(bytes);
29   }
30
31   public Object JavaDoc revert(Object JavaDoc o) {
32     ByteArray byteArray = (ByteArray) o;
33     try {
34       ByteArrayInputStream bais = new ByteArrayInputStream(byteArray.getBytes());
35       ObjectInputStream ois = new ObjectInputStream(bais);
36       return ois.readObject();
37     } catch (Exception JavaDoc e) {
38       throw new RuntimeException JavaDoc("couldn't deserialize object", e);
39     }
40   }
41 }
42
Popular Tags