国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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

延伸 · 閱讀

精彩推薦
1273
主站蜘蛛池模板: 欧美极品视频 | 91在线第一页 | 一级国产 | 成人aaa视频 | 成人在线网站 | 欧美二三区 | 成年人在线看 | 国产目拍亚洲精品99久久精品 | 国产99精品视频 | 三级视频网站 | 日韩欧美中文字幕在线视频 | 亚洲二区在线观看 | 国产精品一区二区三区四区五区 | 免费观看视频毛片 | 99在线播放 | 亚洲日本国产 | 国产一区二区三区久久久久久久久 | 欧美成人一区二区 | 久久精品一区二区 | 一级毛片免费一级 | 天天操天天拍 | 91久久在线观看 | 免费电影av | 国产精品久久久久aaaa九色 | 久久91久久久久麻豆精品 | 一区二区国产精品 | 久久久久国产一级毛片高清片 | 日本一区二区高清视频 | 国产精品久久久久久久久图文区 | 国产成人久久精品一区二区三区 | 日本三级网 | 在线日韩欧美 | 成人精品一区二区 | 成人天堂666 | 国产精品亚洲精品 | 久久久久久久久久久影视 | 91精品啪aⅴ在线观看国产 | 欧美视频免费看 | 亚洲午夜av久久乱码 | 色日韩| 在线岛国av|