001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    @Deprecated
031    public class SerializableUtil {
032    
033            public static Object clone(Object object) {
034                    return deserialize(serialize(object));
035            }
036    
037            public static Object deserialize(byte[] bytes) {
038                    ObjectInputStream objectInputStream = null;
039    
040                    try {
041                            objectInputStream = new ProtectedObjectInputStream(
042                                    new UnsyncByteArrayInputStream(bytes));
043    
044                            return objectInputStream.readObject();
045                    }
046                    catch (ClassNotFoundException cnfe) {
047                            throw new RuntimeException(cnfe);
048                    }
049                    catch (IOException ioe) {
050                            throw new RuntimeException(ioe);
051                    }
052                    finally {
053                            StreamUtil.cleanUp(objectInputStream);
054                    }
055            }
056    
057            public static Object deserialize(byte[] bytes, ClassLoader classLoader) {
058                    ObjectInputStream objectInputStream = null;
059    
060                    try {
061                            objectInputStream = new ProtectedClassLoaderObjectInputStream(
062                                    new UnsyncByteArrayInputStream(bytes), classLoader);
063    
064                            return objectInputStream.readObject();
065                    }
066                    catch (ClassNotFoundException cnfe) {
067                            throw new RuntimeException(cnfe);
068                    }
069                    catch (IOException ioe) {
070                            throw new RuntimeException(ioe);
071                    }
072                    finally {
073                            StreamUtil.cleanUp(objectInputStream);
074                    }
075            }
076    
077            public static byte[] serialize(Object object) {
078                    ObjectOutputStream objectOutputStream = null;
079    
080                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
081                            new UnsyncByteArrayOutputStream();
082    
083                    try {
084                            objectOutputStream = new ObjectOutputStream(
085                                    unsyncByteArrayOutputStream);
086    
087                            objectOutputStream.writeObject(object);
088                    }
089                    catch (IOException ioe) {
090                            throw new RuntimeException(ioe);
091                    }
092                    finally {
093                            StreamUtil.cleanUp(objectOutputStream);
094                    }
095    
096                    return unsyncByteArrayOutputStream.toByteArray();
097            }
098    
099    }