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.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022
023import org.springframework.core.serializer.DefaultDeserializer;
024import org.springframework.core.serializer.DefaultSerializer;
025import org.springframework.core.serializer.Deserializer;
026import org.springframework.core.serializer.Serializer;
027import org.springframework.util.Assert;
028
029/**
030 * A convenient delegate with pre-arranged configuration state for common
031 * serialization needs. Implements {@link Serializer} and {@link Deserializer}
032 * itself, so can also be passed into such more specific callback methods.
033 *
034 * @author Juergen Hoeller
035 * @since 4.3
036 */
037public class SerializationDelegate implements Serializer<Object>, Deserializer<Object> {
038
039        private final Serializer<Object> serializer;
040
041        private final Deserializer<Object> deserializer;
042
043
044        /**
045         * Create a {@code SerializationDelegate} with a default serializer/deserializer
046         * for the given {@code ClassLoader}.
047         * @see DefaultDeserializer
048         * @see DefaultDeserializer#DefaultDeserializer(ClassLoader)
049         */
050        public SerializationDelegate(ClassLoader classLoader) {
051                this.serializer = new DefaultSerializer();
052                this.deserializer = new DefaultDeserializer(classLoader);
053        }
054
055        /**
056         * Create a {@code SerializationDelegate} with the given serializer/deserializer.
057         * @param serializer the {@link Serializer} to use (never {@code null)}
058         * @param deserializer the {@link Deserializer} to use (never {@code null)}
059         */
060        public SerializationDelegate(Serializer<Object> serializer, Deserializer<Object> deserializer) {
061                Assert.notNull(serializer, "Serializer must not be null");
062                Assert.notNull(deserializer, "Deserializer must not be null");
063                this.serializer = serializer;
064                this.deserializer = deserializer;
065        }
066
067
068        @Override
069        public void serialize(Object object, OutputStream outputStream) throws IOException {
070                this.serializer.serialize(object, outputStream);
071        }
072
073        @Override
074        public Object deserialize(InputStream inputStream) throws IOException {
075                return this.deserializer.deserialize(inputStream);
076        }
077
078}