Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultSerializer |
|
| 6.0;6 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.shiro.io; | |
20 | ||
21 | import java.io.*; | |
22 | ||
23 | /** | |
24 | * Serializer implementation that uses the default JVM serialization mechanism (Object Input/Output Streams). | |
25 | * | |
26 | * @since 0.9 | |
27 | */ | |
28 | 2 | public class DefaultSerializer<T> implements Serializer<T> { |
29 | ||
30 | /** | |
31 | * This implementation serializes the Object by using an {@link ObjectOutputStream} backed by a | |
32 | * {@link ByteArrayOutputStream}. The {@code ByteArrayOutputStream}'s backing byte array is returned. | |
33 | * | |
34 | * @param o the Object to convert into a byte[] array. | |
35 | * @return the bytes representing the serialized object using standard JVM serialization. | |
36 | * @throws SerializationException wrapping a {@link IOException} if something goes wrong with the streams. | |
37 | */ | |
38 | public byte[] serialize(T o) throws SerializationException { | |
39 | 0 | if (o == null) { |
40 | 0 | String msg = "argument cannot be null."; |
41 | 0 | throw new IllegalArgumentException(msg); |
42 | } | |
43 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
44 | 0 | BufferedOutputStream bos = new BufferedOutputStream(baos); |
45 | ||
46 | try { | |
47 | 0 | ObjectOutputStream oos = new ObjectOutputStream(bos); |
48 | 0 | oos.writeObject(o); |
49 | 0 | oos.close(); |
50 | 0 | return baos.toByteArray(); |
51 | 0 | } catch (IOException e) { |
52 | 0 | String msg = "Unable to serialize object [" + o + "]. " + |
53 | "In order for the DefaultSerializer to serialize this object, the [" + o.getClass().getName() + "] " + | |
54 | "class must implement java.io.Serializable."; | |
55 | 0 | throw new SerializationException(msg, e); |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * This implementation deserializes the byte array using a {@link ObjectInputStream} using a source | |
61 | * {@link ByteArrayInputStream} constructed with the argument byte array. | |
62 | * | |
63 | * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call. | |
64 | * @return the deserialized/reconstituted object based on the given byte array | |
65 | * @throws SerializationException if anything goes wrong using the streams. | |
66 | */ | |
67 | public T deserialize(byte[] serialized) throws SerializationException { | |
68 | 0 | if (serialized == null) { |
69 | 0 | String msg = "argument cannot be null."; |
70 | 0 | throw new IllegalArgumentException(msg); |
71 | } | |
72 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream(serialized); |
73 | 0 | BufferedInputStream bis = new BufferedInputStream(bais); |
74 | try { | |
75 | 0 | ObjectInputStream ois = new ClassResolvingObjectInputStream(bis); |
76 | @SuppressWarnings({"unchecked"}) | |
77 | 0 | T deserialized = (T) ois.readObject(); |
78 | 0 | ois.close(); |
79 | 0 | return deserialized; |
80 | 0 | } catch (Exception e) { |
81 | 0 | String msg = "Unable to deserialze argument byte array."; |
82 | 0 | throw new SerializationException(msg, e); |
83 | } | |
84 | } | |
85 | } |