我正在将函数从 JavaScript 转换为 AS3,我正在尝试映射Uint8Array和ByteArray之间的调用,我注意到其中一些调用是不同的。
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);
*/
}