Netty3 - 对象的序列化与反序列化 java

Java 本身(Serializable) 对象的序列化与反序列化

1.定义对象(与上节中提到的ProtoBuf中定义的employee字段是相同的)

package javaserializable;

import java.io.Serializable;

public class Employee implements Serializable{
    private long emploee_id ;
    private String first_name;
    private String last_name;
    private int age;

    public Employee(long emploee_id ,String first_name,String last_name,int age){
        this.emploee_id=emploee_id;
        this.first_name=first_name;
        this.last_name=last_name;
        this.age=age;
    }

    public long getEmploee_id() {
        return emploee_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public int getAge() {
        return age;
    }
}

2.对象的序列化与反序列化操作

package javaserializable;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

/**
 * Jdk提供对象序列化与反序列化的机制
 */
public class JSMain {

    public static void main(String[] args) throws  Exception{
        //序列化
        ByteOutputStream byteOutputStream=new ByteOutputStream();
        ObjectOutputStream dataOutputStream=new ObjectOutputStream(byteOutputStream);
        Employee  employee =new Employee(1000,"Gam","Xie",30);
        dataOutputStream.writeObject(employee);
        byte[] employeeBytes = byteOutputStream.getBytes();
        System.out.println("byte length="+employeeBytes.length+",conent="+ Arrays.toString(employeeBytes));

        //反序列化

        ByteArrayInputStream byteInputStream=new ByteArrayInputStream(employeeBytes);
        ObjectInputStream objectInputStream=new ObjectInputStream(byteInputStream);
        Employee newEmployee=(Employee)objectInputStream.readObject();
        System.out.println("parse employeeId="+newEmployee.getEmploee_id());
        System.out.println("parse first name="+newEmployee.getFirst_name());
        System.out.println("parse last name="+newEmployee.getLast_name());
        System.out.println("parse age="+newEmployee.getAge());

    }
}

 

3.Jdk 序列化与ProtoBuf序列结果的比较

<a>大小:

protobuf: 15

byte length=15,conent=[8, -24, 7, 18, 3, 71, 97, 109, 26, 3, 88, 105, 101, 32, 30]

Jdk:1024

byte length=1024,conent=[-84, -19, 0, 5, 115, 114, 0, 25, 106, 97, 118, 97, 115, 101, 114, 105, 97, 108, 105, 122, 97, 98, 108, 101, 46, 69, 109, 112, 108, 111, 121, 101, 101, 127, -34, -4, -19, 20, 117, 51, -68, 2, 0, 4, 73, 0, 3, 97, 103, 101, 74, 0, 10, 101, 109, 112, 108, 111, 101, 101, 95, 105, 100, 76, 0, 10, 102, 105, 114, 115, 116, 95, 110, 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 9, 108, 97, 115, 116, 95, 110, 97, 109, 101, 113, 0, 126, 0, 1, 120, 112, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 3, -24, 116, 0, 3, 71, 97, 109, 116, 0, 3, 88, 105, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0]

<b>对象序列时间的对比(1000次对象所花费的时间)

protobuf:

long beginTime=System.currentTimeMillis();                                                 
for(int i=0;i<1000;i++) {                                                                  
    EmployeeModule.Employee.Builder builder = EmployeeModule.Employee.newBuilder();        
    builder.setEmploeeId(1000);                                                            
    builder.setFirstName("Gam");                                                           
    builder.setLastName("Xie");                                                            
    builder.setAge(30);                                                                    
                                                                                           
    EmployeeModule.Employee employee = builder.build();                                    
    byte[] bytes = employee.toByteArray();                                                 
}                                                                                          
System.out.println("time="+(System.currentTimeMillis()-beginTime));         

结果时间 time=289               
                                                                                           

Jdk:

  //序列化
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<1000;i++) {
            ByteOutputStream byteOutputStream = new ByteOutputStream();
            ObjectOutputStream dataOutputStream = new ObjectOutputStream(byteOutputStream);
            Employee employee = new Employee(1000, "Gam", "Xie", 30);
            dataOutputStream.writeObject(employee);
            byte[] employeeBytes = byteOutputStream.getBytes();
        }
        System.out.println("time="+(System.currentTimeMillis()-beginTime));

结果 time=356

大致上可以看出:Protobuf 在序列化字节大小与序列化所花费的时间上要比JDK的性能要好

 

后续分析两者差异所在?

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值