Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SerializationUtils |
|
| 2.6666666666666665;2.667 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.api.util.io; | |
17 | ||
18 | import org.apache.commons.codec.binary.Base64; | |
19 | import org.apache.commons.lang.SerializationException; | |
20 | import org.apache.commons.lang.StringUtils; | |
21 | ||
22 | import java.io.Serializable; | |
23 | ||
24 | /** | |
25 | * {@code SerializationUtils} is a set of utilities to add in the serialization | |
26 | * of java objects. | |
27 | * | |
28 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
29 | * | |
30 | */ | |
31 | public final class SerializationUtils { | |
32 | ||
33 | /** | |
34 | * Serializes the given {@link Serializable} object and then executes a | |
35 | * Base 64 encoding on it, returning the encoded value as a String. | |
36 | * | |
37 | * @param object the object to serialize, cannot be null | |
38 | * @return a base 64-encoded representation of the serialized object | |
39 | * | |
40 | * @throws IllegalArgumentException if the given object is null | |
41 | * @throws SerializationException if the serialization fails | |
42 | */ | |
43 | public static String serializeToBase64(Serializable object) { | |
44 | 0 | if (object == null) { |
45 | 0 | throw new IllegalArgumentException("Cannot serialize a null object"); |
46 | } | |
47 | 0 | byte[] serializedBytes = org.apache.commons.lang.SerializationUtils.serialize(object); |
48 | 0 | return new Base64().encodeAsString(serializedBytes); |
49 | } | |
50 | ||
51 | /** | |
52 | * Deserializes the given base 64-encoded string value to it's Serializable | |
53 | * object representation. | |
54 | * | |
55 | * @param base64Value the base 64-encoded value to deserialize, must not be null or a blank string | |
56 | * @return the deserialized object | |
57 | * | |
58 | * @throws IllegalArgumentException if the given value is is null or blank | |
59 | * @throws SerializationException if the deserialization fails | |
60 | */ | |
61 | public static Serializable deserializeFromBase64(String base64Value) { | |
62 | 0 | if (StringUtils.isBlank(base64Value)) { |
63 | 0 | throw new IllegalArgumentException("Cannot deserialize a null or blank base64 string value."); |
64 | } | |
65 | 0 | byte[] decoded = new Base64().decode(base64Value); |
66 | 0 | return (Serializable)org.apache.commons.lang.SerializationUtils.deserialize(decoded); |
67 | } | |
68 | ||
69 | 0 | private SerializationUtils() { |
70 | 0 | throw new UnsupportedOperationException("Should never be invoked."); |
71 | } | |
72 | ||
73 | } |