| SerializableUtil.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 */
14
15 package com.liferay.portal.kernel.util;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
19
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23
24 /**
25 * <a href="SerializableUtil.java.html"><b><i>View Source</i></b></a>
26 *
27 * @author Alexander Chow
28 */
29 public class SerializableUtil {
30
31 public static Object deserialize(byte[] bytes)
32 throws ClassNotFoundException, IOException {
33
34 ObjectInputStream ois = null;
35
36 try {
37 ois = new ObjectInputStream(new UnsyncByteArrayInputStream(bytes));
38
39 Object obj = ois.readObject();
40
41 ois.close();
42
43 ois = null;
44
45 return obj;
46 }
47 finally {
48 if (ois != null) {
49 ois.close();
50 }
51 }
52 }
53
54 public static byte[] serialize(Object obj) throws IOException {
55 ObjectOutputStream oos = null;
56
57 try {
58 UnsyncByteArrayOutputStream ubaos =
59 new UnsyncByteArrayOutputStream();
60
61 oos = new ObjectOutputStream(ubaos);
62
63 oos.writeObject(obj);
64
65 oos.close();
66
67 oos = null;
68
69 return ubaos.toByteArray();
70 }
71 finally {
72 if (oos != null) {
73 oos.close();
74 }
75 }
76 }
77
78 }