001
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
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 }