1 /* 2 * $Id$ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com 5 * 6 * The software in this package is published under the terms of the CPAL v1.0 7 * license, a copy of which has been included with this distribution in the 8 * LICENSE.txt file. 9 */ 10 11 package org.mule.util; 12 13 import java.io.ByteArrayInputStream; 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.io.ObjectInputStream; 17 18 import org.apache.commons.io.input.ClassLoaderObjectInputStream; 19 import org.apache.commons.lang.SerializationException; 20 21 public class SerializationUtils extends org.apache.commons.lang.SerializationUtils 22 { 23 24 /** 25 * <p>Deserializes an <code>Object</code> from the specified stream.</p> 26 * <p/> 27 * <p>The stream will be closed once the object is written. This 28 * avoids the need for a finally clause, and maybe also exception 29 * handling, in the application code.</p> 30 * <p/> 31 * <p>The stream passed in is not buffered internally within this method. 32 * This is the responsibility of your application if desired.</p> 33 * 34 * @param inputStream the serialized object input stream, must not be null 35 * @param cl classloader which can load custom classes from the stream 36 * @return the deserialized object 37 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code> 38 * @throws org.apache.commons.lang.SerializationException 39 * (runtime) if the serialization fails 40 */ 41 public static Object deserialize(InputStream inputStream, ClassLoader cl) 42 { 43 if (inputStream == null) 44 { 45 throw new IllegalArgumentException("The InputStream must not be null"); 46 } 47 if (cl == null) 48 { 49 throw new IllegalArgumentException("The ClassLoader must not be null"); 50 } 51 ObjectInputStream in = null; 52 try 53 { 54 // stream closed in the finally 55 in = new ClassLoaderObjectInputStream(cl, inputStream); 56 return in.readObject(); 57 58 } 59 catch (ClassNotFoundException ex) 60 { 61 throw new SerializationException(ex); 62 } 63 catch (IOException ex) 64 { 65 throw new SerializationException(ex); 66 } 67 finally 68 { 69 try 70 { 71 if (in != null) 72 { 73 in.close(); 74 } 75 } 76 catch (IOException ex) 77 { 78 // ignore close exception 79 } 80 } 81 } 82 83 /** 84 * <p>Deserializes a single <code>Object</code> from an array of bytes.</p> 85 * 86 * @param objectData the serialized object, must not be null 87 * @param cl classloader which can load custom classes from the stream 88 * @return the deserialized object 89 * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code> 90 * @throws SerializationException (runtime) if the serialization fails 91 */ 92 public static Object deserialize(byte[] objectData, ClassLoader cl) 93 { 94 if (objectData == null) 95 { 96 throw new IllegalArgumentException("The byte[] must not be null"); 97 } 98 ByteArrayInputStream bais = new ByteArrayInputStream(objectData); 99 return deserialize(bais, cl); 100 } 101 }