JAVA 一个简单的原生RPC例子之序列化

这样介绍几种序列化方法。
一、JDK自带序列化

package org.Simple.API;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * JDK自带序列化
 */
public class SerializeUtils {

    /**
     * 序列化
     * @param object
     * @return
     * @throws IOException
     */
    public static byte[] serialize(Object object) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(os);
        outputStream.writeObject(object);
        outputStream.flush();
        byte[] byteArray = os.toByteArray();    
        outputStream.close();
        os.close();
        return byteArray;
    }
    /**
     * 反序列化
     * @param buf
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deSerialize(byte[] buf) throws IOException, ClassNotFoundException {
        ByteArrayInputStream is = new ByteArrayInputStream(buf);
        ObjectInputStream inputStream = new ObjectInputStream(is);
        Object object =  inputStream.readObject();
        inputStream.close();
        is.close();
        return object;
    }
}

二、Protobuf序列化,该序列化不要求对象实现Serializable接口

package org.Simple.API;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

/**
 * Protobuf序列化
 */
public class ProtobufSerialize {
    public static <T> byte[] serialize(T t,Class<T> clazz) {
        return ProtobufIOUtil.toByteArray(t, RuntimeSchema.createFrom(clazz),
                LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));//这个长度分配可以根据自身情况填写
    }
    public static <T> T deSerialize(byte[] data,Class<T> clazz) {
        RuntimeSchema<T> runtimeSchema = RuntimeSchema.createFrom(clazz);
        T t = runtimeSchema.newMessage();
        ProtobufIOUtil.mergeFrom(data, t, runtimeSchema);
        return t;
    }

}

maven项目下的jar包

<!-- Protobuf -->
        <dependency>
            <groupId>com.dyuproject.protostuff</groupId>
            <artifactId>protostuff-core</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.dyuproject.protostuff</groupId>
            <artifactId>protostuff-runtime</artifactId>
            <version>1.1.1</version>
        </dependency>

三、Hessian序列化

package org.Simple.API;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
/**
 * Hessian序列化
 */
public class HessianSerialize {

    /**
     * 序列化
     * @param object
     * @return
     * @throws IOException
     */
    public static byte[] Serialize(Object object) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Hessian2Output hOutput = new Hessian2Output(os);
        hOutput.writeObject(object);
        hOutput.flush();
        byte[] byteArray = os.toByteArray();
        hOutput.close();
        os.close();
        return byteArray;
    }
    /**
     * 反序列化
     * @param buf
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deSerialize(byte[] buf) throws IOException, ClassNotFoundException {
        ByteArrayInputStream is = new ByteArrayInputStream(buf);
        Hessian2Input hInput = new Hessian2Input(is);
        Object object =  hInput.readObject();
        hInput.close();
        is.close();
        return object;
    }
}

maven项目下的jar包

<dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.51</version>
        </dependency>

四、测试例子

package org.Simple.API;

import java.io.IOException;
/**
 * 几个序列化比较
 */
public class SerializeTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person person = new Person();
        person.setAge(20);
        person.setName("zhangsan");
        byte[] byte1 = SerializeUtils.serialize(person);
        System.out.println("JDK:"+byte1.length);
        System.out.println(SerializeUtils.deSerialize(byte1));
        byte[] byte2 = HessianSerialize.Serialize(person);
        System.out.println("Hessian:"+byte2.length);
        System.out.println(HessianSerialize.deSerialize(byte2));
        byte[] byte3 = ProtobufSerialize.serialize(person, Person.class);
        System.out.println("Protobuf:"+byte3.length);
        System.out.println(ProtobufSerialize.deSerialize(byte3, Person.class));
    }
}

运行结果
这里写图片描述
序列化后的字节长度Protobuf >Hessian>JDK自带,可以看出Protobuf序列化反序列化性能最好,Hessian次之,JDK自带的最差

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值