Coverage Report - org.apache.shiro.io.XmlSerializer
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlSerializer
0%
0/16
0%
0/4
3
 
 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  0
 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  0
         if (source == null) {
 49  0
             String msg = "argument cannot be null.";
 50  0
             throw new IllegalArgumentException(msg);
 51  
         }
 52  
 
 53  0
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 54  0
         XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(bos));
 55  0
         encoder.writeObject(source);
 56  0
         encoder.close();
 57  
 
 58  0
         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  0
         if (serialized == null) {
 71  0
             throw new IllegalArgumentException("Argument cannot be null.");
 72  
         }
 73  0
         ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
 74  0
         XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(bis));
 75  0
         Object o = decoder.readObject();
 76  0
         decoder.close();
 77  0
         return o;
 78  
     }
 79  
 }