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.portal.kernel.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    
021    import java.io.IOException;
022    import java.io.ObjectInputStream;
023    import java.io.ObjectOutputStream;
024    
025    /**
026     * @author     Alexander Chow
027     * @author     Igor Spasic
028     * @deprecated As of 6.1.0, moved to {@link com.liferay.util.SerializableUtil}
029     */
030    public class SerializableUtil {
031    
032            public static Object clone(Object object) {
033                    Class<?> clazz = object.getClass();
034    
035                    return deserialize(serialize(object), clazz.getClassLoader());
036            }
037    
038            public static Object deserialize(byte[] bytes) {
039                    ObjectInputStream objectInputStream = null;
040    
041                    try {
042                            objectInputStream = new ProtectedObjectInputStream(
043                                    new UnsyncByteArrayInputStream(bytes));
044    
045                            return objectInputStream.readObject();
046                    }
047                    catch (ClassNotFoundException cnfe) {
048                            throw new RuntimeException(cnfe);
049                    }
050                    catch (IOException ioe) {
051                            throw new RuntimeException(ioe);
052                    }
053                    finally {
054                            StreamUtil.cleanUp(objectInputStream);
055                    }
056            }
057    
058            public static Object deserialize(byte[] bytes, ClassLoader classLoader) {
059                    ObjectInputStream objectInputStream = null;
060    
061                    try {
062                            objectInputStream = new ProtectedClassLoaderObjectInputStream(
063                                    new UnsyncByteArrayInputStream(bytes), classLoader);
064    
065                            return objectInputStream.readObject();
066                    }
067                    catch (ClassNotFoundException cnfe) {
068                            throw new RuntimeException(cnfe);
069                    }
070                    catch (IOException ioe) {
071                            throw new RuntimeException(ioe);
072                    }
073                    finally {
074                            StreamUtil.cleanUp(objectInputStream);
075                    }
076            }
077    
078            public static byte[] serialize(Object object) {
079                    ObjectOutputStream objectOutputStream = null;
080    
081                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
082                            new UnsyncByteArrayOutputStream();
083    
084                    try {
085                            objectOutputStream = new ObjectOutputStream(
086                                    unsyncByteArrayOutputStream);
087    
088                            objectOutputStream.writeObject(object);
089                    }
090                    catch (IOException ioe) {
091                            throw new RuntimeException(ioe);
092                    }
093                    finally {
094                            StreamUtil.cleanUp(objectOutputStream);
095                    }
096    
097                    return unsyncByteArrayOutputStream.toByteArray();
098            }
099    
100    }