| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>XmlSerializer.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.io</a> > <span class="el_source">XmlSerializer.java</span></div><h1>XmlSerializer.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.io; |
| |
| import java.beans.XMLDecoder; |
| import java.beans.XMLEncoder; |
| import java.io.BufferedInputStream; |
| import java.io.BufferedOutputStream; |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| |
| /** |
| * Serializer implementation that uses the JavaBeans |
| * {@link java.beans.XMLEncoder XMLEncoder} and {@link java.beans.XMLDecoder XMLDecoder} to serialize |
| * and deserialize, respectively. |
| * <p/> |
| * <b>NOTE:</b> The JavaBeans XMLEncoder/XMLDecoder only successfully encode/decode objects when they are |
| * JavaBeans compatible! |
| * |
| * @since 0.9 |
| */ |
| <span class="nc" id="L38">public class XmlSerializer implements Serializer {</span> |
| |
| /** |
| * Serializes the specified <code>source</code> into a byte[] array by using the |
| * {@link java.beans.XMLEncoder XMLEncoder} to encode the object out to a |
| * {@link java.io.ByteArrayOutputStream ByteArrayOutputStream}, where the resulting byte[] array is returned. |
| * @param source the Object to convert into a byte[] array. |
| * @return the byte[] array representation of the XML encoded output. |
| */ |
| public byte[] serialize(Object source) { |
| <span class="nc bnc" id="L48" title="All 2 branches missed."> if (source == null) {</span> |
| <span class="nc" id="L49"> String msg = "argument cannot be null.";</span> |
| <span class="nc" id="L50"> throw new IllegalArgumentException(msg);</span> |
| } |
| |
| <span class="nc" id="L53"> ByteArrayOutputStream bos = new ByteArrayOutputStream();</span> |
| <span class="nc" id="L54"> XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(bos));</span> |
| <span class="nc" id="L55"> encoder.writeObject(source);</span> |
| <span class="nc" id="L56"> encoder.close();</span> |
| |
| <span class="nc" id="L58"> return bos.toByteArray();</span> |
| } |
| |
| /** |
| * Deserializes the specified <code>serialized</code> source back into an Object by using a |
| * {@link java.io.ByteArrayInputStream ByteArrayInputStream} to wrap the argument and then decode this |
| * stream via an {@link java.beans.XMLDecoder XMLDecoder}, where the |
| * {@link java.beans.XMLDecoder#readObject() readObject} call results in the original Object to return. |
| * @param serialized the byte[] array representation of the XML encoded output. |
| * @return the original source Object in reconstituted form. |
| */ |
| public Object deserialize(byte[] serialized) { |
| <span class="nc bnc" id="L70" title="All 2 branches missed."> if (serialized == null) {</span> |
| <span class="nc" id="L71"> throw new IllegalArgumentException("Argument cannot be null.");</span> |
| } |
| <span class="nc" id="L73"> ByteArrayInputStream bis = new ByteArrayInputStream(serialized);</span> |
| <span class="nc" id="L74"> XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(bis));</span> |
| <span class="nc" id="L75"> Object o = decoder.readObject();</span> |
| <span class="nc" id="L76"> decoder.close();</span> |
| <span class="nc" id="L77"> return o;</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |