| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ObjectUtils |
|
| 5.5;5,5 |
| 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 | 0 | 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 | 1 | byte[] byteArray = null; |
| 52 | ||
| 53 | 1 | for (Object value : map.values()) |
| 54 | { | |
| 55 | 1 | if (! (value instanceof Serializable)) |
| 56 | { | |
| 57 | 0 | throw new Exception("Could not serialize, value is not serializable:" + value); |
| 58 | } | |
| 59 | 1 | } |
| 60 | ||
| 61 | 1 | ByteArrayOutputStream baos = null; |
| 62 | 1 | ObjectOutputStream out = null; |
| 63 | try | |
| 64 | { | |
| 65 | // These objects are closed in the finally. | |
| 66 | 1 | baos = new ByteArrayOutputStream(1024); |
| 67 | 1 | out = new ObjectOutputStream(baos); |
| 68 | ||
| 69 | 1 | out.writeObject(map); |
| 70 | 1 | out.flush(); |
| 71 | ||
| 72 | 1 | byteArray = baos.toByteArray(); |
| 73 | } | |
| 74 | finally | |
| 75 | { | |
| 76 | 1 | if (out != null) |
| 77 | { | |
| 78 | 1 | out.close(); |
| 79 | } | |
| 80 | 1 | if (baos != null) |
| 81 | { | |
| 82 | 1 | baos.close(); |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | 1 | 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 | 0 | T object = null; |
| 100 | ||
| 101 | 0 | if (objectData != null) |
| 102 | { | |
| 103 | // These streams are closed in finally. | |
| 104 | 0 | ObjectInputStream in = null; |
| 105 | 0 | ByteArrayInputStream bin = new ByteArrayInputStream(objectData); |
| 106 | ||
| 107 | try | |
| 108 | { | |
| 109 | 0 | in = new ObjectInputStream(bin); |
| 110 | ||
| 111 | // If objectData has not been initialized, an | |
| 112 | // exception will occur. | |
| 113 | 0 | object = (T)in.readObject(); |
| 114 | } | |
| 115 | 0 | catch (Exception e) |
| 116 | { | |
| 117 | // ignore | |
| 118 | } | |
| 119 | finally | |
| 120 | { | |
| 121 | 0 | try |
| 122 | { | |
| 123 | 0 | if (in != null) |
| 124 | { | |
| 125 | 0 | in.close(); |
| 126 | } | |
| 127 | 0 | bin.close(); |
| 128 | } | |
| 129 | 0 | catch (IOException e) |
| 130 | { | |
| 131 | // ignore | |
| 132 | 0 | } |
| 133 | 0 | } |
| 134 | } | |
| 135 | 0 | return object; |
| 136 | } | |
| 137 | } |