View Javadoc
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.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  import java.io.Serializable;
27  import java.util.Map;
28  
29  /**
30   * This is where common Object manipulation routines should go.
31   *
32   * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @version $Id: ObjectUtils.java 1850675 2019-01-07 18:48:42Z painter $
35   */
36  public abstract class ObjectUtils
37  {
38      /**
39       * Converts a map to a byte array for storage/serialization.
40       *
41       * @param map The Map to convert.
42       *
43       * @return A byte[] with the converted Map.
44       *
45       * @throws Exception A generic exception.
46       */
47  	public static byte[] serializeMap(Map<String, Object> map)
48              throws Exception
49      {
50          byte[] byteArray = null;
51  
52          for (Object value : map.values())
53          {
54              if (! (value instanceof Serializable))
55              {
56                  throw new Exception("Could not serialize, value is not serializable:" + value);
57              }
58          }
59  
60          try (ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
61               ObjectOutputStream out = new ObjectOutputStream(baos))
62          {
63              out.writeObject(map);
64              out.flush();
65  
66              byteArray = baos.toByteArray();
67          }
68  
69          return byteArray;
70      }
71  
72      /**
73       * Deserializes a single object from an array of bytes.
74       *
75       * @param <T> type of the object to return
76       * @param objectData The serialized object.
77       *
78       * @return The deserialized object, or <code>null</code> on failure.
79       */
80      @SuppressWarnings("unchecked")
81      public static <T> T deserialize(byte[] objectData)
82      {
83          T object = null;
84  
85          if (objectData != null)
86          {
87              try (ByteArrayInputStream bin = new ByteArrayInputStream(objectData);
88                   ObjectInputStream in = new ObjectInputStream(bin))
89              {
90                  // If objectData has not been initialized, an
91                  // exception will occur.
92                  object = (T)in.readObject();
93              }
94              catch (Exception e)
95              {
96                  // ignore
97              }
98          }
99  
100         return object;
101     }
102 }