2

我正在将函数从 JavaScript 转换为 AS3,我正在尝试映射Uint8ArrayByteArray之间的调用,我注意到其中一些调用是不同的。

var data = new Uint8Array() // Javascript
var bytearray = new ByteArray(); // AS3

在 AS3 中找不到的 Javascript 调用列表:

readUnicodeString()
readString()
readLongLong()
read()
tell()

更新:
看起来作者正在使用 Uint8Array 但也创建了一个不支持 Uint8Array 的后备类。当我弄清楚发生了什么时,我将不得不更新这个问题。

更新 2:
因此传入了 Uint8Array,然后将 Uint8Array 传递给了包装类:

Image = function (data) {
    this.file = new File(data);
    ...
}

var image = new Image(new Uint8Array(buffer));

早...

File.prototype.readString = function(length) {
    return String.fromCharCode.apply(null, this.read(length)).replace(/\u0000/g, "");
};

File.prototype.readUnicodeString = function(length) {
    if (length == null) {
        length = null;
    }
    length || (length = this.readInt());
    return iconv.decode(new Buffer(this.read(length * 2)), 'utf-16be').replace(/\u0000/g, "");
};

File.prototype.read = function(length) {
    var i, j, ref, results;
    results = [];
    for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
        results.push(this.data[this.pos++]);
    }
    return results;
};

现在问题略有不同。

更新 3:

相关帖子中的更多信息。这是我的 AS3 转换尝试:

    public var useJSCalls:Boolean = true;

    public function read(length):Array {
        var i:int;
        var j:int;
        var ref;
        var results:Array;

        results = [];
        var cur:int = file.position;
        //var val = file.readUTFBytes(length);
        //file.position = cur;

        for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
            results.push(file.readUnsignedByte());
            //results.push(file.readByte());
            //results.push(file.readByte());
            //results.push(file.position++);
            //results.push(data[position++]);
        }


        return results;
    }

    public function readString(length:int = -1):String {
        if (useJSCalls) {
            var val = read(length);
            val = String.fromCharCode(val);
            //val = String.fromCharCode(val).replace(/\u0000/g, "");
            return val;
        }

        if (length==-1) {
            length = 1;
        }

        //var value = file.readMultiByte(length, "utf-8");
        var value = file.readMultiByte(length, "utf-8");

        return value;
    }

    public function readUnicodeString(length:int = -1):String {
        var currentPosition:uint = file.position;

        if (useJSCalls) {
            if (length == -1) {
                length = file.readInt();
            }

            //return iconv.decode(new Buffer(this.read(length * 2)), 'utf-16be').replace(/\u0000/g, "");
            var array = read(length * 2);
            var value = String.fromCharCode(array);
            value = value.replace(/\u0000/g, "");
            var newPosition:uint = file.position;
            file.position = currentPosition;

            var value2 = file.readMultiByte(length, "utf-8");

            //value = file.readUTFBytes(int(length));
            file.position = newPosition;

            return value;
        }

        return value;
        /*
        if (length==-1) {
            return file.readInt() + "";
        }

        return file.readUTFBytes(length);
        */
    }
4

2 回答 2

2

读取UnicodeString

function readUnicodeString(source:ByteArray, length:* = null):String
{
    if (isNaN(length)) length = source.readUnsignedInt();
    else if (length < 1) length = source.readUnsignedInt();

    return source.readMultiByte(length, "utf-16be");
}

读取字符串

// 大概读取一个非 UTF(可能是 ASCII)字符串。

function readString(source:ByteArray, length:uint):String
{
    return source.readMultiByte(length, "ascii");
}

读长长

在 AS3 中有两种整数类型,intuint,都是 4 个字节,所以很可能它会像

function readLongLong(source:ByteArray):Number
{
    var result:Number = 0;

    result += source.readUnsignedInt();
    result += source.readUnsignedInt() << 32;

    return result;
}

// 我仍然认为原始代码做的事情比看起来更简单。

function read(source:ByteArray, length:int):void
{
    var result:Array = new Array;

    for (var i:int = Math.abs(length); i > 0; i--)
        result.push(source.readUnsignedByte());

    return result;
}

告诉

需要更多信息。

于 2017-08-29T00:24:37.300 回答
1

查看ByteArray 的 as3 文档

readUnicodeString()并且readString()应该是readUTFBytes()

我不认为 as3 有 LongIntegers,但readDouble()据我所知应该为此工作。

于 2017-08-29T00:09:46.887 回答