001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.io.ProtectedObjectInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
020    import com.liferay.portal.kernel.util.ProtectedClassLoaderObjectInputStream;
021    import com.liferay.portal.kernel.util.StreamUtil;
022    
023    import java.io.IOException;
024    import java.io.ObjectInputStream;
025    import java.io.ObjectOutputStream;
026    
027    /**
028     * @author Alexander Chow
029     * @author Igor Spasic
030     */
031    public class SerializableUtil {
032    
033            public static Object clone(Object object) {
034                    Class<?> clazz = object.getClass();
035    
036                    return deserialize(serialize(object), clazz.getClassLoader());
037            }
038    
039            public static Object deserialize(byte[] bytes) {
040                    ObjectInputStream objectInputStream = null;
041    
042                    try {
043                            objectInputStream = new ProtectedObjectInputStream(
044                                    new UnsyncByteArrayInputStream(bytes));
045    
046                            return objectInputStream.readObject();
047                    }
048                    catch (ClassNotFoundException cnfe) {
049                            throw new RuntimeException(cnfe);
050                    }
051                    catch (IOException ioe) {
052                            throw new RuntimeException(ioe);
053                    }
054                    finally {
055                            StreamUtil.cleanUp(objectInputStream);
056                    }
057            }
058    
059            public static Object deserialize(byte[] bytes, ClassLoader classLoader) {
060                    ObjectInputStream objectInputStream = null;
061    
062                    try {
063                            objectInputStream = new ProtectedClassLoaderObjectInputStream(
064                                    new UnsyncByteArrayInputStream(bytes), classLoader);
065    
066                            return objectInputStream.readObject();
067                    }
068                    catch (ClassNotFoundException cnfe) {
069                            throw new RuntimeException(cnfe);
070                    }
071                    catch (IOException ioe) {
072                            throw new RuntimeException(ioe);
073                    }
074                    finally {
075                            StreamUtil.cleanUp(objectInputStream);
076                    }
077            }
078    
079            public static byte[] serialize(Object object) {
080                    ObjectOutputStream objectOutputStream = null;
081    
082                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
083                            new UnsyncByteArrayOutputStream();
084    
085                    try {
086                            objectOutputStream = new ObjectOutputStream(
087                                    unsyncByteArrayOutputStream);
088    
089                            objectOutputStream.writeObject(object);
090                    }
091                    catch (IOException ioe) {
092                            throw new RuntimeException(ioe);
093                    }
094                    finally {
095                            StreamUtil.cleanUp(objectOutputStream);
096                    }
097    
098                    return unsyncByteArrayOutputStream.toByteArray();
099            }
100    
101    }