对象序列化统一管理,升级注意,需更新此文件
This commit is contained in:
@@ -7,7 +7,7 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@@ -17,14 +17,14 @@ import org.springframework.beans.BeanUtils;
|
|||||||
import org.springframework.core.NamedThreadLocal;
|
import org.springframework.core.NamedThreadLocal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对象操作工具类, 继承org.apache.commons.lang3.ObjectUtils类
|
* 对象操作工具类,继承 org.apache.commons.lang3.ObjectUtils 类
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
* @version 2018-08-11
|
* @version 2020-3-29
|
||||||
*/
|
*/
|
||||||
public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Double类型
|
* 转换为 Double 类型
|
||||||
*/
|
*/
|
||||||
public static Double toDouble(final Object val) {
|
public static Double toDouble(final Object val) {
|
||||||
if (val == null) {
|
if (val == null) {
|
||||||
@@ -38,28 +38,29 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Float类型
|
* 转换为 Float 类型
|
||||||
*/
|
*/
|
||||||
public static Float toFloat(final Object val) {
|
public static Float toFloat(final Object val) {
|
||||||
return toDouble(val).floatValue();
|
return toDouble(val).floatValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Long类型
|
* 转换为 Long 类型
|
||||||
*/
|
*/
|
||||||
public static Long toLong(final Object val) {
|
public static Long toLong(final Object val) {
|
||||||
return toDouble(val).longValue();
|
return toDouble(val).longValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Integer类型
|
* 转换为 Integer 类型
|
||||||
*/
|
*/
|
||||||
public static Integer toInteger(final Object val) {
|
public static Integer toInteger(final Object val) {
|
||||||
return toLong(val).intValue();
|
return toLong(val).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Boolean类型 'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will return true. Otherwise, false is returned.
|
* 转换为 Boolean 类型 'true', 'on', 'y', 't', 'yes' or '1'
|
||||||
|
* (case insensitive) will return true. Otherwise, false is returned.
|
||||||
*/
|
*/
|
||||||
public static Boolean toBoolean(final Object val) {
|
public static Boolean toBoolean(final Object val) {
|
||||||
if (val == null) {
|
if (val == null) {
|
||||||
@@ -70,43 +71,35 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为字符串
|
* 转换为字符串
|
||||||
* @param obj
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object obj) {
|
public static String toString(final Object obj) {
|
||||||
return toString(obj, StringUtils.EMPTY);
|
return toString(obj, StringUtils.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果对象为空,则使用defaultVal值
|
* 转换为字符串,如果对象为空,则使用 defaultVal 值
|
||||||
* @param obj
|
|
||||||
* @param defaultVal
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object obj, final String defaultVal) {
|
public static String toString(final Object obj, final String defaultVal) {
|
||||||
return obj == null ? defaultVal : obj.toString();
|
return obj == null ? defaultVal : obj.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 空转空字符串("" to "" ; null to "" ; "null" to "" ; "NULL" to "" ; "Null" to "")
|
* 转换为字符串,忽略空值。如 null 字符串,则被认为空值,如下:
|
||||||
* @param val 需转换的值
|
* "" to "" ; null to "" ; "null" to "" ; "NULL" to "" ; "Null" to ""
|
||||||
* @return 返回转换后的值
|
|
||||||
*/
|
*/
|
||||||
public static String toStringIgnoreNull(final Object val) {
|
public static String toStringIgnoreNull(final Object val) {
|
||||||
return ObjectUtils.toStringIgnoreNull(val, StringUtils.EMPTY);
|
return ObjectUtils.toStringIgnoreNull(val, StringUtils.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 空对象转空字符串 ("" to defaultVal ; null to defaultVal ; "null" to defaultVal ; "NULL" to defaultVal ; "Null" to defaultVal)
|
* 转换为字符串,忽略空值。如 null 字符串,则被认为空值,如下:
|
||||||
* @param val 需转换的值
|
* "" to defaultVal ; null to defaultVal ; "null" to defaultVal ; "NULL" to defaultVal ; "Null" to defaultVal
|
||||||
* @param defaultVal 默认值
|
|
||||||
* @return 返回转换后的值
|
|
||||||
*/
|
*/
|
||||||
public static String toStringIgnoreNull(final Object val, String defaultVal) {
|
public static String toStringIgnoreNull(final Object val, String defaultVal) {
|
||||||
String str = ObjectUtils.toString(val);
|
String str = ObjectUtils.toString(val);
|
||||||
return !"".equals(str) && !"null".equals(str.trim().toLowerCase()) ? str : defaultVal;
|
return !"".equals(str) && !"null".equals(str.trim().toLowerCase()) ? str : defaultVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拷贝一个对象(但是子对象无法拷贝)
|
* 拷贝一个对象(但是子对象无法拷贝)
|
||||||
* @param source
|
* @param source
|
||||||
@@ -117,40 +110,26 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Object target = source.getClass().newInstance();
|
Object target = source.getClass().getDeclaredConstructor().newInstance();
|
||||||
BeanUtils.copyProperties(source, target, ignoreProperties);
|
BeanUtils.copyProperties(source, target, ignoreProperties);
|
||||||
return target;
|
return target;
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||||
|
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||||
throw ExceptionUtils.unchecked(e);
|
throw ExceptionUtils.unchecked(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注解到对象复制,只复制能匹配上的方法。 硕正组件用。
|
* 克隆一个对象(完全拷贝)
|
||||||
* @param annotation
|
* @param source
|
||||||
* @param object
|
|
||||||
*/
|
*/
|
||||||
public static void annotationToObject(Object annotation, Object object) {
|
public static Object cloneBean(Object source){
|
||||||
if (annotation != null && object != null) {
|
if (source == null){
|
||||||
Class<?> annotationClass = annotation.getClass();
|
return null;
|
||||||
Class<?> objectClass = object.getClass();
|
|
||||||
for (Method m : objectClass.getMethods()) {
|
|
||||||
if (StringUtils.startsWith(m.getName(), "set")) {
|
|
||||||
try {
|
|
||||||
String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
|
|
||||||
Object obj = annotationClass.getMethod(s).invoke(annotation);
|
|
||||||
if (obj != null && !"".equals(obj.toString())) {
|
|
||||||
// if (object == null){
|
|
||||||
// object = objectClass.newInstance();
|
|
||||||
// }
|
|
||||||
m.invoke(object, obj);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 忽略所有设置失败方法
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
byte[] bytes = ObjectUtils.serialize(source);
|
||||||
|
Object target = ObjectUtils.unserialize(bytes);
|
||||||
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,6 +138,24 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static byte[] serialize(Object object) {
|
public static byte[] serialize(Object object) {
|
||||||
|
return ObjectUtils.serializeFst(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反序列化对象
|
||||||
|
* @param bytes
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Object unserialize(byte[] bytes) {
|
||||||
|
return ObjectUtils.unserializeFst(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列化对象
|
||||||
|
* @param object
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static byte[] serializeJava(Object object) {
|
||||||
if (object == null){
|
if (object == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -183,7 +180,7 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
* @param bytes
|
* @param bytes
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Object unserialize(byte[] bytes) {
|
public static Object unserializeJava(byte[] bytes) {
|
||||||
if (bytes == null){
|
if (bytes == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -204,8 +201,8 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FST序列化配置对象
|
private static ThreadLocal<FSTConfiguration> fstConfiguration =
|
||||||
private static ThreadLocal<FSTConfiguration> fst = new NamedThreadLocal<FSTConfiguration>("FSTConfiguration") {
|
new NamedThreadLocal<FSTConfiguration>("FSTConfiguration") {
|
||||||
public FSTConfiguration initialValue() {
|
public FSTConfiguration initialValue() {
|
||||||
return FSTConfiguration.createDefaultConfiguration();
|
return FSTConfiguration.createDefaultConfiguration();
|
||||||
}
|
}
|
||||||
@@ -221,7 +218,7 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
long beginTime = System.currentTimeMillis();
|
long beginTime = System.currentTimeMillis();
|
||||||
byte[] bytes = fst.get().asByteArray(object);
|
byte[] bytes = fstConfiguration.get().asByteArray(object);
|
||||||
long totalTime = System.currentTimeMillis() - beginTime;
|
long totalTime = System.currentTimeMillis() - beginTime;
|
||||||
if (totalTime > 3000){
|
if (totalTime > 3000){
|
||||||
System.out.println("Fst serialize time: " + TimeUtils.formatDateAgo(totalTime));
|
System.out.println("Fst serialize time: " + TimeUtils.formatDateAgo(totalTime));
|
||||||
@@ -239,25 +236,66 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
long beginTime = System.currentTimeMillis();
|
long beginTime = System.currentTimeMillis();
|
||||||
Object object = fst.get().asObject(bytes);
|
Object object = fstConfiguration.get().asObject(bytes);
|
||||||
long totalTime = System.currentTimeMillis() - beginTime;
|
long totalTime = System.currentTimeMillis() - beginTime;
|
||||||
if (totalTime > 3000){
|
if (totalTime > 3000){
|
||||||
System.out.println("Fst unserialize time: " + TimeUtils.formatDateAgo(totalTime));
|
System.out.println("Fst unserialize time: " + TimeUtils.formatDateAgo(totalTime));
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// private static Pool<Kryo> kryoPool = new Pool<Kryo>(true, false, 8) {
|
||||||
* 克隆一个对象(完全拷贝)
|
// protected Kryo create() {
|
||||||
* @param source
|
// Kryo kryo = new Kryo();
|
||||||
*/
|
// kryo.setRegistrationRequired(false);
|
||||||
public static Object cloneBean(Object source){
|
// kryo.setReferences(false);
|
||||||
if (source == null){
|
// return kryo;
|
||||||
return null;
|
// }
|
||||||
}
|
// };
|
||||||
byte[] bytes = ObjectUtils.serializeFst(source);
|
// private static Pool<Output> outputPool = new Pool<Output>(true, false, 16) {
|
||||||
Object target = ObjectUtils.unserializeFst(bytes);
|
// protected Output create() {
|
||||||
return target;
|
// return new Output(1024, -1);
|
||||||
}
|
// }
|
||||||
|
// };
|
||||||
|
// private static Pool<Input> inputPool = new Pool<Input>(true, false, 16) {
|
||||||
|
// protected Input create() {
|
||||||
|
// return new Input(1024);
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * Kryo 序列化对象
|
||||||
|
// * @param object
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static byte[] serializeKryo(Object object) {
|
||||||
|
// Kryo kryo = kryoPool.obtain();
|
||||||
|
// Output output = outputPool.obtain();
|
||||||
|
// try {
|
||||||
|
// output.reset();
|
||||||
|
// kryo.writeClassAndObject(output, object);
|
||||||
|
// return output.toBytes();
|
||||||
|
// } finally {
|
||||||
|
// kryoPool.free(kryo);
|
||||||
|
// outputPool.free(output);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * Kryo 反序列化对象
|
||||||
|
// * @param bytes
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static Object unserializeKryo(byte[] bytes) {
|
||||||
|
// Kryo kryo = kryoPool.obtain();
|
||||||
|
// Input input = inputPool.obtain();
|
||||||
|
// try {
|
||||||
|
// input.setBuffer(bytes);
|
||||||
|
// return kryo.readClassAndObject(input);
|
||||||
|
// } finally {
|
||||||
|
// kryoPool.free(kryo);
|
||||||
|
// inputPool.free(input);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user