我正在将功能从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:
相关post中的更多信息。这是我的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);
*/
}
答案 0 :(得分: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 强>
//推测读取非UTF(可能是ASCII)字符串。
function readString(source:ByteArray, length:uint):String
{
return source.readMultiByte(length, "ascii");
}
<强> readLongLong 强>
在AS3中有两种整数类型, int 和 uint ,两个都是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;
}
<强>告诉强>
需要更多信息。
答案 1 :(得分:1)
readUnicodeString()
和readString()
应为readUTFBytes()
我不认为as3有LongIntegers,但就我所知,readDouble()
应该可以正常工作。