ProtoStuff序列化工具

来源:互联网 发布:java ringbuffer 编辑:程序博客网 时间:2024/04/30 01:24

1. 序列化传输

项目中http通信离不开对象的序列化和反序列化,通过序列化技术,可以跨语言实现数据的传输,例如把一个对象序列化后的二进制数据、xml格式数据存放在文件中,下次通过读取文件,然后反序列化一下即可重新生成该对象,抑或通过网络把序列化后的数据传输到另一个终端,对方通过反序列化后也可以重新复制出一个大概相同的对象出来。

在一般项目中,xml是一个不错的选择,例如微信公众平台的大多数接口,就是使用xml技术来序列化传输的,学习成本低,可读性高,方便调试,可以直接在浏览器查看结果等等都是他的优点,对于对速度要求不高的系统来说,的确是一种不错的选择。但如果系统对序列化效率要求很高,那么可能就得考虑换成其他技术了,例如ProtoStuff,它可以直接对普通的javabean进行序列化、反序列化的操作,而效率上甚至比protobuf还快。

2. 使用教程

public class ProtoStuffUtil {    //序列化普通对象    public static <T> byte[] serialize(T obj) {        if (obj == null) {            throw new RuntimeException("序列化对象为null");        }        @SuppressWarnings("unchecked")        //获得模式        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());        //缓存        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);        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[] data, Class<T> targetClass) {        if (data == null || data.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(data, 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(LinkedBuffer.DEFAULT_BUFFER_SIZE);        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;    }}
0 0
原创粉丝点击