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