1 21 package oracle.toplink.essentials.internal.helper; 23 24 import oracle.toplink.essentials.exceptions.ValidationException; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.OutputStream ; 32 import java.io.Serializable ; 33 34 44 public class SerializationHelper { 45 46 52 public static Object clone(Serializable object) throws IOException , ClassNotFoundException { 53 return deserialize(serialize(object)); 54 } 55 56 63 public static void serialize(Serializable obj, OutputStream outputStream) throws IOException { 64 if (outputStream == null) { 65 throw ValidationException.invalidNullMethodArguments(); 66 } 67 ObjectOutputStream outStream = null; 68 69 try { 70 outStream = new ObjectOutputStream (outputStream); 72 outStream.writeObject(obj); 73 } finally { 74 try { 75 if (outStream != null) { 76 outStream.close(); 77 } 78 } catch (IOException ex) { 79 } 81 } 82 } 83 84 91 public static byte[] serialize(Serializable obj) throws IOException { 92 ByteArrayOutputStream outStream = new ByteArrayOutputStream (512); 93 serialize(obj, outStream); 94 return outStream.toByteArray(); 95 } 96 97 104 public static Object deserialize(InputStream inputStream) throws IOException , ClassNotFoundException { 105 if (inputStream == null) { 106 throw new IllegalArgumentException ("The inputStream argument cannot be null"); 107 } 108 ObjectInputStream inStream = null; 109 try { 110 inStream = new ObjectInputStream (inputStream); 112 return inStream.readObject(); 113 114 } finally { 115 try { 116 if (inStream != null) { 117 inStream.close(); 118 } 119 } catch (IOException ex) { 120 } 122 } 123 } 124 125 132 public static Object deserialize(byte[] objectBytes) throws IOException , ClassNotFoundException { 133 if (objectBytes == null) { 134 throw ValidationException.invalidNullMethodArguments(); 135 } 136 ByteArrayInputStream inStream = new ByteArrayInputStream (objectBytes); 137 return deserialize(inStream); 138 } 139 } 140 | Popular Tags |