Example usage for org.apache.shiro.io SerializationException SerializationException

List of usage examples for org.apache.shiro.io SerializationException SerializationException

Introduction

In this page you can find the example usage for org.apache.shiro.io SerializationException SerializationException.

Prototype

public SerializationException(String message, Throwable cause) 

Source Link

Document

Constructs a new SerializationException.

Usage

From source file:com.github.zhangkaitao.shiro.chapter7.cache.SerializationUtils.java

License:Apache License

/**
 * <p>Deep clone an {@code Object} using serialization.</p>
 *
 * <p>This is many times slower than writing clone methods by hand
 * on all objects in your object graph. However, for complex object
 * graphs, or for those that don't support deep cloning this can
 * be a simple alternative implementation. Of course all the objects
 * must be {@code Serializable}.</p>
 *
 * @param <T> the type of the object involved
 * @param object  the {@code Serializable} object to clone
 * @return the cloned object/* w w w. ja  v  a  2s . co  m*/
 * @throws SerializationException (runtime) if the serialization fails
 */
public static <T extends Serializable> T clone(T object) {
    if (object == null) {
        return null;
    }
    byte[] objectData = serialize(object);
    ByteArrayInputStream bais = new ByteArrayInputStream(objectData);

    ClassLoaderAwareObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ClassLoaderAwareObjectInputStream(bais, object.getClass().getClassLoader());
        /*
         * when we serialize and deserialize an object,
         * it is reasonable to assume the deserialized object
         * is of the same type as the original serialized object
         */
        return (T) in.readObject();

    } catch (ClassNotFoundException ex) {
        throw new SerializationException("ClassNotFoundException while reading cloned object data", ex);
    } catch (IOException ex) {
        throw new SerializationException("IOException while reading cloned object data", ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            throw new SerializationException("IOException on closing cloned object data InputStream.", ex);
        }
    }
}