日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

protobuf java_ProtoBuf for java使用笔记 | 学步园

發布時間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 protobuf java_ProtoBuf for java使用笔记 | 学步园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

三、新建項目ProtobufDemo。包名:com.protobufdemo.protobuf。

四、把上面的jar包跟exe放到工程目錄下。新建文件夾:proto。在其下新建文件:msg.proto,內容如下:

option java_package = "com.protobufdemo.protobuf";

option java_outer_classname = "PersonProbuf";

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

message CountryInfo {

required string name = 1;

required string code = 2;

optional int32 number = 3;

}

}

message AddressBook {

repeated Person person = 1;

}

五、生成 java文件:在proto.exe目錄下:protoc ?--java_out=./src ? ./proto/msg.proto

六、測試類:ProtobufDemo.java

package com.protobufdemo.protobuf;

import java.util.List;

import com.google.protobuf.InvalidProtocolBufferException;

import com.protobufdemo.protobuf.PersonProbuf.Person;

import com.protobufdemo.protobuf.PersonProbuf.Person.PhoneNumber;

public class ProtobufDemo {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

PersonProbuf.Person.Builder builder = PersonProbuf.Person.newBuilder();

builder.setEmail("Test@email.com");

builder.setId(1);

builder.setName("TestName");

builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder()

.setNumber("15120051111")

.setType(PersonProbuf.Person.PhoneType.MOBILE));

builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder()

.setNumber("18602991111")

.setType(PersonProbuf.Person.PhoneType.HOME));

Person person = builder.build();

int length =new String("Test@email.com"+"TestName"+"15120051111"+"18602991111").length();

byte[] buf = person.toByteArray();

try {

Person person2 = PersonProbuf.Person.parseFrom(buf);

System.out.println(person2.getName() + ", " + person2.getEmail());

List lstPhones = person2.getPhoneList();

for (PhoneNumber phoneNumber : lstPhones) {

System.out.println(phoneNumber.getNumber());

}

} catch (InvalidProtocolBufferException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(Converter.getHexString(buf, buf.length));

System.out.println("壓縮前長度:"+length);

System.out.println("壓縮后長度:"+buf.length);

}

}

七、輔助類:Converter.java

//------------------------------------------------------------------------------

package com.protobufdemo.protobuf;

//[------------------------------ MAIN CLASS ----------------------------------]

//--------------------------------- REVISIONS ----------------------------------

//Date Name Tracking # Description

//-------- ------------------- ------------- --------------------------

//13SEP2011 James Shen Initial Creation

/**

* Convert help class.

*


* ? Copyright 2011 Guidebee, Inc. All Rights Reserved.

*

* @version 1.00, 13/09/11

* @author Guidebee Pty Ltd.

*/

public class Converter {

// Hex help

private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',

(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',

(byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',

(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

// //

// --------------------------------- REVISIONS

// ------------------------------

// Date Name Tracking # Description

// --------- ------------------- ------------- ----------------------

// 13SEP2011 James Shen Initial Creation

// //

/**

* convert a byte arrary to hex string

*

* @param raw

* byte arrary

* @param len

* lenght of the arrary.

* @return hex string.

*/

public static String getHexString(byte[] raw, int len) {

byte[] hex = new byte[2 * len];

int index = 0;

int pos = 0;

for (byte b : raw) {

if (pos >= len)

break;

pos++;

int v = b & 0xFF;

hex[index++] = HEX_CHAR_TABLE[v >>> 4];

hex[index++] = HEX_CHAR_TABLE[v & 0xF];

}

return new String(hex);

}

private static byte uniteBytes(byte src0, byte src1) {

byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))

.byteValue();

_b0 = (byte) (_b0 << 4);

byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))

.byteValue();

byte ret = (byte) (_b0 | _b1);

return ret;

}

public static byte[] HexString2Bytes(String src) {

int length = src.length()/2;

byte[] ret = new byte[length];

byte[] tmp = src.getBytes();

for (int i = 0; i < length; ++i) {

ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);

}

return ret;

}

}

Demo:點擊下載。

總結

以上是生活随笔為你收集整理的protobuf java_ProtoBuf for java使用笔记 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。