BlazeDS自定义序列化导致RangeError

发布于 2024-10-17 08:05:52 字数 3740 浏览 3 评论 0原文

我正在使用 BlazeDS 在 Java 和 Flash/Flex 之间进行通信。一切工作正常,除了 Java Null Integer 在 Flex 端变成 0 之外。

为了处理将 Java Null Integer 传输到 Flash/Flex int 的问题,我实现了自定义序列化,它在 Java 端工作并使用负值来表示 Null 值。

实现后,我得到一个

RangeError: Error #2006: Der angegebene Index liegt außerhalb des zulässigen Bereichs.
(in english: the index is out of range)
                at ObjectInput/readObject()
                at mx.collections::ArrayList/readExternal()[E:\dev\4.x\frameworks\projects\framework\src\mx\collections\ArrayList.as:586]
                at mx.collections::ArrayCollection/readExternal()[E:\dev\4.x\frameworks\projects\framework\src\mx\collections\ArrayCollection.as:147]
                at ObjectInput/readObject()
                at mx.messaging.messages::AbstractMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AbstractMessage.as:486]
                at mx.messaging.messages::AsyncMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AsyncMessage.as:170]
                at mx.messaging.messages::AcknowledgeMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AcknowledgeMessage.as:95]

异常,在反序列化 Java 结果时 Flex 端发生。 这是一个复杂对象的列表,其中包含带有自定义序列化的特殊类。因为在我添加自定义序列化之前没有出现这样的问题,所以我猜它一定属于问题,但我不知道是什么触发了异常。

这是具有自定义序列化的对象的代码:

package crux.domain.dto;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

public class NullAbleID implements Serializable, Externalizable {

    private static final long serialVersionUID = 788620879056753289L;

    private Integer id;

    public NullAbleID() {
        super();
        this.id = null;
    }

    public NullAbleID(final Integer id) {
        this.id = id;
    }

    getter, setter for ID and hashCode and equals

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {        
        out.writeObject(this.nullToNegative(this.id));                
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        this.id = this.negativeToNull(in.readInt());
    }

    private int nullToNegative(Integer id) {
        if (id == null) {
            return -1;
        } else {        
            return id.intValue();
        }
    }

    private Integer negativeToNull(int flashId) {
        if (flashId < 0) {
            return null;
        } else {
            return Integer.valueOf(flashId);
        }
    }
}

Flex:两个类,因为我们使用 Gas3 Granite Data Service 代码生成:

/**
* Generated by Gas3 v2.1.0 (Granite Data Services).
*
*/
package crux.domain.dto {

    import flash.utils.IExternalizable;

    [Bindable]
    public class NullAbleIDBase {

        public function NullAbleIDBase() {}


        private var _id:Number;

        public function set id(value:Number):void {
            _id = value;
        }
        public function get id():Number {
            return _id;
        }
    }
}

带有读写外部的 Flex 子类

package crux.domain.dto {

      import flash.utils.IDataInput;
      import flash.utils.IDataOutput;
      import flash.utils.IExternalizable;

    [Bindable]
    [RemoteClass(alias="crux.domain.dto.NullAbleID")]
    public class NullAbleID extends NullAbleIDBase implements IExternalizable{

            public function readExternal(input:IDataInput):void {
                  id = input.readInt();
            }

            public function writeExternal(output:IDataOutput):void {
                  output.writeInt(id);
            }
    }
}

我在这个问题上花了几个小时,但我不知道是什么问题是。 您看到异常的原因了吗?

I am using BlazeDS to communicate between Java and Flash/Flex. And everything works fine, except that Java Null Integer becomes 0 on Flex side.

To handle the problem with transferring a Java Null Integer to an Flash/Flex int, I have implement a custom serialization, which works on the Java side and uses negative values to express Null values.

After implementing that I get an

RangeError: Error #2006: Der angegebene Index liegt außerhalb des zulässigen Bereichs.
(in english: the index is out of range)
                at ObjectInput/readObject()
                at mx.collections::ArrayList/readExternal()[E:\dev\4.x\frameworks\projects\framework\src\mx\collections\ArrayList.as:586]
                at mx.collections::ArrayCollection/readExternal()[E:\dev\4.x\frameworks\projects\framework\src\mx\collections\ArrayCollection.as:147]
                at ObjectInput/readObject()
                at mx.messaging.messages::AbstractMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AbstractMessage.as:486]
                at mx.messaging.messages::AsyncMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AsyncMessage.as:170]
                at mx.messaging.messages::AcknowledgeMessage/readExternal()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\messages\AcknowledgeMessage.as:95]

The exception occures on the Flex side while deserializing the Java Result.
Which is an list of complex objects which contains this special class with the custom serialization. Because there was no such problem until I added the custom serialization, I guess it must belong to the problem, but i have no clue what triggers the exception.

This is the code of the object with the custom serialization:

package crux.domain.dto;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

public class NullAbleID implements Serializable, Externalizable {

    private static final long serialVersionUID = 788620879056753289L;

    private Integer id;

    public NullAbleID() {
        super();
        this.id = null;
    }

    public NullAbleID(final Integer id) {
        this.id = id;
    }

    getter, setter for ID and hashCode and equals

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {        
        out.writeObject(this.nullToNegative(this.id));                
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        this.id = this.negativeToNull(in.readInt());
    }

    private int nullToNegative(Integer id) {
        if (id == null) {
            return -1;
        } else {        
            return id.intValue();
        }
    }

    private Integer negativeToNull(int flashId) {
        if (flashId < 0) {
            return null;
        } else {
            return Integer.valueOf(flashId);
        }
    }
}

Flex: two classes, because we use Gas3 Granite Data Service code generation:

/**
* Generated by Gas3 v2.1.0 (Granite Data Services).
*
*/
package crux.domain.dto {

    import flash.utils.IExternalizable;

    [Bindable]
    public class NullAbleIDBase {

        public function NullAbleIDBase() {}


        private var _id:Number;

        public function set id(value:Number):void {
            _id = value;
        }
        public function get id():Number {
            return _id;
        }
    }
}

Flex sub class with read and write external

package crux.domain.dto {

      import flash.utils.IDataInput;
      import flash.utils.IDataOutput;
      import flash.utils.IExternalizable;

    [Bindable]
    [RemoteClass(alias="crux.domain.dto.NullAbleID")]
    public class NullAbleID extends NullAbleIDBase implements IExternalizable{

            public function readExternal(input:IDataInput):void {
                  id = input.readInt();
            }

            public function writeExternal(output:IDataOutput):void {
                  output.writeInt(id);
            }
    }
}

I have spend several hours on this problem, but I have no idea what the problem is.
Do you see the cause for the exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-10-24 08:05:52

不确定这是问题的原因,因为我不了解 BlazeDS,但是 NullAbleID 类的方法 readExternalwriteExternal 不是symetric :在 writeExternal 中写入 Integer 类型的对象,并在 readExternal 中读取 int 类型。

Not sure it's the cause of the problem, because I don't know BlazeDS, but the methods readExternal and writeExternal of your NullAbleID class are not symetric : you write an object of type Integer in writeExternal, and you read an int in readExternal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文