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.io;
20  
21  import java.beans.XMLDecoder;
22  import java.beans.XMLEncoder;
23  import java.io.BufferedInputStream;
24  import java.io.BufferedOutputStream;
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  
28  /**
29   * Serializer implementation that uses the JavaBeans
30   * {@link java.beans.XMLEncoder XMLEncoder} and {@link java.beans.XMLDecoder XMLDecoder} to serialize
31   * and deserialize, respectively.
32   * <p/>
33   * <b>NOTE:</b> The JavaBeans XMLEncoder/XMLDecoder only successfully encode/decode objects when they are
34   * JavaBeans compatible!
35   * 
36   * @since 0.9
37   */
38  public class XmlSerializer implements Serializer {
39  
40      /**
41       * Serializes the specified <code>source</code> into a byte[] array by using the
42       * {@link java.beans.XMLEncoder XMLEncoder} to encode the object out to a
43       * {@link java.io.ByteArrayOutputStream ByteArrayOutputStream}, where the resulting byte[] array is returned.
44       * @param source the Object to convert into a byte[] array.
45       * @return the byte[] array representation of the XML encoded output.
46       */
47      public byte[] serialize(Object source) {
48          if (source == null) {
49              String msg = "argument cannot be null.";
50              throw new IllegalArgumentException(msg);
51          }
52  
53          ByteArrayOutputStream bos = new ByteArrayOutputStream();
54          XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(bos));
55          encoder.writeObject(source);
56          encoder.close();
57  
58          return bos.toByteArray();
59      }
60  
61      /**
62       * Deserializes the specified <code>serialized</code> source back into an Object by using a
63       * {@link java.io.ByteArrayInputStream ByteArrayInputStream} to wrap the argument and then decode this
64       * stream via an {@link java.beans.XMLDecoder XMLDecoder}, where the
65       * {@link java.beans.XMLDecoder#readObject() readObject} call results in the original Object to return.
66       * @param serialized the byte[] array representation of the XML encoded output.
67       * @return the original source Object in reconstituted form.
68       */
69      public Object deserialize(byte[] serialized) {
70          if (serialized == null) {
71              throw new IllegalArgumentException("Argument cannot be null.");
72          }
73          ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
74          XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(bis));
75          Object o = decoder.readObject();
76          decoder.close();
77          return o;
78      }
79  }