您的位置:首页 > 其它

socket通信数据类型

2014-01-02 16:03 429 查看
在java中socket传输数据时,数据类型往往比较难选择。可能要考虑带宽、跨语言、版本的兼容等问题。比较常见的做法有两种:一是把对象包装成JSON字符串传输,二是采用java对象的序列化和反序列化。随着Google工具protoBuf的开源,protobuf也是个不错的选择。对JSON,Object Serialize,ProtoBuf 做个对比。定义一个待传输的对象UserVo:
Java代码





publicclass UserVo{

private String name;

privateint age;

privatelong phone;

private List<UserVo> friends;

……

}

public class UserVo{
private String name;
private int age;
private long phone;

private List<UserVo> friends;
……
}
初始化UserVo的实例src:
Java代码





UserVo src = new UserVo();

src.setName("Yaoming");

src.setAge(30);

src.setPhone(13789878978L);

UserVo f1 = new UserVo();

f1.setName("tmac");

f1.setAge(32);

f1.setPhone(138999898989L);

UserVo f2 = new UserVo();

f2.setName("liuwei");

f2.setAge(29);

f2.setPhone(138999899989L);

List<UserVo> friends = new ArrayList<UserVo>();

friends.add(f1);

friends.add(f2);

src.setFriends(friends);

UserVo src = new UserVo();
src.setName("Yaoming");
src.setAge(30);
src.setPhone(13789878978L);

UserVo f1 = new UserVo();
f1.setName("tmac");
f1.setAge(32);
f1.setPhone(138999898989L);
UserVo f2 = new UserVo();
f2.setName("liuwei");
f2.setAge(29);
f2.setPhone(138999899989L);

List<UserVo> friends = new ArrayList<UserVo>();
friends.add(f1);
friends.add(f2);
src.setFriends(friends);

JSON格式

采用Google的gson-2.2.2.jar 进行转义
Java代码





Gson gson = new Gson();

String json = gson.toJson(src);

Gson gson = new Gson();
String json = gson.toJson(src);
得到的字符串:
Js代码





{"name":"Yaoming","age":30,"phone":13789878978,"friends":[{"name":"tmac","age":32,"phone":138999898989},{"name":"liuwei","age":29,"phone":138999899989}]}

{"name":"Yaoming","age":30,"phone":13789878978,"friends":[{"name":"tmac","age":32,"phone":138999898989},{"name":"liuwei","age":29,"phone":138999899989}]}
字节数为153
Json的优点:明文结构一目了然,可以跨语言,属性的增加减少对解析端影响较小。缺点:字节数过多,依赖于不同的第三方类库。

Object Serialize

UserVo实现Serializalbe接口,提供唯一的版本号:
Java代码





publicclass UserVo implements Serializable{

privatestaticfinallong serialVersionUID = -5726374138698742258L;

private String name;

privateint age;

privatelong phone;

private List<UserVo> friends;

public class UserVo implements Serializable{

private static final long serialVersionUID = -5726374138698742258L;
private String name;
private int age;
private long phone;

private List<UserVo> friends;


序列化方法:
Java代码





ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream os = new ObjectOutputStream(bos);

os.writeObject(src);

os.flush();

os.close();

byte[] b = bos.toByteArray();

bos.close();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos);
os.writeObject(src);
os.flush();
os.close();
byte[] b = bos.toByteArray();
bos.close();
字节数是238

反序列化:
Java代码





ObjectInputStream ois = new ObjectInputStream(fis);

vo = (UserVo) ois.readObject();

ois.close();

fis.close();

ObjectInputStream ois = new ObjectInputStream(fis);
vo = (UserVo) ois.readObject();
ois.close();
fis.close();
Object Serializalbe 优点:java原生支持,不需要提供第三方的类库,使用比较简单。缺点:无法跨语言,字节数占用比较大,某些情况下对于对象属性的变化比较敏感。
对象在进行序列化和反序列化的时候,必须实现Serializable接口,但并不强制声明唯一的serialVersionUID
是否声明serialVersionUID对于对象序列化的向上向下的兼容性有很大的影响。我们来做个测试:
思路一把UserVo中的serialVersionUID去掉,序列化保存。反序列化的时候,增加或减少个字段,看是否成功。
Java代码





publicclass UserVo implements Serializable{

private String name;

privateint age;

privatelong phone;

private List<UserVo> friends;

public class UserVo implements Serializable{
private String name;
private int age;
private long phone;

private List<UserVo> friends;


保存到文件中:
Java代码





ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream os = new ObjectOutputStream(bos);

os.writeObject(src);

os.flush();

os.close();

byte[] b = bos.toByteArray();

bos.close();

FileOutputStream fos = new FileOutputStream(dataFile);

fos.write(b);

fos.close();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos);
os.writeObject(src);
os.flush();
os.close();
byte[] b = bos.toByteArray();
bos.close();

FileOutputStream fos = new FileOutputStream(dataFile);
fos.write(b);
fos.close();


增加或者减少字段后,从文件中读出来,反序列化:
Java代码





FileInputStream fis = new FileInputStream(dataFile);

ObjectInputStream ois = new ObjectInputStream(fis);

vo = (UserVo) ois.readObject();

ois.close();

fis.close();

FileInputStream fis = new FileInputStream(dataFile);
ObjectInputStream ois = new ObjectInputStream(fis); vo = (UserVo) ois.readObject(); ois.close(); fis.close();


结果:抛出异常信息
Java代码





Exception in thread "main" java.io.InvalidClassException: serialize.obj.UserVo; local class incompatible: stream classdesc serialVersionUID = 3305402508581390189, local class serialVersionUID = 7174371419787432394

at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:560)

at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1582)

at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)

at serialize.obj.ObjectSerialize.read(ObjectSerialize.java:74)

at serialize.obj.ObjectSerialize.main(ObjectSerialize.java:27)

Exception in thread "main" java.io.InvalidClassException: serialize.obj.UserVo; local class incompatible: stream classdesc serialVersionUID = 3305402508581390189, local class serialVersionUID = 7174371419787432394
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:560)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1582)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at serialize.obj.ObjectSerialize.read(ObjectSerialize.java:74)
at serialize.obj.ObjectSerialize.main(ObjectSerialize.java:27)
思路二eclipse指定生成一个serialVersionUID,序列化保存,修改字段后反序列化
略去代码
结果:反序列化成功
结论如果没有明确指定serialVersionUID,序列化的时候会根据字段和特定的算法生成一个serialVersionUID,当属性有变化时这个id发生了变化,所以反序列化的时候就会失败。抛出“本地classd的唯一id和流中class的唯一id不匹配”。

jdk文档关于serialVersionUID的描述:
写道如果可序列化类未显式声明 serialVersionUID,则序列化运行时将基于该类的各个方面计算该类的默认 serialVersionUID 值,如“Java(TM) 对象序列化规范”中所述。不过,强烈建议 所有可序列化类都显式声明 serialVersionUID 值,原因是计算默认的 serialVersionUID 对类的详细信息具有较高的敏感性,根据编译器实现的不同可能千差万别,这样在反序列化过程中可能会导致意外的 InvalidClassException。因此,为保证 serialVersionUID 值跨不同 java 编译器实现的一致性,序列化类必须声明一个明确的 serialVersionUID 值。还强烈建议使用 private 修饰符显示声明 serialVersionUID(如果可能),原因是这种声明仅应用于直接声明类 -- serialVersionUID 字段作为继承成员没有用处。数组类不能声明一个明确的 serialVersionUID,因此它们总是具有默认的计算值,但是数组类没有匹配 serialVersionUID 值的要求。

Google ProtoBuf

protocol buffers 是google内部得一种传输协议,目前项目已经开源(http://code.google.com/p/protobuf/)。它定义了一种紧凑得可扩展得二进制协议格式,适合网络传输,并且针对多个语言有不同得版本可供选择。
以protobuf-2.5.0rc1为例,准备工作:
下载源码,解压,编译,安装
Shell代码





tar zxvf protobuf-2.5.0rc1.tar.gz

./configure

./make

./make install

tar zxvf protobuf-2.5.0rc1.tar.gz
./configure
./make
./make install
测试:
Shell代码





MacBook-Air:~ ming$ protoc --version

libprotoc 2.5.0

MacBook-Air:~ ming$ protoc --version
libprotoc 2.5.0
安装成功!进入源码得java目录,用mvn工具编译生成所需得jar包,protobuf-java-2.5.0rc1.jar

1、编写.proto文件,命名UserVo.proto
Text代码





package serialize;

option java_package = "serialize";

option java_outer_classname="UserVoProtos";

message UserVo{

optional string name = 1;

optional int32 age = 2;

optional int64 phone = 3;

repeated serialize.UserVo friends = 4;

}

package serialize;

option java_package = "serialize";
option java_outer_classname="UserVoProtos";

message UserVo{
optional string name = 1;
optional int32 age = 2;
optional int64 phone = 3;
repeated serialize.UserVo friends = 4;
}


2、在命令行利用protoc 工具生成builder类
Shell代码





protoc -IPATH=.proto文件所在得目录 --java_out=java文件的输出路径 .proto的名称
protoc -IPATH=.proto文件所在得目录 --java_out=java文件的输出路径  .proto的名称
得到UserVoProtos类

3、编写序列化代码
Java代码





UserVoProtos.UserVo.Builder builder = UserVoProtos.UserVo.newBuilder();

builder.setName("Yaoming");

builder.setAge(30);

builder.setPhone(13789878978L);

UserVoProtos.UserVo.Builder builder1 = UserVoProtos.UserVo.newBuilder();

builder1.setName("tmac");

builder1.setAge(32);

builder1.setPhone(138999898989L);

UserVoProtos.UserVo.Builder builder2 = UserVoProtos.UserVo.newBuilder();

builder2.setName("liuwei");

builder2.setAge(29);

builder2.setPhone(138999899989L);

builder.addFriends(builder1);

builder.addFriends(builder2);

UserVoProtos.UserVo vo = builder.build();

byte[] v = vo.toByteArray();

UserVoProtos.UserVo.Builder builder = UserVoProtos.UserVo.newBuilder();
builder.setName("Yaoming");
builder.setAge(30);
builder.setPhone(13789878978L);

UserVoProtos.UserVo.Builder builder1 = UserVoProtos.UserVo.newBuilder();
builder1.setName("tmac");
builder1.setAge(32);
builder1.setPhone(138999898989L);

UserVoProtos.UserVo.Builder builder2 = UserVoProtos.UserVo.newBuilder();
builder2.setName("liuwei");
builder2.setAge(29);
builder2.setPhone(138999899989L);

builder.addFriends(builder1);
builder.addFriends(builder2);

UserVoProtos.UserVo vo = builder.build();

byte[] v = vo.toByteArray();
字节数53

4、反序列化
Java代码





UserVoProtos.UserVo uvo = UserVoProtos.UserVo.parseFrom(dstb);

System.out.println(uvo.getFriends(0).getName());

UserVoProtos.UserVo uvo = UserVoProtos.UserVo.parseFrom(dstb);
System.out.println(uvo.getFriends(0).getName());
结果:tmac,反序列化成功google protobuf 优点:字节数很小,适合网络传输节省io,跨语言 。缺点:需要依赖于工具生成代码。

工作机制proto文件是对数据的一个描述,包括字段名称,类型,字节中的位置。protoc工具读取proto文件生成对应builder代码的类库。protoc xxxxx --java_out=xxxxxx 生成java类库。builder类根据自己的算法把数据序列化成字节流,或者把字节流根据反射的原理反序列化成对象。官方的示例:https://developers.google.com/protocol-buffers/docs/javatutorial。
proto文件中的字段类型和java中的对应关系:
详见:https://developers.google.com/protocol-buffers/docs/proto
.proto Typejava Typec++ Type
doubledoubledouble
floatfloatfloat
int32intint32
int64long int64
uint32intuint32
unint64longuint64
sint32intint32
sint64longint64
fixed32intuint32
fixed64longuint64
sfixed32intint32
sfixed64longint64
boolbooleanbool
stringStringstring
bytesbytestring
字段属性的描述:写道required: a well-formed message must have exactly one of this field.
optional: a well-formed message can have zero or one of this field (but not more than one).
repeated: this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.protobuf 在序列化和反序列化的时候,是依赖于.proto文件生成的builder类完成,字段的变化如果不表现在.proto文件中就不会影响反序列化,比较适合字段变化的情况。做个测试:把UserVo序列化到文件中:Java代码





UserVoProtos.UserVo vo = builder.build();

byte[] v = vo.toByteArray();

FileOutputStream fos = new FileOutputStream(dataFile);

fos.write(vo.toByteArray());

fos.close();

UserVoProtos.UserVo vo = builder.build();
byte[] v = vo.toByteArray();
FileOutputStream fos = new FileOutputStream(dataFile);
fos.write(vo.toByteArray());
fos.close();
为UserVo增加字段,对应的.proto文件:Text代码





package serialize;

option java_package = "serialize";

option java_outer_classname="UserVoProtos";

message UserVo{

optional string name = 1;

optional int32 age = 2;

optional int64 phone = 3;

repeated serialize.UserVo friends = 4;

optional string address = 5;

}

package serialize;

option java_package = "serialize";
option java_outer_classname="UserVoProtos";

message UserVo{
optional string name = 1;
optional int32 age = 2;
optional int64 phone = 3;
repeated serialize.UserVo friends = 4;
optional string address = 5;
}
从文件中反序列化回来:Java代码





FileInputStream fis = new FileInputStream(dataFile);

byte[] dstb = newbyte[fis.available()];

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

dstb[i] = (byte)fis.read();

}

fis.close();

UserVoProtos.UserVo uvo = UserVoProtos.UserVo.parseFrom(dstb);

System.out.println(uvo.getFriends(0).getName());

FileInputStream fis = new FileInputStream(dataFile);
byte[] dstb = new byte[fis.available()];
for(int i=0;i<dstb.length;i++){
dstb[i] = (byte)fis.read();
}
fis.close();
UserVoProtos.UserVo uvo = UserVoProtos.UserVo.parseFrom(dstb); System.out.println(uvo.getFriends(0).getName());
成功得到结果。三种方式对比传输同样的数据,google protobuf只有53个字节是最少的。结论:
方式优点缺点
JSON跨语言、格式清晰一目了然
字节数比较大,需要第三方类库
Object Serializejava原生方法不依赖外部类库字节数比较大,不能跨语言
Google protobuf跨语言、字节数比较少
编写.proto配置用protoc工具生成对应的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: