1 25 26 package org.objectweb.easybeans.util.marshalling; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.IOException ; 31 import java.io.ObjectOutputStream ; 32 import java.io.Serializable ; 33 34 40 public final class Serialization { 41 42 45 private Serialization() { 46 47 } 48 49 50 56 public static byte[] storeObject(final Serializable object) throws IOException { 57 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 58 ObjectOutputStream oos = null; 59 try { 60 oos = new ObjectOutputStream (baos); 61 oos.writeObject(object); 62 oos.flush(); 63 return baos.toByteArray(); 64 } finally { 65 if (oos != null) { 66 oos.close(); 67 } 68 if (baos != null) { 69 baos.close(); 70 } 71 } 72 } 73 74 75 76 83 public static Object loadObject(final byte[] bytes) 84 throws IOException , ClassNotFoundException { 85 if (bytes == null) { 87 return null; 88 } 89 90 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 91 CtxClassLoaderObjectInputStream ctxClassLoaderObjectInputStream = new CtxClassLoaderObjectInputStream(bais); 92 try { 93 return ctxClassLoaderObjectInputStream.readObject(); 94 } finally { 95 if (ctxClassLoaderObjectInputStream != null) { 96 ctxClassLoaderObjectInputStream.close(); 97 } 98 if (ctxClassLoaderObjectInputStream != null) { 99 bais.close(); 100 } 101 102 } 103 104 } 105 106 107 108 109 110 111 } 112 | Popular Tags |