All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.link_intersystems.util.Serialization Maven / Gradle / Ivy

Go to download

There is a newer version: 1.9.7
Show newest version
package com.link_intersystems.util;

import java.io.*;

/**
 * @author René Link {@literal }
 */
public class Serialization {
    public static  byte[] serialize(T object) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(bout)) {
            objectOutputStream.writeObject(object);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return bout.toByteArray();
    }

    public static  T deserialize(byte[] serializedObject) {
        try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(serializedObject))) {
            return (T) objIn.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new SerializationException(e);
        }
    }

    public static  T clone(T serializable) {
        return deserialize(serialize(serializable));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy