source: branches/java-utils-alignment/src/main/java/de/ugoe/cs/util/SerializationUtils.java @ 2132

Last change on this file since 2132 was 1632, checked in by sherbold, 10 years ago
  • Property svn:mime-type set to text/plain
File size: 4.2 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util;
16
17import java.io.ByteArrayInputStream;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.ObjectInputStream;
21import java.io.ObjectStreamClass;
22import java.io.OutputStream;
23import java.io.Serializable;
24
25import org.apache.commons.lang.SerializationException;
26
27/**
28 * <p>
29 * This class is basically a partial copy of the the
30 * {@link org.apache.commons.lang.SerializationUtils}. The difference is that we use the class
31 * loader of the current context for the ObjectInputStream for deserialization. This allows the
32 * usage of this method in multi-classloader environments, such as application servers.
33 * </p>
34 *
35 * @author Steffen Herbold
36 */
37public class SerializationUtils {
38
39    /**
40     * @see org.apache.commons.lang.SerializationUtils#serialize(Serializable, OutputStream)
41     */
42    public static void serialize(final Serializable obj, final OutputStream outputStream) {
43        org.apache.commons.lang.SerializationUtils.serialize(obj, outputStream);
44    }
45
46    /**
47     * @see org.apache.commons.lang.SerializationUtils#serialize(Serializable)
48     */
49    public static byte[] serialize(final Serializable obj) {
50        return org.apache.commons.lang.SerializationUtils.serialize(obj);
51    }
52
53    /**
54     * @see org.apache.commons.lang.SerializationUtils#deserialize(InputStream)
55     */
56    public static <T> T deserialize(final InputStream inputStream) {
57        if (inputStream == null) {
58            throw new IllegalArgumentException("The InputStream must not be null");
59        }
60        ObjectInputStream in = null;
61        try {
62            // stream closed in the finally
63            in = new ObjectInputStream(inputStream) {
64                /*
65                 * (non-Javadoc)
66                 *
67                 * @see java.io.ObjectInputStream#resolveClass(java.io.ObjectStreamClass)
68                 */
69                @Override
70                protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException,
71                    ClassNotFoundException
72                {
73                    Class<?> clazz =
74                        Class.forName(desc.getName(), false, Thread.currentThread()
75                            .getContextClassLoader());
76
77                    if (clazz != null) {
78                        return clazz;
79                    }
80                    return super.resolveClass(desc);
81                }
82            };
83            @SuppressWarnings("unchecked")
84            // may fail with CCE if serialised form is incorrect
85            final T obj = (T) in.readObject();
86            return obj;
87
88        }
89        catch (final ClassCastException ex) {
90            throw new SerializationException(ex);
91        }
92        catch (final ClassNotFoundException ex) {
93            throw new SerializationException(ex);
94        }
95        catch (final IOException ex) {
96            throw new SerializationException(ex);
97        }
98        finally {
99            try {
100                if (in != null) {
101                    in.close();
102                }
103            }
104            catch (final IOException ex) { // NOPMD
105                // ignore close exception
106            }
107        }
108    }
109
110    /**
111     * @see org.apache.commons.lang.SerializationUtils#deserialize(byte[])
112     */
113    public static <T> T deserialize(final byte[] objectData) {
114        if (objectData == null) {
115            throw new IllegalArgumentException("The byte[] must not be null");
116        }
117        return SerializationUtils.<T> deserialize(new ByteArrayInputStream(objectData));
118    }
119
120}
Note: See TracBrowser for help on using the repository browser.