
freak.core.util.StreamCopy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of freak-core Show documentation
Show all versions of freak-core Show documentation
Core library of the Free Evolutionary Algorithm Toolkit
/*
* This file is part of FrEAK. For licensing and copyright information
* please see the file COPYING in the root directory of this
* distribution or contact .
*/
package freak.core.util;
import java.io.*;
/**
* @author Stefan
*/
public class StreamCopy {
/**
* Creates a deep copy of object by serializing and then deserializing it.
*
* @exception NotSerializableException if an object, reachable by a chain of references from the source, is not serializable.
*/
public static Serializable copy(Serializable source) throws NotSerializableException {
return read(serialize(source));
}
public static byte[] serialize(Serializable source) throws NotSerializableException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(source);
return baos.toByteArray();
} catch (NotSerializableException exc) {
throw exc;
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
public static Serializable read(byte[] serializedObject) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(serializedObject);
ObjectInputStream ois = new ObjectInputStream(bais);
return (Serializable)ois.readObject();
} catch (IOException exc) {
throw new RuntimeException(exc);
} catch (ClassNotFoundException exc) {
throw new RuntimeException(exc);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy