1 package org.apache.turbine.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.Serializable;
28 import java.util.Map;
29
30 /**
31 * This is where common Object manipulation routines should go.
32 *
33 * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
34 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
35 * @version $Id: ObjectUtils.java 1812628 2017-10-19 12:34:25Z gk $
36 */
37 public abstract class ObjectUtils
38 {
39 /**
40 * Converts a map to a byte array for storage/serialization.
41 *
42 * @param map The Map to convert.
43 *
44 * @return A byte[] with the converted Map.
45 *
46 * @throws Exception A generic exception.
47 */
48 public static byte[] serializeMap(Map<String, Object> map)
49 throws Exception
50 {
51 byte[] byteArray = null;
52
53 for (Object value : map.values())
54 {
55 if (! (value instanceof Serializable))
56 {
57 throw new Exception("Could not serialize, value is not serializable:" + value);
58 }
59 }
60
61 ByteArrayOutputStream baos = null;
62 ObjectOutputStream out = null;
63 try
64 {
65 // These objects are closed in the finally.
66 baos = new ByteArrayOutputStream(1024);
67 out = new ObjectOutputStream(baos);
68
69 out.writeObject(map);
70 out.flush();
71
72 byteArray = baos.toByteArray();
73 }
74 finally
75 {
76 if (out != null)
77 {
78 out.close();
79 }
80 if (baos != null)
81 {
82 baos.close();
83 }
84 }
85
86 return byteArray;
87 }
88
89 /**
90 * Deserializes a single object from an array of bytes.
91 *
92 * @param objectData The serialized object.
93 *
94 * @return The deserialized object, or <code>null</code> on failure.
95 */
96 @SuppressWarnings("unchecked")
97 public static <T> T deserialize(byte[] objectData)
98 {
99 T object = null;
100
101 if (objectData != null)
102 {
103 // These streams are closed in finally.
104 ObjectInputStream in = null;
105 ByteArrayInputStream bin = new ByteArrayInputStream(objectData);
106
107 try
108 {
109 in = new ObjectInputStream(bin);
110
111 // If objectData has not been initialized, an
112 // exception will occur.
113 object = (T)in.readObject();
114 }
115 catch (Exception e)
116 {
117 // ignore
118 }
119 finally
120 {
121 try
122 {
123 if (in != null)
124 {
125 in.close();
126 }
127 bin.close();
128 }
129 catch (IOException e)
130 {
131 // ignore
132 }
133 }
134 }
135 return object;
136 }
137 }