org.vertexium.util.JavaSerializableUtils Maven / Gradle / Ivy
package org.vertexium.util;
import java.io.*;
public class JavaSerializableUtils {
public static byte[] objectToBytes(Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Object bytesToObject(byte[] bytes) {
try {
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
return ois.readObject();
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static T copy(T value) {
return (T) bytesToObject(objectToBytes(value));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy