Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SerializationUtils |
|
| 7.5;7.5 |
1 | /* | |
2 | * $Id: SerializationUtils.java 20320 2010-11-24 15:03:31Z dfeist $ | |
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 | 0 | 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 | 0 | if (inputStream == null) |
44 | { | |
45 | 0 | throw new IllegalArgumentException("The InputStream must not be null"); |
46 | } | |
47 | 0 | if (cl == null) |
48 | { | |
49 | 0 | throw new IllegalArgumentException("The ClassLoader must not be null"); |
50 | } | |
51 | 0 | ObjectInputStream in = null; |
52 | try | |
53 | { | |
54 | // stream closed in the finally | |
55 | 0 | in = new ClassLoaderObjectInputStream(cl, inputStream); |
56 | 0 | return in.readObject(); |
57 | ||
58 | } | |
59 | 0 | catch (ClassNotFoundException ex) |
60 | { | |
61 | 0 | throw new SerializationException(ex); |
62 | } | |
63 | 0 | catch (IOException ex) |
64 | { | |
65 | 0 | throw new SerializationException(ex); |
66 | } | |
67 | finally | |
68 | { | |
69 | 0 | try |
70 | { | |
71 | 0 | if (in != null) |
72 | { | |
73 | 0 | in.close(); |
74 | } | |
75 | } | |
76 | 0 | catch (IOException ex) |
77 | { | |
78 | // ignore close exception | |
79 | 0 | } |
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 | 0 | if (objectData == null) |
95 | { | |
96 | 0 | throw new IllegalArgumentException("The byte[] must not be null"); |
97 | } | |
98 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream(objectData); |
99 | 0 | return deserialize(bais, cl); |
100 | } | |
101 | } |