激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Protostuff序列化和反序列化的使用說明

Protostuff序列化和反序列化的使用說明

2021-07-30 11:04zhglance Java教程

今天小編就為大家分享一篇關于Protostuff序列化和反序列化的使用說明,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

大家都知道protobuf好用,可是在網上找到的netty整合protobuf的文章都是千篇一律,自己編寫proto文件然后使用工具轉java文件用起來復雜麻煩,經過不懈努力終于找到了一個簡單的方法希望大家喜歡。

google原生的protobuffer使用起來相當麻煩,首先要寫.proto文件,然后編譯.proto文件,生成對應的.java文件,鄙人試了一次,發現真的很麻煩。而protostuff的官方網站(http://www.protostuff.io/documentation/runtime-schema/),對于智商比較低的小編來說也略顯生澀,于是鄙人就根據項目中用到的protostuff,撰寫此文,以方便自己和他人加深印象和學習。

1.實戰

1.maven依賴:

?
1
2
3
4
5
6
7
8
9
10
<dependency>
  <groupid>io.protostuff</groupid>
  <artifactid>protostuff-core</artifactid>
  <version>1.4.0</version>
</dependency>
<dependency>
  <groupid>io.protostuff</groupid>
  <artifactid>protostuff-runtime</artifactid>
  <version>1.4.0</version>
</dependency>

2.protobufutil工具類:protobufutil.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import io.protostuff.linkedbuffer;
import io.protostuff.protobufioutil;
import io.protostuff.protostuffioutil;
import io.protostuff.schema;
import io.protostuff.runtime.runtimeschema;
/**
 * created by zhangzh on 2017/2/20.
 */
public class protobufutil {
  public protobufutil() {
  }
  public static <t> byte[] serializer(t o) {
    schema schema = runtimeschema.getschema(o.getclass());
    return protobufioutil.tobytearray(o, schema, linkedbuffer.allocate(256));
  }
  public static <t> t deserializer(byte[] bytes, class<t> clazz) {
    t obj = null;
    try {
      obj = clazz.newinstance();
      schema schema = runtimeschema.getschema(obj.getclass());
      protostuffioutil.mergefrom(bytes, obj, schema);
    } catch (instantiationexception e) {
      e.printstacktrace();
    } catch (illegalaccessexception e) {
      e.printstacktrace();
    }
    return obj;
  }
}

3. bean類student.java:  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import io.protostuff.tag;
/**
 * created by zhangzh on 2017/2/20.
 */
public class student {
  @tag(1)
  private string name;
  @tag(2)
  private string studentno;
  @tag(3)
  private int age;
  @tag(4)
  private string schoolname;
  // 關于@tag,要么所有屬性都有@tag注解,要么都沒有,不能一個類中只有部分屬性有@tag注解
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public string getstudentno() {
    return studentno;
  }
  public void setstudentno(string studentno) {
    this.studentno = studentno;
  }
  public int getage() {
    return age;
  }
  public void setage(int age) {
    this.age = age;
  }
  public string getschoolname() {
    return schoolname;
  }
  public void setschoolname(string schoolname) {
    this.schoolname = schoolname;
  }
  @override
  public string tostring() {
    return "student{" +
        "name='" + name + '\'' +
        ", studentno='" + studentno + '\'' +
        ", age=" + age +
        ", schoolname='" + schoolname + '\'' +
        '}';
  }
}

3.test類protobufutiltest.java:  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.arrays;
/**
 * created by zhangzh on 2017/2/20.
 */
public class protobufutiltest {
  public static void main(string[] args) {
    student student = new student();
    student.setname("lance");
    student.setage(28);
    student.setstudentno("2011070122");
    student.setschoolname("bjut");
    byte[] serializerresult = protobufutil.serializer(student);
    system.out.println("serializer result:" + arrays.tostring(serializerresult));
    student deserializerresult = protobufutil.deserializer(serializerresult,student.class);
    system.out.println("deserializerresult:" + deserializerresult.tostring());
  }
}

4.輸出結果:  

serializer result:[10, 5, 108, 97, 110, 99, 101, 18, 10, 50, 48, 49, 49, 48, 55, 48, 49, 50, 50, 24, 28, 34, 4, 66, 74, 85, 84]
deserializerresult:student{name='lance', studentno='2011070122', age=28, schoolname='bjut'}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/zhglance/article/details/56017926

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄色网电影 | 91精品国产乱码久久久久久久久 | 爽爽视频免费看 | 国产一级桃视频播放 | 精品一区二区免费 | 国产正在播放 | 一区二区三区日韩视频在线观看 | 国产免费大片视频 | 国产精品av久久久久久久久久 | 91av大片| 精品国产精品久久 | 久久精品一级片 | 欧美中文字幕一区二区 | 欧美ab| 欧美另类综合 | 国内精品久久久久久久影视红豆 | 性高跟鞋xxxxhd4kvideos | 日本免费一区二区三区四区 | 少妇一级淫片免费放播放 | 在线看免电影网站 | 成人羞羞视频在线观看免费 | 成年人黄色片视频 | 亚洲天堂ww | 国内免费视频成人精品 | 日本精品网 | 青草伊人网 | 宅男噜噜噜66国产在线观看 | 欧美一级三级在线观看 | 亚洲人成中文字幕在线观看 | 精品二区在线观看 | h网站在线观看 | av视屏| 欧美成人精品不卡视频在线观看 | 2017亚洲男人天堂 | 国产91精品久久久 | 精品无吗乱吗av国产爱色 | 在线影院av | av影片在线观看 | 黄色av电影在线播放 | 特级黄色一级毛片 | 中文字幕观看 |