@awayjs/core
Version:
AwayJS core classes
172 lines (171 loc) • 7.67 kB
JavaScript
var Byte32Array = /** @class */ (function () {
function Byte32Array(byteLength) {
if (byteLength === void 0) { byteLength = 0; }
this._maxLength = -1;
this.bytePosition = 0;
this.byteLength = -1;
this._ensureSpace(byteLength);
}
Object.defineProperty(Byte32Array.prototype, "arrayBuffer", {
get: function () {
return this._arrayBuffer;
},
set: function (value) {
//reset position
this.bytePosition = 0;
//clear length
this.byteLength = -1;
this._maxLength = -1;
//calc new length
this._ensureSpace(value.byteLength);
//write new buffer
this._int32Array.set(new Int32Array(value));
},
enumerable: false,
configurable: true
});
Byte32Array.prototype.readByte32Array = function (byte32Array, byteLength) {
if (byteLength === void 0) { byteLength = -1; }
if (byteLength == -1)
byteLength = byte32Array.byteLength - byte32Array.bytePosition;
byte32Array.writeInt32Array(this._int32Array.subarray(this.bytePosition / 4, (this.bytePosition + byteLength) / 4));
this.bytePosition += byteLength;
};
Byte32Array.prototype.writeByte32Array = function (byte32Array) {
byte32Array.bytePosition = 0;
byte32Array.readByte32Array(this, byte32Array.byteLength);
};
Byte32Array.prototype.writeFloat32Array = function (float32Array) {
this._ensureSpace(this.bytePosition + float32Array.length * 4);
this._float32Array.set(float32Array, this.bytePosition / 4);
this.bytePosition += float32Array.length * 4;
};
Byte32Array.prototype.writeInt32Array = function (int32Array) {
this._ensureSpace(this.bytePosition + int32Array.length * 4);
this._uint32Array.set(int32Array, this.bytePosition / 4);
this.bytePosition += int32Array.length * 4;
};
Byte32Array.prototype.writeUint32Array = function (uint32Array) {
this._ensureSpace(this.bytePosition + uint32Array.length * 4);
this._uint32Array.set(uint32Array, this.bytePosition / 4);
this.bytePosition += uint32Array.length * 4;
};
Byte32Array.prototype.readFloat32Array = function (float32Array) {
float32Array.set(this._float32Array.subarray(this.bytePosition / 4, this.bytePosition / 4 + float32Array.length));
};
Byte32Array.prototype.readInt32Array = function (int32Array) {
int32Array.set(this._int32Array.subarray(this.bytePosition / 4, this.bytePosition / 4 + int32Array.length));
};
Byte32Array.prototype.readUint32Array = function (uint32Array) {
uint32Array.set(this._uint32Array.subarray(this.bytePosition / 4, this.bytePosition / 4 + uint32Array.length));
};
Byte32Array.prototype.getBytesAvailable = function () {
return this.byteLength - this.bytePosition;
};
Byte32Array.prototype.readUTFBytes = function (len) {
var value = '';
var max = this.bytePosition + len;
var data = new DataView(this._arrayBuffer);
// utf8-encode
while (this.bytePosition < max) {
var c = data.getUint8(this.bytePosition++);
if (c < 0x80) {
if (c == 0)
break;
value += String.fromCharCode(c);
}
else if (c < 0xE0) {
value += String.fromCharCode(((c & 0x3F) << 6)
| (data.getUint8(this.bytePosition++) & 0x7F));
}
else if (c < 0xF0) {
var c2 = data.getUint8(this.bytePosition++);
value += String.fromCharCode(((c & 0x1F) << 12)
| ((c2 & 0x7F) << 6)
| (data.getUint8(this.bytePosition++) & 0x7F));
}
else {
var c2 = data.getUint8(this.bytePosition++);
var c3 = data.getUint8(this.bytePosition++);
value += String.fromCharCode(((c & 0x0F) << 18)
| ((c2 & 0x7F) << 12)
| ((c3 << 6) & 0x7F)
| (data.getUint8(this.bytePosition++) & 0x7F));
}
}
return value;
};
Byte32Array.prototype.writeUTFBytes = function (s) {
var escstr = encodeURIComponent(s);
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(parseInt('0x' + p1));
});
if (binstr.length % 4) {
var padding = 4 - (binstr.length % 4);
for (var i = 0; i < padding; i++)
binstr += ' ';
}
this._ensureSpace(this.bytePosition + 4 + binstr.length);
this.writeInt(binstr.length);
for (var i = 0; i < binstr.length; i += 4)
this.writeUnsignedInt((binstr.charCodeAt(i + 3) << 24)
| (binstr.charCodeAt(i + 2) << 16)
| (binstr.charCodeAt(i + 1) << 8)
| binstr.charCodeAt(i));
return binstr.length;
};
Byte32Array.prototype.readInt = function () {
if (this.bytePosition > this.byteLength + 4)
throw 'ByteArray out of bounds read. Position=' + this.bytePosition + ', Length=' + this.byteLength;
var pa = this.bytePosition >> 2;
this.bytePosition += 4;
return this._int32Array[pa];
};
Byte32Array.prototype.writeUnsignedInt = function (b) {
this._ensureSpace(this.bytePosition + 4);
this._uint32Array[this.bytePosition >> 2] = (~~b) & 0xffffffff; // ~~ is cast to int in js...
this.bytePosition += 4;
};
Byte32Array.prototype.writeInt = function (b) {
this._ensureSpace(this.bytePosition + 4);
this._int32Array[this.bytePosition >> 2] = (~~b); // ~~ is cast to int in js...
this.bytePosition += 4;
};
Byte32Array.prototype.readUnsignedInt = function () {
if (this.bytePosition > this.byteLength + 4)
throw 'ByteArray out of bounds read. Position=' + this.bytePosition + ', Length=' + this.byteLength;
var pa = this.bytePosition >> 2;
this.bytePosition += 4;
return this._uint32Array[pa];
};
Byte32Array.prototype.writeFloat = function (b) {
this._ensureSpace(this.bytePosition + 4);
this._float32Array[this.bytePosition >> 2] = b;
this.bytePosition += 4;
};
Byte32Array.prototype.readFloat = function () {
if (this.bytePosition > this.byteLength + 4)
throw 'ByteArray out of bounds read. Positon=' + this.bytePosition + ', Length=' + this.byteLength;
var pa = this.bytePosition >> 2;
this.bytePosition += 4;
return this._float32Array[pa];
};
Byte32Array.prototype._ensureSpace = function (length) {
if (this.byteLength < length) {
this.byteLength = length;
if (this._maxLength < length) { //add additional length in minimum 255-byte increments
this._maxLength = Math.max((length + 255) & (~255), 4);
var newArrayBuffer = new ArrayBuffer(this._maxLength);
var newInt32Array = new Int32Array(newArrayBuffer);
if (this._int32Array)
newInt32Array.set(this._int32Array); // memcpy
this._uint32Array = new Uint32Array(newArrayBuffer);
this._float32Array = new Float32Array(newArrayBuffer);
this._int32Array = newInt32Array;
this._arrayBuffer = newArrayBuffer;
}
}
};
return Byte32Array;
}());
export { Byte32Array };