001package org.apache.turbine.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.ByteArrayInputStream;
023import java.io.ByteArrayOutputStream;
024import java.io.IOException;
025import java.io.ObjectInputStream;
026import java.io.ObjectOutputStream;
027import java.io.Serializable;
028import java.util.Map;
029
030/**
031 * This is where common Object manipulation routines should go.
032 *
033 * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
034 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035 * @version $Id: ObjectUtils.java 1812628 2017-10-19 12:34:25Z gk $
036 */
037public abstract class ObjectUtils
038{
039    /**
040     * Converts a map to a byte array for storage/serialization.
041     *
042     * @param map The Map to convert.
043     *
044     * @return A byte[] with the converted Map.
045     *
046     * @throws Exception A generic exception.
047     */
048        public static byte[] serializeMap(Map<String, Object> map)
049            throws Exception
050    {
051        byte[] byteArray = null;
052
053        for (Object value : map.values())
054        {
055            if (! (value instanceof Serializable))
056            {
057                throw new Exception("Could not serialize, value is not serializable:" + value);
058            }
059        }
060
061        ByteArrayOutputStream baos = null;
062        ObjectOutputStream out = null;
063        try
064        {
065            // These objects are closed in the finally.
066            baos = new ByteArrayOutputStream(1024);
067            out  = new ObjectOutputStream(baos);
068
069            out.writeObject(map);
070            out.flush();
071
072            byteArray = baos.toByteArray();
073        }
074        finally
075        {
076            if (out != null)
077            {
078                out.close();
079            }
080            if (baos != null)
081            {
082                baos.close();
083            }
084        }
085
086        return byteArray;
087    }
088
089    /**
090     * Deserializes a single object from an array of bytes.
091     *
092     * @param objectData The serialized object.
093     *
094     * @return The deserialized object, or <code>null</code> on failure.
095     */
096    @SuppressWarnings("unchecked")
097    public static <T> T deserialize(byte[] objectData)
098    {
099        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}