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 + '\'' + '}'; } }
We are serialized writes the results file, the new test field in the Employee, the deserialized results are as follows:
The Employee age and remove the test results are as follows:
We can all be successfully deserialized