What are the equivalent calls of these JavaScript functions in an AS3 ByteArray?

398 views Asked by At

I'm converting function from JavaScript to AS3 and I'm trying to map the calls between Uint8Array and ByteArray and I notice that a few of the calls are different.

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

List of calls in Javascript not found in AS3:

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

Update:
It looks like the author is using Uint8Array but also creating a fallback class where Uint8Array is not supported. I'll have to update this question when I can figure out what is going on.

Update 2:
So a Uint8Array is passed in and then that Uint8Array is passed into a wrapper class:

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

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

earlier...

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;
};

Now the question is slightly different.

Update 3:

Some more info in related post. Here is my AS3 conversion attempt:

    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);
        */
    }
2

There are 2 answers

2
Manuel Otto On

Check out the as3 doc of ByteArray

readUnicodeString() and readString() should be readUTFBytes()

I dont think as3 has LongIntegers, but readDouble() should work for that as far as I know.

3
Organis On

readUnicodeString

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");
}

readString

// Presumably reads a non-UTF (probably an ASCII) string.

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

readLongLong

In AS3 there are two integer types, int and uint, 4 bytes both, so probaly it will be something like

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

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

    return result;
}

read

// I still think that original code does simpler things than it looks.

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;
}

tell

Need more information.