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);
*/
}
Check out the as3 doc of ByteArray
readUnicodeString()
andreadString()
should bereadUTFBytes()
I dont think as3 has LongIntegers, but
readDouble()
should work for that as far as I know.