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使用笔记 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java字数统计_java统计字数
- 下一篇: java用法_Java 习惯用法总结