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

freak.core.util.StreamCopy Maven / Gradle / Ivy

There is a newer version: 0.4.7
Show newest version
/*
 * 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