Protostuff序列化工具类

源代码

package org.wit.ff.util;

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; /**  *   * <pre>  * 序列号工具  * </pre>  *  * @author F.Fang  */ public class ProtoStuffSerializerUtil {  public static <T> byte[] serialize(T obj) {  if (obj == null) {  throw new RuntimeException("序列化对象(" + obj + ")!");  }  @SuppressWarnings("unchecked")  Schema<T> schema = (Schema<T>) 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;  }  public static <T> T deserialize(byte[] paramArrayOfByte, Class<T> 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<T> schema = RuntimeSchema.getSchema(targetClass);  ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema);  return instance;  }   public static <T> byte[] serializeList(List<T> objList) {  if (objList == null || objList.isEmpty()) {  throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!");  }  @SuppressWarnings("unchecked")  Schema<T> schema = (Schema<T>) 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;  }   public static <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) {  if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {  throw new RuntimeException("反序列化对象发生异常,byte序列为空!"); } Schema<T> schema = RuntimeSchema.getSchema(targetClass); List<T> result = null; try { result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema); } catch (IOException e) { throw new RuntimeException("反序列化对象列表发生异常!",e); } return result; } }

测试代码

public class ProtoStuffSerializerUtilTest { public static class Person{ int id; String name; public Person(){ } public Person(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } } @Test public void demo(){ Person p = new Person(1,"ff"); byte[] arr = ProtoStuffSerializerUtil.serialize(p); Person result = ProtoStuffSerializerUtil.deserialize(arr, Person.class); assertEquals(p.getId(), result.getId()); assertEquals(p.getName(), result.getName()); } }
一段旅程,远去所有昨天的昨天,如记事本翻开新的一页,永远的不漏痕迹的把记忆藏在一片洁白中。然后绚烂的开始新的故事。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值