package cert;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @ClassName Serializer
* @Description TODO(序列化器)
*/
public final class Serializer
{
private static final Logger logger = LoggerFactory.getLogger(Serializer.class);
/**
* 序列化。
* @param object 需要序列化的对象。
* @return 字节数组。
*/
public static final byte[] serialize(Object object)
{
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try
{
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
return baos.toByteArray();
}
catch(Exception ex)
{
logger.error("序列化对象失败", ex);
}
finally
{
if(oos != null)
{
try {
oos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(baos != null)
{
try {
baos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 反序列化。
* @param bytes 字节数组。
* @return 反序列化后的对象。
*/
public static final Object unserialize(byte[] bytes)
{
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try
{
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
}
catch(Exception ex)
{
logger.error("反序列化对象失败", ex);
}
finally
{
if(ois != null)
{
try {
ois.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(bais != null)
{
try {
bais.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @ClassName Serializer
* @Description TODO(序列化器)
*/
public final class Serializer
{
private static final Logger logger = LoggerFactory.getLogger(Serializer.class);
/**
* 序列化。
* @param object 需要序列化的对象。
* @return 字节数组。
*/
public static final byte[] serialize(Object object)
{
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try
{
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
return baos.toByteArray();
}
catch(Exception ex)
{
logger.error("序列化对象失败", ex);
}
finally
{
if(oos != null)
{
try {
oos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(baos != null)
{
try {
baos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 反序列化。
* @param bytes 字节数组。
* @return 反序列化后的对象。
*/
public static final Object unserialize(byte[] bytes)
{
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try
{
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
}
catch(Exception ex)
{
logger.error("反序列化对象失败", ex);
}
finally
{
if(ois != null)
{
try {
ois.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(bais != null)
{
try {
bais.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}