2

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);
        */
    }
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • While I'm at answer, could you please also explain what these methods are expected to return? – Organis Aug 29 '17 at 00:06
  • I updated the question with more details. – 1.21 gigawatts Aug 29 '17 at 01:12
  • Do you understand what **read** method does? It looks like author wanted to create something complicated yet abandoned it halfway, so it actually just reads **Math.abs(length)** bytes into regular **Array**. – Organis Aug 29 '17 at 10:15
  • I don't understand what read does. I tried to use readByte, readUnsignedByte and so on on a bytearray and the position increments by one but I don't think it's working. I've included a link to a related question that I posted the links to the source. – 1.21 gigawatts Aug 29 '17 at 12:10
  • Updated my answer. – Organis Aug 29 '17 at 23:23

2 Answers2

2

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.

Organis
  • 7,243
  • 2
  • 12
  • 14
  • I discovered something. The author created a class (does not look extended) to deal with buffers in JavaScript. It may be all custom. So I cannot say that these methods are part of Uint8Array. That may be why I could not find information on them. These will help in converting though. I may withdraw question if this class is not related to Uint8Array. – 1.21 gigawatts Aug 29 '17 at 00:27
  • It looks like the author made a fallback class for older browsers. I don't know which methods are part of Uint8Buffer and which are custom. But this information is helpful and may be enough to get going. It looks like he is passing in Uint8Array though. – 1.21 gigawatts Aug 29 '17 at 00:46
  • @1.21gigawatts That's what confused me as any official documentation in Uint8Array does not have anything on the methods you mentioned. – Organis Aug 29 '17 at 00:50
1

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.

Manuel Otto
  • 6,410
  • 1
  • 18
  • 25
  • I discovered something. The author created a class (does not look extended) to deal with buffers in JavaScript. It may be all custom. So I cannot say that these methods are part of Uint8Array. That may be why I could not find information on them. These will help in converting though. I may withdraw question if this class is not related to Uint8Array. – 1.21 gigawatts Aug 29 '17 at 00:28
  • It looks like the author made a fallback class for older browsers. I do not know which methods are part of Uint8Buffer and which are custom. But this information is helpful and may be enough to get going. – 1.21 gigawatts Aug 29 '17 at 00:43