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