com.link_intersystems.util.Serialization Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lis-commons-util Show documentation
Show all versions of lis-commons-util Show documentation
Link Intersystems Commons Util (lis-commons-util) is collection of reusable software components
that extend the standard java.util components.
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));
}
}