001/*
002 * Copyright 2002-2015 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.support;
018
019import java.io.ByteArrayInputStream;
020
021import org.springframework.core.convert.converter.Converter;
022import org.springframework.core.serializer.DefaultDeserializer;
023import org.springframework.core.serializer.Deserializer;
024import org.springframework.util.Assert;
025
026/**
027 * A {@link Converter} that delegates to a
028 * {@link org.springframework.core.serializer.Deserializer}
029 * to convert data in a byte array to an object.
030 *
031 * @author Gary Russell
032 * @author Mark Fisher
033 * @author Juergen Hoeller
034 * @since 3.0.5
035 */
036public class DeserializingConverter implements Converter<byte[], Object> {
037
038        private final Deserializer<Object> deserializer;
039
040
041        /**
042         * Create a {@code DeserializingConverter} with default {@link java.io.ObjectInputStream}
043         * configuration, using the "latest user-defined ClassLoader".
044         * @see DefaultDeserializer#DefaultDeserializer()
045         */
046        public DeserializingConverter() {
047                this.deserializer = new DefaultDeserializer();
048        }
049
050        /**
051         * Create a {@code DeserializingConverter} for using an {@link java.io.ObjectInputStream}
052         * with the given {@code ClassLoader}.
053         * @since 4.2.1
054         * @see DefaultDeserializer#DefaultDeserializer(ClassLoader)
055         */
056        public DeserializingConverter(ClassLoader classLoader) {
057                this.deserializer = new DefaultDeserializer(classLoader);
058        }
059
060        /**
061         * Create a {@code DeserializingConverter} that delegates to the provided {@link Deserializer}.
062         */
063        public DeserializingConverter(Deserializer<Object> deserializer) {
064                Assert.notNull(deserializer, "Deserializer must not be null");
065                this.deserializer = deserializer;
066        }
067
068
069        @Override
070        public Object convert(byte[] source) {
071                ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
072                try {
073                        return this.deserializer.deserialize(byteStream);
074                }
075                catch (Throwable ex) {
076                        throw new SerializationFailedException("Failed to deserialize payload. " +
077                                        "Is the byte array a result of corresponding serialization for " +
078                                        this.deserializer.getClass().getSimpleName() + "?", ex);
079                }
080        }
081
082}