package a.b.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.List;
/**
* @author lys
* @since 2023/4/6 12:03
*/
@Slf4j
public class SerializationUtils {
/**
* 深拷贝
*
* @param src 原始数据
* @param <T> 泛型
* @return 深拷贝数据
*/
@SuppressWarnings("unchecked")
public static <T> List<T> deepCopy(List<T> src) {
ByteArrayOutputStream byteOut = null;
ObjectOutputStream out = null;
ByteArrayInputStream byteIn = null;
ObjectInputStream in = null;
try {
byteOut = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteOut);
out.writeObject(src);
byteIn = new ByteArrayInputStream(byteOut.toByteArray());
in = new ObjectInputStream(byteIn);
return (List<T>) in.readObject();
} catch (Exception ex) {
log.error("深拷贝异常", ex);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(byteIn);
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(byteOut);
}
return Collections.emptyList();
}
}
标签:java,byteOut,IOUtils,io,集合,import,拷贝
From: https://www.cnblogs.com/yslu/p/17292362.html