001    /**
002     * Copyright (c) 2000-2011 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.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019    import com.liferay.portal.kernel.util.StreamUtil;
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     */
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 byte[] serialize(Object object) {
056                    ObjectOutputStream objectOutputStream = null;
057    
058                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
059                            new UnsyncByteArrayOutputStream();
060    
061                    try {
062                            objectOutputStream = new ObjectOutputStream(
063                                    unsyncByteArrayOutputStream);
064    
065                            objectOutputStream.writeObject(object);
066                    }
067                    catch (IOException e) {
068                            throw new RuntimeException(e);
069                    }
070                    finally {
071                            StreamUtil.cleanUp(objectOutputStream);
072                    }
073    
074                    return unsyncByteArrayOutputStream.toByteArray();
075            }
076    
077    }