wulm

protostuff是由谷歌开发的一个非常优秀的序列化反序列化工具

java代码如下:

  1 package com.common.utils;
  2 
  3 import io.protostuff.LinkedBuffer;
  4 import io.protostuff.ProtostuffIOUtil;
  5 import io.protostuff.Schema;
  6 import io.protostuff.runtime.RuntimeSchema;
  7 
  8 /**
  9  * 
 10  * @desc protobuf序列化反序列化工具
 11  * @author wulm
 12  */
 13 public class SerializationProtostuffUtil {
 14 
 15     /**
 16      * @desc protostuff 目前不支持直接序列化List等对象,需要使用普通的POJO包装一下
 17      * @author wulm
 18      */
 19     private static class SerializeData {
 20         private Object target;
 21 
 22         public Object getTarget() {
 23             return target;
 24         }
 25 
 26         public void setTarget(Object target) {
 27             this.target = target;
 28         }
 29     }
 30 
 31     private static final ThreadLocal<LinkedBuffer> BUFFER_THREAD_LOCAL = ThreadLocal
 32             .withInitial(() -> LinkedBuffer.allocate(512));
 33 
 34     @SuppressWarnings("unchecked")
 35     public static byte[] serialize(Object obj) {
 36         SerializeData data = new SerializeData();
 37         data.setTarget(obj);
 38 
 39         // this is lazily created and cached by RuntimeSchema
 40         // so its safe to call RuntimeSchema.getSchema(Foo.class) over and over
 41         // The getSchema method is also thread-safe
 42         Schema<SerializeData> schema = RuntimeSchema.getSchema((Class<SerializeData>) data.getClass());
 43 
 44         // Re-use (manage) this buffer to avoid allocating on every serialization
 45         // LinkedBuffer buffer = LinkedBuffer.allocate(512);
 46         LinkedBuffer buffer = BUFFER_THREAD_LOCAL.get();
 47 
 48         // ser
 49         try {
 50             return ProtostuffIOUtil.toByteArray(data, schema, buffer);
 51         } catch (Exception e) {
 52             throw new IllegalStateException(e.getMessage(), e);
 53         } finally {
 54             buffer.clear();
 55         }
 56 
 57     }
 58 
 59     @SuppressWarnings("unchecked")
 60     public static <T> T deserialize(byte[] data, Class<T> cls) {
 61         Schema<SerializeData> schema = RuntimeSchema.getSchema(SerializeData.class);
 62         // deser
 63         SerializeData message = schema.newMessage();
 64         ProtostuffIOUtil.mergeFrom(data, message, schema);
 65 
 66         return (T) message.getTarget();
 67     }
 68 
 69     // public static void main(String[] args) {
 70     //
 71     // Aaa aaa = new Aaa();
 72     // aaa.setA("你好呀");
 73     // aaa.setB("我是佩琪");
 74     // aaa.setC("你好");
 75     // aaa.setD("我是猪爸爸");
 76     //
 77     // byte[] serialize = SerializationProtostuffUtil.serialize(aaa);
 78     //
 79     // Aaa bb = SerializationProtostuffUtil.deserialize(serialize, Aaa.class);
 80     //
 81     // System.out.println(JacksonUtils.writeValueAsString(bb));
 82     //
 83     // }
 84     //
 85     // public static class Aaa {
 86     //
 87     // private String a;
 88     // private String b;
 89     // private String c;
 90     // private String d;
 91     //
 92     // public String getA() {
 93     // return a;
 94     // }
 95     //
 96     // public void setA(String a) {
 97     // this.a = a;
 98     // }
 99     //
100     // public String getB() {
101     // return b;
102     // }
103     //
104     // public void setB(String b) {
105     // this.b = b;
106     // }
107     //
108     // public String getC() {
109     // return c;
110     // }
111     //
112     // public void setC(String c) {
113     // this.c = c;
114     // }
115     //
116     // public String getD() {
117     // return d;
118     // }
119     //
120     // public void setD(String d) {
121     // this.d = d;
122     // }
123     //
124     // }
125 
126 }

 

参阅资料:

  https://github.com/protostuff/protostuff

  http://www.zhangleiup.com/post/112.htm

  http://www.cnblogs.com/wolf-bin/p/9269987.html

 

分类:

java

技术点:

相关文章: