对象序列化。
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 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; } }
相关文章推荐
- 无人机,diyDrones,3dr,pixhawk,Chris Anderson
- Android小记-WebView的回收销毁
- python 编码转换
- C#事件与委托详解【精华 多看看】
- 异常控制流
- Human-level concept learning翻译(1)
- teamviewer在mac系统上不能正常使用。
- centos 6.5 清除yum源方式
- Spring事务分析
- BaseAdapter实现不存储列表选项的 ListView
- 自适应波束形成算法 之 初识【一】(Adaptive Beamforming Algorithm)
- 用C/C++实现一个日期类,重载运算符=,==,+,-,++,--,>,>=,<,<=等
- 打开Android Studio时出现No JVM installaton found a 64-bit jdk
- 博弈(巴什博弈讲解及其应用)(hdu2149,hdu2188)
- Java虚拟机运行时内存区域
- LeetCode 206. Reverse Linked List
- vs2010 c++ CString to long
- leetcode -Remove Element
- 深入Java集合类
- 关于java中jsp和servlet传值的问题