parent
c925bd1685
commit
895ad80e2a
@ -0,0 +1,73 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuxueli 2020-04-12 0:14:00
|
||||||
|
*/
|
||||||
|
public class JdkSerializeTool {
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(JdkSerializeTool.class);
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------ serialize and unserialize ------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象-->byte[] (由于jedis中不支持直接存储object所以转换成byte[]存入)
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static byte[] serialize(Object object) {
|
||||||
|
ObjectOutputStream oos = null;
|
||||||
|
ByteArrayOutputStream baos = null;
|
||||||
|
try {
|
||||||
|
// 序列化
|
||||||
|
baos = new ByteArrayOutputStream();
|
||||||
|
oos = new ObjectOutputStream(baos);
|
||||||
|
oos.writeObject(object);
|
||||||
|
byte[] bytes = baos.toByteArray();
|
||||||
|
return bytes;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
oos.close();
|
||||||
|
baos.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将byte[] -->Object
|
||||||
|
*
|
||||||
|
* @param bytes
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> Object deserialize(byte[] bytes, Class<T> clazz) {
|
||||||
|
ByteArrayInputStream bais = null;
|
||||||
|
try {
|
||||||
|
// 反序列化
|
||||||
|
bais = new ByteArrayInputStream(bytes);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||||
|
return ois.readObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
bais.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuxueli 2018-10-20 20:07:26
|
||||||
|
*/
|
||||||
|
public class ThrowableUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse error to string
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String toString(Throwable e) {
|
||||||
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(stringWriter));
|
||||||
|
String errorMsg = stringWriter.toString();
|
||||||
|
return errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue