博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ProtoStuff 序列化例子
阅读量:2351 次
发布时间:2019-05-10

本文共 2953 字,大约阅读时间需要 9 分钟。

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.List;import com.dyuproject.protostuff.LinkedBuffer;import com.dyuproject.protostuff.ProtostuffIOUtil;import com.dyuproject.protostuff.Schema;import com.dyuproject.protostuff.runtime.RuntimeSchema;/** * 序列话工具 */public class ProtoStuffSerializerUtil {	/**	 * 序列化对象	 * @param obj	 * @return	 */	public static 
byte[] serialize(T obj) { if (obj == null) { throw new RuntimeException("序列化对象(" + obj + ")!"); } @SuppressWarnings("unchecked") Schema
schema = (Schema
) RuntimeSchema.getSchema(obj.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024); byte[] protostuff = null; try { protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new RuntimeException("序列化(" + obj.getClass() + ")对象(" + obj + ")发生异常!", e); } finally { buffer.clear(); } return protostuff; } /** * 反序列化对象 * @param paramArrayOfByte * @param targetClass * @return */ public static
T deserialize(byte[] paramArrayOfByte, Class
targetClass) { if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { throw new RuntimeException("反序列化对象发生异常,byte序列为空!"); } T instance = null; try { instance = targetClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("反序列化过程中依据类型创建对象失败!", e); } Schema
schema = RuntimeSchema.getSchema(targetClass); ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema); return instance; } /** * 序列化列表 * @param objList * @return */ public static
byte[] serializeList(List
objList) { if (objList == null || objList.isEmpty()) { throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!"); } @SuppressWarnings("unchecked") Schema
schema = (Schema
) RuntimeSchema.getSchema(objList.get(0).getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024); byte[] protostuff = null; ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(); ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer); protostuff = bos.toByteArray(); } catch (Exception e) { throw new RuntimeException("序列化对象列表(" + objList + ")发生异常!", e); } finally { buffer.clear(); try { if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return protostuff; } /** * 反序列化列表 * @param paramArrayOfByte * @param targetClass * @return */ public static
List
deserializeList(byte[] paramArrayOfByte, Class
targetClass) { if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { throw new RuntimeException("反序列化对象发生异常,byte序列为空!"); } Schema
schema = RuntimeSchema.getSchema(targetClass); List
result = null; try { result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema); } catch (IOException e) { throw new RuntimeException("反序列化对象列表发生异常!", e); } return result; }}

 

转载地址:http://nrevb.baihongyu.com/

你可能感兴趣的文章
哈希表(散列表)介绍
查看>>
Win7系统中bat 后缀文件关联程序恢复
查看>>
SMTP邮件传输协议发送邮件和附件
查看>>
libcurl开源库在VS2010环境下编译配置详解
查看>>
使用libcurl库实现SMTP发送邮件
查看>>
基于 libcurl 的通用网络传输库的实现
查看>>
软件设计中的Ping Pong 操作
查看>>
cURL开源库中的发送邮件示例代码(smtp-multi.c)
查看>>
结合MIME C++ library与CURL发送带附件的邮件
查看>>
CMake初步(1)
查看>>
CMake生成VS2013项目失败的解决办法
查看>>
什么是RST包,什么是三次握手,什么是四次握手
查看>>
几种TCP连接中出现RST的情况
查看>>
HTTP的长连接和短连接
查看>>
TCP,IP,HTTP,SOCKET区别和联系
查看>>
域名解析过程,很好的一张图收藏了
查看>>
整理时下流行的浏览器User-Agent大全
查看>>
IAAS、SAAS 和 PAAS 的区别、理解
查看>>
RichEdit对ole 对象的相关支持总结
查看>>
(分享)win10下双显示屏独立设置不同缩放率的方法
查看>>