importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.util.Arrays;importjava.util.List;importjava.util.Objects;importorg.junit.Test;importio.protostuff.LinkedBuffer;importio.protostuff.ProtostuffIOUtil;importio.protostuff.Schema;importio.protostuff.runtime.RuntimeSchema;/*** 序列化,反序列化工具
*
* @project iweixin-pay
* @fileName SerializeUtil.java
* @Description
*@authorlight-zhang
* @date 2018年5月31日上午9:51:25
*@version1.0.0*/
public classSerializeUtil {/*** 序列化
*
*@paramobject
*@return
*/
public static byte[] serialize(T object) {if(Objects.isNull(object)) {throw new RuntimeException("序列化对象(" + object + ")!");
}
@SuppressWarnings("unchecked")
Schema schema = (Schema) RuntimeSchema.getSchema(object.getClass());
LinkedBuffer buffer= LinkedBuffer.allocate(1024 * 1024);byte[] protostuff = null;try{
protostuff=ProtostuffIOUtil.toByteArray(object, schema, buffer);
}catch(Exception e) {throw new RuntimeException("序列化(" + object.getClass() + ")对象(" + object + ")发生异常!", e);
}finally{
buffer.clear();
}returnprotostuff;
}/*** 反序列化
*
*@paramparamArrayOfByte
*@paramtargetClass
*@return
*/
public static T unserialize(byte[] paramArrayOfByte, ClasstargetClass) {if (Objects.isNull(paramArrayOfByte) || 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);returninstance;
}/*** 序列化集合
*
*@paramobjList
*@return
*/
public static byte[] serializeList(ListobjList) {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= newByteArrayOutputStream();
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();
}
}returnprotostuff;
}public static List deserializeList(byte[] paramArrayOfByte, ClasstargetClass) {if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {throw new RuntimeException("反序列化对象发生异常,byte序列为空!");
}
Schema schema =RuntimeSchema.getSchema(targetClass);
List result = null;try{
result= ProtostuffIOUtil.parseListFrom(newByteArrayInputStream(paramArrayOfByte), schema);
}catch(IOException e) {throw new RuntimeException("反序列化对象列表发生异常!", e);
}returnresult;
}
@Testpublic voiddemo() {byte[] result = SerializeUtil.serialize("123");
System.out.println(Arrays.toString(result));
System.out.println(SerializeUtil.unserialize(result, String.class));
}
}