美文网首页
Protostuff

Protostuff

作者: brightranger | 来源:发表于2017-05-25 15:57 被阅读332次

    使用Protostuff进行序列化和反序列化
    (官网:http://www.protostuff.io/)
    工具类如下:

    package com.gillion.cache.manager.utils;
    
    import io.protostuff.LinkedBuffer;
    import io.protostuff.ProtostuffIOUtil;
    import io.protostuff.Schema;
    import io.protostuff.runtime.RuntimeSchema;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    /**
     * Creator: zhanghf
     * Create Date: 2016/10/17
     * Create Time: 21:09
     * Gillion
     */
    public class ProtostuffUtil {
    
        private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
    
        private static <T> Schema<T> getSchema(Class<T> clazz) {
            Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
            if (schema == null) {
                schema = RuntimeSchema.getSchema(clazz);
                if (schema != null) {
                    cachedSchema.put(clazz, schema);
                }
            }
            return schema;
        }
    
        /**
         * 序列化
         *
         * @param obj 序列化对象
         * @return 序列化后的byte[]值
         */
        public static <T> byte[] serializer(T obj) {
            @SuppressWarnings("unchecked")
            Class<T> clazz = (Class<T>) obj.getClass();
            LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
            try {
                Schema<T> schema = getSchema(clazz);
                return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            } finally {
                buffer.clear();
            }
        }
    
        /**
         * 反序列化
         *
         * @param data 序列化后的byte[]值
         * @param clazz 反序列化后的对象
         * @return 返回的对象
         */
        public static <T> T deserializer(byte[] data, Class<T> clazz) {
            try {
                T obj = clazz.newInstance();
                Schema<T> schema = getSchema(clazz);
                ProtostuffIOUtil.mergeFrom(data, obj, schema);
                return obj;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
    
    
        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 ignore) {
    
                }
            }
    
            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;
            try {
                result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
            } catch (IOException e) {
                throw new RuntimeException("反序列化对象列表发生异常!",e);
            }
            return result;
        }
    
    
    }
    

    相关文章

      网友评论

          本文标题:Protostuff

          本文链接:https://www.haomeiwen.com/subject/skrzxxtx.html