
science.aist.jack.general.util.CopyUtils Maven / Gradle / Ivy
/*
* Copyright (c) 2020 the original author or authors.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package science.aist.jack.general.util;
import lombok.CustomLog;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Util class defining functions to create copies of other objects.
*
* @author Rainer Meindl
* @since 2.0
*/
@CustomLog
public final class CopyUtils {
private CopyUtils() {
}
/**
* This method makes a "deep copy" of any object it is given.
*
* @param object object to clone
* @param type of the object that should be copied
* @return deep copy of object
*/
@SuppressWarnings("unchecked")
public static T deepCopy(T object) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (T) ois.readObject();
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy