mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add DriverNamePool
This commit is contained in:
@@ -4,14 +4,8 @@ import cn.hutool.core.collection.IterUtil;
|
||||
import cn.hutool.core.comparator.CompareUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FastByteArrayOutputStream;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
@@ -427,24 +421,8 @@ public class ObjectUtil {
|
||||
* @return 克隆后的对象
|
||||
* @throws UtilException IO异常和ClassNotFoundException封装
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T cloneByStream(T obj) {
|
||||
if (false == (obj instanceof Serializable)) {
|
||||
return null;
|
||||
}
|
||||
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
|
||||
ObjectOutputStream out = null;
|
||||
try {
|
||||
out = new ObjectOutputStream(byteOut);
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
|
||||
return (T) in.readObject();
|
||||
} catch (Exception e) {
|
||||
throw new UtilException(e);
|
||||
} finally {
|
||||
IoUtil.close(out);
|
||||
}
|
||||
return SerializeUtil.clone(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -456,12 +434,7 @@ public class ObjectUtil {
|
||||
* @return 序列化后的字节码
|
||||
*/
|
||||
public static <T> byte[] serialize(T obj) {
|
||||
if (false == (obj instanceof Serializable)) {
|
||||
return null;
|
||||
}
|
||||
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
|
||||
IoUtil.writeObjects(byteOut, false, (Serializable) obj);
|
||||
return byteOut.toByteArray();
|
||||
return SerializeUtil.serialize(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -477,7 +450,7 @@ public class ObjectUtil {
|
||||
* @return 反序列化后的对象
|
||||
*/
|
||||
public static <T> T deserialize(byte[] bytes) {
|
||||
return IoUtil.readObj(new ByteArrayInputStream(bytes));
|
||||
return SerializeUtil.deserialize(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -612,7 +585,7 @@ public class ObjectUtil {
|
||||
* @param objs 被检查对象
|
||||
* @return 是否存在
|
||||
* @since 5.5.3
|
||||
* @see ArrayUtil#hasNull(Object[])
|
||||
* @see ArrayUtil#hasNull(Object[])
|
||||
*/
|
||||
public static boolean hasNull(Object... objs) {
|
||||
return ArrayUtil.hasNull(objs);
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FastByteArrayOutputStream;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 序列化工具类
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.6.3
|
||||
*/
|
||||
public class SerializeUtil {
|
||||
|
||||
/**
|
||||
* 序列化后拷贝流的方式克隆<br>
|
||||
* 对象必须实现Serializable接口
|
||||
*
|
||||
* @param <T> 对象类型
|
||||
* @param obj 被克隆对象
|
||||
* @return 克隆后的对象
|
||||
* @throws UtilException IO异常和ClassNotFoundException封装
|
||||
*/
|
||||
public static <T> T clone(T obj) {
|
||||
if (false == (obj instanceof Serializable)) {
|
||||
return null;
|
||||
}
|
||||
return deserialize(serialize(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化<br>
|
||||
* 对象必须实现Serializable接口
|
||||
*
|
||||
* @param <T> 对象类型
|
||||
* @param obj 要被序列化的对象
|
||||
* @return 序列化后的字节码
|
||||
*/
|
||||
public static <T> byte[] serialize(T obj) {
|
||||
if (false == (obj instanceof Serializable)) {
|
||||
return null;
|
||||
}
|
||||
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
|
||||
IoUtil.writeObjects(byteOut, false, (Serializable) obj);
|
||||
return byteOut.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化<br>
|
||||
* 对象必须实现Serializable接口
|
||||
*
|
||||
* <p>
|
||||
* 注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
|
||||
* </p>
|
||||
*
|
||||
* @param <T> 对象类型
|
||||
* @param bytes 反序列化的字节码
|
||||
* @return 反序列化后的对象
|
||||
*/
|
||||
public static <T> T deserialize(byte[] bytes) {
|
||||
return IoUtil.readObj(new ByteArrayInputStream(bytes));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user