Hessian 不能反序列化HashMap问题记录,3.2.0+以上版本的BUG

代码段:


    public static void main(String[] args) throws IOException {
        HashMap<String,String> hashMap = new HashMap<>();

        hashMap.put("1","2");


        byte[] userByte = getData(hashMap);
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream( userByte);
        HessianInput input = new HessianInput(inputStream);
        Object o = input.readObject(Serializable.class);
        Assert.assertNotNull(o);

    }

    private static <V extends Serializable> byte[] getData(V v) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //Hessian的序列化输出
        HessianOutput ho = new HessianOutput(os);
        ho.writeObject(v);
        ho.flush();
        os.flush();
        return os.toByteArray();

    }

错误原因:
com.caucho.hessian.io.HessianInput

 /**
     * Reads an object from the input stream with an expected type.
     */
    public Object readObject(Class cl)
        throws IOException
    {
        if (cl == null || cl == Object.class)
            return readObject();

        int tag = read();

        switch (tag) {
            case 'N':
                return null;

            case 'M': {
                String type = readType();

                // hessian/3386        ----------------------------------------------------------- 原因在这里
                if ("".equals(type)) {
                    Deserializer reader;
                    reader = _serializerFactory.getDeserializer(cl);

                    return reader.readMap(this);
                }
                else {
                    Deserializer reader;
                    reader = _serializerFactory.getObjectDeserializer(type);

                    return reader.readMap(this);
                }
            }

            case 'V': {
                String type = readType();
                int length = readLength();

                Deserializer reader;
                reader = _serializerFactory.getObjectDeserializer(type);

                if (cl != reader.getType() && cl.isAssignableFrom(reader.getType()))
                    return reader.readList(this, length);

                reader = _serializerFactory.getDeserializer(cl);

                Object v = reader.readList(this, length);

                return v;
            }

            case 'R': {
                int ref = parseInt();

                return _refs.get(ref);
            }

            case 'r': {
                String type = readType();
                String url = readString();

                return resolveRemote(type, url);
            }
        }

        _peek = tag;

        // hessian/332i vs hessian/3406
        //return readObject();

        Object value = _serializerFactory.getDeserializer(cl).readObject(this);

        return value;
    }

这个type 在map传输过程中,不知为何会是"",导致不能反序列化,但是继承HashMap的实现类,或者实现序列化接口的类,均不会出现此问题
找了很久,发现一篇2009年的博客,有具体信息描述
链接:http://www.blogjava.net/BlueDavy/archive/2009/08/06/290003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北漂的菜小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值