对象序列化

在分布式系统,任何数据都要转化为二进制流在网络上进行传输,在面向对象设计中,我们经常会面临如何将定义好的对象发送到远端的问题,换言之,问题精确为发送方如何将对象转化为二进制流,接收方如何将二进制数据流转化为对象。将对象转换为二进制流的过程称之为对象序列化,反之将二进制流恢复为对象的过程称为反序列化。

目前成熟的解决方案包括: Google的protocal Buffer, Java内置的序列化,Hessian等,以及JSON和XMl。Protol Buffer性能优异,缺点需要编写proto文件,无法直接使用Java等中的对象。Hessian相比于ProtolBuffer效率稍低,但是对其他语言的支持优良,且性能稳定,比Java的内置序列化机制要高。Java的序列化机制不需要引入第三方包,使用简单,在对效率要求不是很高的情况下,可以考虑使用。

本文重点讲解一下Java内置的序列化方式和基于Java的Hessian序列化方式,代码实施如下所示

Java的内置序列化code实施代码

    public byte[] Serialize(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            //将对象写入到字节数组中进行序列化
            objectOutputStream.writeObject(object);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public Object deSerialize(byte[] bytes) {
        //将二进制数组导入字节数据流中
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        try {
            //将字节数组流转化为对象
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

基于Hession的序列化实施代码

使用Hession进行序列化,需要引入Hessian包,maven添加如下依赖:

 <dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.38</version>
</dependency>
 public byte[] Serialize(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //Hession的序列化
        HessianOutput hessianOutput = new HessianOutput(byteArrayOutputStream);
        try {
            hessianOutput.writeObject(object);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Object deSerialize(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        HessianInput hessianInput = new HessianInput(byteArrayInputStream);
        try {
            return hessianInput.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

代码测试

测试代码如下所示,分别对上面两种方式进行junit测试,使用的测试对象Employee实现Serializable接口。

@Test
public void should_serialize_and_deserizlie_when_apply_java_serialize() throws Exception {
    //given
    SerialSample serialSample=new SerialSample();
    Employee employee=new Employee("xiaoya",1);
    //when
    byte[] serialize = serialSample.Serialize(employee);
    Employee result = (Employee) serialSample.deSerialize(serialize);
    //then
    assertThat(result,is(employee));
}
@Test
public void should_serialize_and_deserizlie_object_when_apply_hession_serialize() throws Exception {
    //when
    HessionSample hessionSample = new HessionSample();
    Employee employee = new Employee("xiaoya", 1);
    //given
    byte[] serialize = hessionSample.Serialize(employee);
    Employee result = (Employee) hessionSample.deSerialize(serialize);
    //then
    assertThat(result, is(employee));
}

Conclusion

今天主要和大家分享了序列化的一些基本知识和技巧,希望对大家有所帮助。本文的主要参考来源于大型分布式网站架构设计与实践这本书,今后一段时间里的博文将会以这本书作为主要参考来源。

原文地址:https://www.cnblogs.com/jun-ma/p/4883516.html