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 clazz) { 13 return Serializable.class.isAssignableFrom(clazz); 14 } 15 16 public Object convert(Object 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 ("couldn't serialize '"+o+"'", e); 26 } 27 28 return new ByteArray(bytes); 29 } 30 31 public Object revert(Object 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 e) { 38 throw new RuntimeException ("couldn't deserialize object", e); 39 } 40 } 41 } 42 | Popular Tags |