测试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去掉结果如下:

都是可以成功反序列化的

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

时间: 2024-07-31 05:30:21

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

【字符串处理算法】将输入字符串中的各个单词反序的算法设计及C代码实现

一.需求描述 输入一个字符串,编写程序将该字符串中的各个单词反序拼装并输出.例如,如果输入的字符串是"Hello, how do you do",那么输出的字符串为"do you do how Hello,".注意保留各个单词之间的空格及相应的标点符号. 二.算法设计 通过观察示例字符串(即"Hello, how do you do"),我们可以看到该字符串中各个单词与空格之间的关系为:单词总数=空格总数+1.也就是说,示例字符串中的空格总数为4

如何测试手机上的SOAP客户端

周四晚上,服务端和客户端的两个同事因为soap接口的问题争论了起来.服务端的同事认为客户端的同事发给服务端的soap消息的xml结构有问题,少了几个xml节点,导致服务器端解析出错.而客户端的同事认为自己在android手机上发送的soap,是通过一个对象发出的,对象属性什么的,看代码是正确设置了的,不认为是自己这边出问题. 这个时候,就要靠抓包定位了.服务端那边的同事,本来是打算用wireshark来抓包的.后面另外一个同事提出抓客户端,即手机上的包.之前我抓手机上的包的时候,是让电脑共享无线

android listview反序和正序显示

本人很喜欢晚上睡觉用暴风语音看电影,如图: 现在写个demo 演示下 MainActivity.java package com.example.listview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickLis

给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序

[问题] 找出反序的个数 给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序. 即:数组a[]; 对于i < j 且 a[i] > a[j],则称这是一个反序. 给定一个数组,要求写一个函数,计算出这个数组里所有反序的个数. [代码] #include <stdio.h> #include <stdlib.h> #include <string.h> int sumNum = 0; void merge(int *a,

反序链表

原题: Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6. Input Specif

oc之数组反序输出示例

1. // 反序迭代器(从尾部开始遍历元素) NSEnumerator *enumerator = [array reverseObjectEnumerator]; // allObjects是取出没有被遍历过的对象 NSArray *array2 = [enumerator allObjects]; NSLog(@"array2:%@", array2); 2. NSArray *sortarr=[arr sortedArrayUsingSelector:@selector(clas

SortedDictionary&lt;TKey,TValue&gt;正序与反序排序

SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortDictionary { class Program { static void Main(string[] args) { TestDictionarySort()

(练手备忘)汇编实现将输入的字符串中的空格去掉后反序输出

功能:任意输入一个字符串,去掉其中的空格后反序输出 注:使用 int 21h 里的 0AH 功能 输入一个字符串时,字符串的第一个字节存储的是字符串的最大长度,第二个字节存储的是实际读入字符的个数 编译器使用的是MASMPlus ;#Mode = DOS MAXLEN = 64 ;设置字符串的最大长度 SPACE = ' ' ;空格 datasg segment buffer db MAXLEN+1,0,MAXLEN+1 dup(0) ;字符串输入缓冲区 string db MAXLEN+3 d

treemap反序输出 逆序输出 输出倒数几个值

treemap是按键的ASCII码从小到大排序的,比如要对若干个带有时间属性的对象排序时,可以用时间作键,放到Treemap中,即是有序集合了.先不管性能,省了很多自己写排序的实现了. 默认是按key的ASCII码顺序由小到大排序的,如果要实现自定义的排序,则要重写treemap的比较器. 最简单的方法就是使用集合对象自带的方法,Collections.reverseOrder() 具体代码如下 package com.example.commonwtf2.test; import java.u