1 2 12 package com.versant.core.jdo.externalizer; 13 14 import com.versant.core.common.BindingSupportImpl; 15 16 import javax.jdo.PersistenceManager; 17 import java.io.*; 18 19 24 public class SerializedExternalizer implements Externalizer, Serializable { 25 26 public static final String SHORT_NAME = "SERIALIZED"; 27 28 public Object toExternalForm(PersistenceManager pm, Object o) { 29 if (o == null) return null; 30 try { 31 ByteArrayOutputStream bo = new ByteArrayOutputStream(256); 32 ObjectOutputStream os = new ObjectOutputStream(bo); 33 os.writeObject(o); 34 os.close(); 35 return bo.toByteArray(); 36 } catch (IOException e) { 37 throw BindingSupportImpl.getInstance().fatal(e.toString(), e); 38 } 39 } 40 41 public Object fromExternalForm(PersistenceManager pm, Object o) { 42 if (o == null) return o; 43 try { 44 ByteArrayInputStream bi = new ByteArrayInputStream((byte[])o); 45 ObjectInputStream oi = new ObjectInputStream(bi); 46 Object ans = oi.readObject(); 47 oi.close(); 48 return ans; 49 } catch (Exception e) { 50 throw BindingSupportImpl.getInstance().fatal(e.toString(), e); 51 } 52 } 53 54 public Class getExternalType() { 55 return byte[].class; 56 } 57 } 58 | Popular Tags |