CloneHelper.javaPackage: org.projectforge.framework.utils
Classes: CloneHelper
Imports: 1 packages
package org.projectforge.framework.utils;
import java.io.*;
/**
* For cloning a object including all fields (recursive).
*
* @author Kai Reinhard (k.reinhard@micromata.de)
*
*/
public class CloneHelper
{
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CloneHelper.class);
/**
* Serialized the given object as ByteArray and deserializes it.
* @param origin
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T cloneBySerialization(final T origin)
{
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
final ObjectOutputStream
oos = new ObjectOutputStream(bos);
oos.writeObject(origin);
oos.flush();
oos.close();
bos.close();
} catch (final IOException ex) {
log.error("Exception encountered while cloning given object '" + origin + "': " + ex, ex);
return null;
}
final byte[] byteData = bos.toByteArray();
final ByteArrayInputStream bais = new ByteArrayInputStream(byteData);
T object;
try {
object = (T) new ObjectInputStream(bais).readObject();
return object;
} catch (final ClassNotFoundException | IOException ex) {
log.error("Exception encountered while cloning given object '" + origin + "': " + ex, ex);
return null;
}
}
}
868d6abb7 2025 -> 2026 63081666f Source file headers: 2024-> 2025. 5f9bbfbd3 Fix typos in projectforge-business directory b6092df09 Copyright 2023 -> 2024 ab45d51fa Copyright 2001-2022 -> 2001-2023.