dubbo序列化和反序列化的测试代码

package org.didinem.hessian2;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.serialize.ObjectOutput;
import com.alibaba.dubbo.common.serialize.Serialization;
import com.alibaba.dubbo.common.serialize.support.hessian.Hessian2ObjectInput;
import com.alibaba.dubbo.common.serialize.support.hessian.Hessian2ObjectOutput;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.didinem.api.TestApiService;
import org.didinem.object.Person;
import org.junit.Test;

import java.io.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * Created by didinem on 9/3/2017.
 */
public class Hessian2Test {

    @Test
    public void test() throws IOException {
        Person person = new Person(1, "aaa");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Hessian2ObjectOutput hessian2ObjectOutput = new Hessian2ObjectOutput(byteArrayOutputStream);
        hessian2ObjectOutput.writeObject(person);
        hessian2ObjectOutput.flushBuffer();

        byte[] byteArray = byteArrayOutputStream.toByteArray();
        System.out.println(Arrays.toString(byteArray));
    }

    @Test
    public void serializationSPI() throws IOException {
        URL url = URL.valueOf("dubbo://192.168.99.1:20880/org.didinem.api.TestApiService?anyhost=true&application=play-dubbo-provider&dubbo=2.5.3&interface=org.didinem.api.TestApiService&methods=setService,mapService&pid=12632&revision=1.0-SNAPSHOT&side=provider&timestamp=1504442138354");
        ExtensionLoader<Serialization> extensionLoader = ExtensionLoader.getExtensionLoader(Serialization.class);
        Serialization hessian2Serialization = extensionLoader.getExtension("hessian2");

        File file = new File("D:\\aaa.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutput objectOutput = hessian2Serialization.serialize(url, fileOutputStream);
        Person person = new Person(1, "aaa");
        objectOutput.writeObject(person);
        objectOutput.flushBuffer();

    }

    @Test
    public void mapTest() throws IOException {
        Map<String, String> map = Maps.newHashMap();
        map.put("aa", "11");

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Hessian2ObjectOutput hessian2ObjectOutput = new Hessian2ObjectOutput(byteArrayOutputStream);
        hessian2ObjectOutput.writeObject(map);
        hessian2ObjectOutput.flushBuffer();

        byte[] byteArray = byteArrayOutputStream.toByteArray();
        System.out.println(Arrays.toString(byteArray));

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        Hessian2ObjectInput hessian2ObjectInput = new Hessian2ObjectInput(byteArrayInputStream);
        Map<String, String> result = (Map<String, String>) hessian2ObjectInput.readObject();
        System.out.println(result);

    }

    @Test
    public void setTest() throws IOException, NoSuchMethodException, ClassNotFoundException {
        int[] arrays = new int[]{2, 4, 5, 7, 1, 3, 6};
        Set<Integer> set = Sets.newLinkedHashSet();
        for (int element : arrays) {
            set.add(element);
        }
        System.out.println(set);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Hessian2ObjectOutput hessian2ObjectOutput = new Hessian2ObjectOutput(byteArrayOutputStream);
        hessian2ObjectOutput.writeObject(set);
        hessian2ObjectOutput.flushBuffer();

        byte[] byteArray = byteArrayOutputStream.toByteArray();
        System.out.println(Arrays.toString(byteArray));

        Class clazz = TestApiService.class;
        Method method = clazz.getDeclaredMethod("setService", int[].class);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        Hessian2ObjectInput hessian2ObjectInput = new Hessian2ObjectInput(byteArrayInputStream);
//        Set<Integer> result = (Set<Integer>) hessian2ObjectInput.readObject();
        Object result = hessian2ObjectInput.readObject(method.getReturnType(), method.getGenericReturnType());
        System.out.println(result);
    }

    @Test
    public void serverSerializeResult() throws IOException {
        byte[] bytes = new byte[]{-38, -69, 2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, -111, 119, 23, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 76, 105, 110, 107, 101, 100, 72, 97, 115, 104, 83, 101, 116, -110, -108, -107, -105, -111, -109, -106};

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        Hessian2ObjectInput hessian2ObjectInput = new Hessian2ObjectInput(byteArrayInputStream);
        Object result = hessian2ObjectInput.readObject();
        System.out.println(result);
    }

    @Test
    public void clientDecode() throws IOException {
        byte[] bytes = new byte[]{-38, -69, 2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, -111, 119, 23, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 76, 105, 110, 107, 101, 100, 72, 97, 115, 104, 83, 101, 116, -110, -108, -107, -105, -111, -109, -106};
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

        URL url = URL.valueOf("dubbo://10.115.1.88:20880/org.didinem.api.TestApiService?anyhost=true&application=play-dubbo-consumer&check=false&codec=dubbo&default.check=false&dubbo=2.5.3&heartbeat=60000&interface=org.didinem.api.TestApiService&methods=setService,mapService&monitor=dubbo%3A%2F%2F192.168.0.107%3A7071%3Fdubbo%3D2.5.3%26interface%3Dcom.alibaba.dubbo.monitor.MonitorService%26pid%3D8208%26timestamp%3D1504527533648&pid=8208&retries=0&revision=1.0-SNAPSHOT&side=consumer&timeout=1000000&timestamp=1504527533550");

        ExtensionLoader<Serialization> extensionLoader = ExtensionLoader.getExtensionLoader(Serialization.class);
        Serialization hessian2Serialization = extensionLoader.getExtension("hessian2");

        Hessian2ObjectInput objectInput = (Hessian2ObjectInput) hessian2Serialization.deserialize(url, byteArrayInputStream);
        Object object = objectInput.readObject();
        System.out.println(object);

    }

    @Test
    public void andTest() {
        byte b = 106;
        System.out.println(b & 0xff);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micrle_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值