您的位置:首页 > 其它

对象序列化。

2016-05-21 23:24 253 查看
1、利用jdk自带的 (ObjectOutPutStream.writeObject ,ObjectInputStream.readObject())

public static byte[] ObjectToByte(java.lang.Object obj) {

byte[] bytes = null;

try {

// object to bytearray

ByteArrayOutputStream bo = new ByteArrayOutputStream();

ObjectOutputStream oo = new ObjectOutputStream(bo);

oo.writeObject(obj);

bytes = bo.toByteArray();

bo.close();

oo.close();

} catch (Exception e) {

System.out.println("translation" + e.getMessage());

e.printStackTrace();

}

return bytes;

}

public static Object ByteToObject(byte[] bytes) {

Object obj = null;

try {

// bytearray to object

ByteArrayInputStream bi = new ByteArrayInputStream(bytes);

ObjectInputStream oi = new ObjectInputStream(bi);

obj = oi.readObject();

bi.close();

oi.close();

} catch (Exception e) {

System.out.println("translation" + e.getMessage());

e.printStackTrace();

}

return obj;

}

二:自定义对象的序列化与反序列化

public class ByteArraySerialize
{

private static long START_TIME = 1264953600000L;// 2010.01.01 00:00:00
private static int CAPACITY = 1024;
private static byte VERSION_2 = 2;// 版本号 orderId
private ThreadLocal<ByteBuffer> local = new ThreadLocal<ByteBuffer>() {
protected ByteBuffer initialValue() {
return ByteBuffer.allocate(CAPACITY);
}
};

public byte[] serialize(PromiseResult promise) {
if (promise == null)
return new byte[0];
ByteBuffer buf = local.get();
buf.position(0);
buf.put(VERSION_2);
buf.putLong(promise.getOrderId());

buf.putInt(promise.isShow() ? 1 : 0);

if (promise.getPromiseDate() != null) {
buf.putInt((int) ((promise.getPromiseDate().getTime() - START_TIME) / 1000));
} else {
buf.putInt(-1);
}

if (promise.getOrderEffectDate() != null) {

buf.putInt((int) ((promise.getOrderEffectDate().getTime() - START_TIME) / 1000));
} else {
buf.putInt(-1);
}

if (promise.getOrderTime() != null) {
buf.putInt((int) ((promise.getOrderTime().getTime() - START_TIME) / 1000));
} else {
buf.putInt(-1);
}

if (promise.getResultType() != null) {
buf.putInt(promise.getResultType());
} else {
buf.putInt(-1);
}
// deal with the message
int length = 0;
byte[] by = null;
final String message = promise.getMessage();
if (message != null && !StringUtils.equals("", message)) {
try {
by = message.getBytes("UTF-8");
length = by.length;
buf.putInt(length);
buf.put(by);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
buf.putInt(-1);
}
byte[] result = new byte[buf.position()];
buf.position(0);
buf.get(result, 0, result.length);
return result;
}

public PromiseResult deserialize(byte[] bytes) {
if (bytes.length == 0) {
return null;
}

PromiseResult promise = new PromiseResult();

ByteBuffer buf = local.get();
buf.position(0);
buf.put(bytes);
buf.position(0);
byte version = buf.get();
Long orderId = buf.getLong();
promise.setOrderId(orderId);

int isshow = buf.getInt();
promise.setShow(isshow ==  1 ?  true : false);

int time = buf.getInt();
if (time != -1) {
promise.setPromiseDate(new Date(time * 1000L + START_TIME));
} else {
promise.setPromiseDate(null);
}

time = buf.getInt();
if (time != -1) {
promise.setOrderEffectDate(new Date(time * 1000L + START_TIME));
} else {
promise.setOrderEffectDate(null);
}

time = buf.getInt();
if (time != -1) {
promise.setOrderTime(new Date(time * 1000L + START_TIME));
} else {
promise.setOrderTime(null);
}

int resultType = buf.getInt();
if (resultType != -1) {
promise.setResultType(resultType);
}

// deal with the message
int length = buf.getInt();
if(length != -1) {
byte[] dst = new byte[length];
buf.get(dst);
try {
promise.setMessage(new String(dst, "UTF-8"));
} catch (UnsupportedEncodingException e) {
}
} else {
promise.setMessage(null);
}

return promise;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: