001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.io;
020    
021    import java.io.*;
022    
023    /**
024     * Serializer implementation that uses the default JVM serialization mechanism (Object Input/Output Streams).
025     *
026     * @author Les Hazlewood
027     * @since 0.9
028     */
029    public class DefaultSerializer<T> implements Serializer<T> {
030    
031        /**
032         * This implementation serializes the Object by using an {@link ObjectOutputStream} backed by a
033         * {@link ByteArrayOutputStream}.  The {@code ByteArrayOutputStream}'s backing byte array is returned.
034         *
035         * @param o the Object to convert into a byte[] array.
036         * @return the bytes representing the serialized object using standard JVM serialization.
037         * @throws SerializationException wrapping a {@link IOException} if something goes wrong with the streams.
038         */
039        public byte[] serialize(T o) throws SerializationException {
040            if (o == null) {
041                String msg = "argument cannot be null.";
042                throw new IllegalArgumentException(msg);
043            }
044            ByteArrayOutputStream baos = new ByteArrayOutputStream();
045            BufferedOutputStream bos = new BufferedOutputStream(baos);
046    
047            try {
048                ObjectOutputStream oos = new ObjectOutputStream(bos);
049                oos.writeObject(o);
050                oos.close();
051                return baos.toByteArray();
052            } catch (IOException e) {
053                String msg = "Unable to serialize object [" + o + "].  " +
054                        "In order for the DefaultSerializer to serialize this object, the [" + o.getClass().getName() + "] " +
055                        "class must implement java.io.Serializable.";
056                throw new SerializationException(msg, e);
057            }
058        }
059    
060        /**
061         * This implementation deserializes the byte array using a {@link ObjectInputStream} using a source
062         * {@link ByteArrayInputStream} constructed with the argument byte array.
063         *
064         * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call.
065         * @return the deserialized/reconstituted object based on the given byte array
066         * @throws SerializationException if anything goes wrong using the streams.
067         */
068        public T deserialize(byte[] serialized) throws SerializationException {
069            if (serialized == null) {
070                String msg = "argument cannot be null.";
071                throw new IllegalArgumentException(msg);
072            }
073            ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
074            BufferedInputStream bis = new BufferedInputStream(bais);
075            try {
076                ObjectInputStream ois = new ObjectInputStream(bis);
077                @SuppressWarnings({"unchecked"})
078                T deserialized = (T) ois.readObject();
079                ois.close();
080                return deserialized;
081            } catch (Exception e) {
082                String msg = "Unable to deserialze argument byte array.";
083                throw new SerializationException(msg, e);
084            }
085        }
086    }