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