1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
31 import java.io.Serializable;
32 import java.util.Hashtable;
33 import java.util.Map;
34
35
36
37
38
39
40
41
42 public abstract class ObjectUtils
43 {
44
45
46
47
48
49
50
51
52
53 public static byte[] serializeHashtable(Hashtable<String, Object> hash)
54 throws Exception
55 {
56 Hashtable<String, Serializable> saveData =
57 new Hashtable<String, Serializable>(hash.size());
58 String key = null;
59 Object value = null;
60 byte[] byteArray = null;
61
62 for (Map.Entry<String, Object> entry : hash.entrySet())
63 {
64 key = entry.getKey();
65 value = entry.getValue();
66 if (value instanceof Serializable)
67 {
68 saveData.put (key, (Serializable)value);
69 }
70 }
71
72 ByteArrayOutputStream baos = null;
73 BufferedOutputStream bos = null;
74 ObjectOutputStream out = null;
75 try
76 {
77
78 baos = new ByteArrayOutputStream();
79 bos = new BufferedOutputStream(baos);
80 out = new ObjectOutputStream(bos);
81
82 out.writeObject(saveData);
83 out.flush();
84 bos.flush();
85
86 byteArray = baos.toByteArray();
87 }
88 finally
89 {
90 if (out != null)
91 {
92 out.close();
93 }
94 if (bos != null)
95 {
96 bos.close();
97 }
98 if (baos != null)
99 {
100 baos.close();
101 }
102 }
103 return byteArray;
104 }
105
106
107
108
109
110
111
112
113 public static Object deserialize(byte[] objectData)
114 {
115 Object object = null;
116
117 if (objectData != null)
118 {
119
120 ObjectInputStream in = null;
121 ByteArrayInputStream bin = new ByteArrayInputStream(objectData);
122 BufferedInputStream bufin = new BufferedInputStream(bin);
123
124 try
125 {
126 in = new ObjectInputStream(bufin);
127
128
129
130 object = in.readObject();
131 }
132 catch (Exception e)
133 {
134
135 }
136 finally
137 {
138 try
139 {
140 if (in != null)
141 {
142 in.close();
143 }
144
145 bufin.close();
146 bin.close();
147 }
148 catch (IOException e)
149 {
150
151 }
152 }
153 }
154 return object;
155 }
156 }