001    /**
002     * Copyright (c) 2000-2012 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.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    }