码迷,mamicode.com
首页 > 其他好文 > 详细

测试Hessian反序反序列化 客户端少字段和多字段时能否成功

时间:2019-12-15 20:10:33      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ring   vat   write   public   des   tput   nts   ima   override   

import java.io.*;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import hessian.Employee;

public class HessianSerializeDeserializeMain {
    /**
     * Hessian实现序列化
     * @param employee
     * @return
     * @throws IOException
     */
    public static byte[] serialize(Employee employee){
        ByteArrayOutputStream byteArrayOutputStream = null;
        HessianOutput hessianOutput = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            // Hessian的序列化输出
            hessianOutput = new HessianOutput(byteArrayOutputStream);
            hessianOutput.writeObject(employee);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                hessianOutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Hessian实现反序列化
     * @param employeeArray
     * @return
     */
    public static Employee deserialize(byte[] employeeArray) {
        ByteArrayInputStream byteArrayInputStream = null;
        HessianInput hessianInput = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(employeeArray);
            // Hessian的反序列化读取对象
            hessianInput = new HessianInput(byteArrayInputStream);
            return (Employee)hessianInput.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                hessianInput.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
//       doSerialize();
//        System.out.println(serialize);
//        // 反序列化
//        Employee deserialize = deserialize(serialize);
//        System.out.println(deserialize.toString());
        deSerialize();
    }

    public static void doSerialize() throws IOException {
        Employee employee = new Employee();
        employee.setEmployeeId(1);
        employee.setEmployeeName("lcc");
//        employee.setAge("nan");
        // 序列化
        byte[] serialize = serialize(employee);
        File file=new File("./serialize");
        if (!file.exists()){
            file.createNewFile();
        }
        System.out.println(String.valueOf(serialize));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
        bufferedOutputStream.write(serialize);
        bufferedOutputStream.flush();
    }

    public static void deSerialize() throws IOException {
        File file=new File("./serialize");
        FileInputStream fileInputStream=new FileInputStream(file);
        BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
        byte result[]=new byte[1024];
        if (bufferedInputStream.read(result)>result.length){
            System.out.println("too small");
        }
        // 反序列化
        Employee deserialize = deserialize(result);
        System.out.println(deserialize.toString());
    }


}

Employee:

public class Employee {
    private int employeeId;
    private String employeeName;
    private String age;

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "employeeId=" + employeeId +
                ", employeeName=‘" + employeeName + ‘\‘‘ +
                ", age=‘" + age + ‘\‘‘ +
                ‘}‘;
    }
}

我们现进行序列化将结果写道文件中,在Employee中新增test字段,反序列化结果如下:

技术图片

 

 将Employee中的test和age去掉结果如下:

技术图片

 

都是可以成功反序列化的

测试Hessian反序反序列化 客户端少字段和多字段时能否成功

标签:ring   vat   write   public   des   tput   nts   ima   override   

原文地址:https://www.cnblogs.com/lccsblog/p/12045558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!