测试Hessian反序反序列化 客户端少字段和多字段时能否成功
Posted lccsblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试Hessian反序反序列化 客户端少字段和多字段时能否成功相关的知识,希望对你有一定的参考价值。
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反序反序列化 客户端少字段和多字段时能否成功的主要内容,如果未能解决你的问题,请参考以下文章
UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)