View Javadoc
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.lang.io;
20  
21  import java.io.BufferedInputStream;
22  import java.io.BufferedOutputStream;
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  
29  /**
30   * Serializer implementation that uses the default JVM serialization mechanism (Object Input/Output Streams).
31   *
32   * @param <T> the type of target.
33   * @since 0.9
34   */
35  public class DefaultSerializer<T> implements Serializer<T> {
36  
37      /**
38       * This implementation serializes the Object by using an {@link ObjectOutputStream} backed by a
39       * {@link ByteArrayOutputStream}.  The {@code ByteArrayOutputStream}'s backing byte array is returned.
40       *
41       * @param o the Object to convert into a byte[] array.
42       * @return the bytes representing the serialized object using standard JVM serialization.
43       * @throws SerializationException wrapping a {@link IOException} if something goes wrong with the streams.
44       */
45      public byte[] serialize(T o) throws SerializationException {
46          if (o == null) {
47              String msg = "argument cannot be null.";
48              throw new IllegalArgumentException(msg);
49          }
50          ByteArrayOutputStream baos = new ByteArrayOutputStream();
51          BufferedOutputStream bos = new BufferedOutputStream(baos);
52  
53          try {
54              ObjectOutputStream oos = new ObjectOutputStream(bos);
55              oos.writeObject(o);
56              oos.close();
57              baos.flush();
58              return baos.toByteArray();
59          } catch (IOException e) {
60              String msg = "Unable to serialize object [" + o + "].  "
61                      + "In order for the DefaultSerializer to serialize this object, "
62                      + " the [" + o.getClass().getName() + "] class must implement java.io.Serializable.";
63              throw new SerializationException(msg, e);
64          }
65      }
66  
67      /**
68       * This implementation deserializes the byte array using a {@link ObjectInputStream} using a source
69       * {@link ByteArrayInputStream} constructed with the argument byte array.
70       *
71       * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call.
72       * @return the deserialized/reconstituted object based on the given byte array
73       * @throws SerializationException if anything goes wrong using the streams.
74       */
75      public T deserialize(byte[] serialized) throws SerializationException {
76          if (serialized == null) {
77              String msg = "argument cannot be null.";
78              throw new IllegalArgumentException(msg);
79          }
80          ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
81          BufferedInputStream bis = new BufferedInputStream(bais);
82          try {
83              ObjectInputStream ois = new ClassResolvingObjectInputStream(bis);
84              @SuppressWarnings({"unchecked"})
85              T deserialized = (T) ois.readObject();
86              ois.close();
87              return deserialized;
88          } catch (Exception e) {
89              String msg = "Unable to deserialize argument byte array.";
90              throw new SerializationException(msg, e);
91          }
92      }
93  }