博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Netty3 - 对象的序列化与反序列化 java
阅读量:4041 次
发布时间:2019-05-24

本文共 7300 字,大约阅读时间需要 24 分钟。

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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0, 0, 0, 0, 0, 0, 0, 0, 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的性能要好

 

后续分析两者差异所在?

 

 

 

转载地址:http://nwadi.baihongyu.com/

你可能感兴趣的文章
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
arm-linux开机读取硬件时钟,设置系统时钟。
查看>>
交叉编译在x86上调试好的qt程序
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>