001/*
002 * Copyright 2002-2019 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.util;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024
025import org.springframework.lang.Nullable;
026
027/**
028 * Static utilities for serialization and deserialization.
029 *
030 * @author Dave Syer
031 * @since 3.0.5
032 */
033public abstract class SerializationUtils {
034
035        /**
036         * Serialize the given object to a byte array.
037         * @param object the object to serialize
038         * @return an array of bytes representing the object in a portable fashion
039         */
040        @Nullable
041        public static byte[] serialize(@Nullable Object object) {
042                if (object == null) {
043                        return null;
044                }
045                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
046                try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
047                        oos.writeObject(object);
048                        oos.flush();
049                }
050                catch (IOException ex) {
051                        throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
052                }
053                return baos.toByteArray();
054        }
055
056        /**
057         * Deserialize the byte array into an object.
058         * @param bytes a serialized object
059         * @return the result of deserializing the bytes
060         */
061        @Nullable
062        public static Object deserialize(@Nullable byte[] bytes) {
063                if (bytes == null) {
064                        return null;
065                }
066                try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
067                        return ois.readObject();
068                }
069                catch (IOException ex) {
070                        throw new IllegalArgumentException("Failed to deserialize object", ex);
071                }
072                catch (ClassNotFoundException ex) {
073                        throw new IllegalStateException("Failed to deserialize object type", ex);
074                }
075        }
076
077}