001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.serializer;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022
023/**
024 * A strategy interface for converting from data in an InputStream to an Object.
025 *
026 * @author Gary Russell
027 * @author Mark Fisher
028 * @author Juergen Hoeller
029 * @since 3.0.5
030 * @param <T> the object type
031 * @see Serializer
032 */
033@FunctionalInterface
034public interface Deserializer<T> {
035
036        /**
037         * Read (assemble) an object of type T from the given InputStream.
038         * <p>Note: Implementations should not close the given InputStream
039         * (or any decorators of that InputStream) but rather leave this up
040         * to the caller.
041         * @param inputStream the input stream
042         * @return the deserialized object
043         * @throws IOException in case of errors reading from the stream
044         */
045        T deserialize(InputStream inputStream) throws IOException;
046
047        /**
048         * Read (assemble) an object of type T from the given byte array.
049         * @param serialized the byte array
050         * @return the deserialized object
051         * @throws IOException in case of deserialization failure
052         * @since 5.2.7
053         */
054        default T deserializeFromByteArray(byte[] serialized) throws IOException {
055                return deserialize(new ByteArrayInputStream(serialized));
056        }
057
058}