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 /**
022 * A <code>Serializer</code> converts objects to raw binary data and vice versa, enabling persistent storage
023 * of objects to files, HTTP cookies, or other mechanism.
024 * <p/>
025 * A <code>Serializer</code> should only do conversion, never change the data, such as encoding/decoding or
026 * encryption. These orthogonal concerns are handled elsewhere by Shiro, for example, via
027 * {@link org.apache.shiro.codec.CodecSupport CodecSupport} and {@link org.apache.shiro.crypto.CipherService CipherService}s.
028 *
029 * @author Les Hazlewood
030 * @param <T> The type of the object being serialized and deserialized.
031 * @since 0.9
032 */
033 public interface Serializer<T> {
034
035 /**
036 * Converts the specified Object into a byte[] array. This byte[] array must be able to be reconstructed
037 * back into the original Object form via the {@link #deserialize(byte[]) deserialize} method.
038 *
039 * @param o the Object to convert into a byte[] array.
040 * @return a byte[] array representing the Object's state that can be restored later.
041 * @throws SerializationException if an error occurrs converting the Object into a byte[] array.
042 */
043 byte[] serialize(T o) throws SerializationException;
044
045 /**
046 * Converts the specified raw byte[] array back into an original Object form. This byte[] array is expected to
047 * be the output of a previous {@link #serialize(Object) serialize} method call.
048 *
049 * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call.
050 * @return the Object that was previously serialized into the raw byte[] array.
051 * @throws SerializationException if an error occurrs converting the raw byte[] array back into an Object.
052 */
053 T deserialize(byte[] serialized) throws SerializationException;
054 }