Netty学习07--Protocol Buff

1.protocol buff是一种协议,是谷歌推出的一种序列化协议

2.Java序列化协议也是一种协议

3.两者的目的是,将对象序列化成字节数组,或者说是二进制数据

 

下面使用代码来体现两者的区别

目录结构

Netty学习07--Protocol Buff_第1张图片

protoc.exe 编译工具

build.bat 编译脚本

protoc ./proto/*.proto --java_out=./src

pause

player.proto  更多请参考

option java_package = "com.proto";
option java_outer_classname = "PlayerModule";

message PBPlayer{
	required int64 playerId = 1;
	
	required int32 age = 2;
	
	required string name = 3;
	
	repeated int32 skills = 4;
}

message PBResource{
	required int64 gold = 1;
	
	required int32 energy = 2;
}
PlayerModule 通过执行build.bat会自动生成
PB2Bytes
package com.proto;

import java.util.Arrays;
import com.proto.PlayerModule.PBPlayer;
import com.proto.PlayerModule.PBPlayer.Builder;

/**
 * protobuf学习
 */
public class PB2Bytes {

	public static void main(String[] args) throws Exception {
		byte[] bytes = toBytes();
		toPlayer(bytes);

	}

	/**
	 * 序列化
	 */
	public static byte[] toBytes(){
		//获取一个PBPlayer的构造器
		Builder builder = PBPlayer.newBuilder();
		//设置数据
		builder.setPlayerId(101).setAge(20).setName("peter").addSkills(1001);
		//构造出对象
		PBPlayer player = builder.build();
		//序列化成字节数组
		byte[] byteArray = player.toByteArray();
		
		System.out.println(Arrays.toString(byteArray));
		
		return byteArray;
	}
	
	/**
	 * 反序列化
	 * @param bs
	 * @throws Exception 
	 */
	public static void toPlayer(byte[] bs) throws Exception{
		
		 PBPlayer player = PBPlayer.parseFrom(bs);
		 
		 System.out.println("playerId:" + player.getPlayerId());
		 System.out.println("age:" + player.getAge());
		 System.out.println("name:" + player.getName());
		 System.out.println("skills:" + (Arrays.toString(player.getSkillsList().toArray())));
	}
}

运行效果: 

Netty学习07--Protocol Buff_第2张图片

JAVA的序列化和反序列化

Player
package com.java;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 玩家对象
 */
public class Player implements Serializable{

	private static final long serialVersionUID = -5248069984631225347L;

	public Player(long playerId,  int age, String name) {
		this.playerId = playerId;
		this.age = age;
		this.name = name;
	}
	
	private long playerId;
	
	private int age;
	
	private String name;
	
	private List skills = new ArrayList<>();

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List getSkills() {
		return skills;
	}

	public void setSkills(List skills) {
		this.skills = skills;
	}
}
JAVA2Bytes
package com.java;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

public class JAVA2Bytes {

	public static void main(String[] args) throws Exception {
		byte[] bytes = toBytes();
		toPlayer(bytes);
	}
	
	
	/**
	 * 序列化
	 * @throws IOException 
	 */
	public static byte[] toBytes() throws IOException{
		
		Player player = new Player(101, 20, "peter");
		player.getSkills().add(1001);
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		
		//写入对象
		objectOutputStream.writeObject(player);
		
		//获取 字节数组
		byte[] byteArray = byteArrayOutputStream.toByteArray();
		System.out.println(Arrays.toString(byteArray));
		return byteArray;
	}
	
	
	/**
	 * 反序列化
	 * @param bs
	 * @throws Exception 
	 */
	public static void toPlayer(byte[] bs) throws Exception{
		
		ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(bs));
		Player player = (Player)inputStream.readObject();
		
		//打印
		 System.out.println("playerId:" + player.getPlayerId());
		 System.out.println("age:" + player.getAge());
		 System.out.println("name:" + player.getName());
		 System.out.println("skills:" + (Arrays.toString(player.getSkills().toArray())));
	}

}

测试效果

通过运行效果得知,java的序列化生成的字节比protoc方式更占内存。

代码下载​​​​​​​

你可能感兴趣的:(Netty)