java oracle序列化_java序列化和反序列化

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));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值