radio/libjs/wType/blob.js

96 lines
2.8 KiB
JavaScript

"use strict";
var Object = require("./object");
var Blob = Object.inherit({
"className": "Blob",
"constructor": function(/*ArrayBuffer*/data) {
Object.fn.constructor.call(this);
data = data || new ArrayBuffer(0);
this._data = data;
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return this.size() == other.size(); //TODO let's pretend one shall never wish to compare blobs)
},
"+=": function(other) {
if (this.getType() !== other.getType()) {
throw new Error("An attempt to add and assign an " + other.className + " to " + this.className);
}
var newData = new ArrayBuffer(this._data.byteLength + other._data.byteLength);
var newView = new Uint8Array(newData);
var thisView = new Uint8Array(this._data);
var otherView = new Uint8Array(other._data);
newView.set(thisView, 0);
newView.set(otherView, this._data.byteLength);
this._data = newData;
},
"+": function(other) {
if (this.getType() !== other.getType()) {
throw new Error("An attempt to add an " + other.className + " to " + this.className);
}
var newData = new ArrayBuffer(this._data.byteLength + other._data.byteLength);
var newView = new Uint8Array(newData);
var thisView = new Uint8Array(this._data);
var otherView = new Uint8Array(other._data);
newView.set(thisView, 0);
newView.set(otherView, this._data.byteLength);
return new Blob(newData);
},
"base64": function() {
var arr = new Uint8Array(this._data);
var bin = "";
for (var i = 0; i < arr.length; ++i) {
bin += String.fromCharCode(arr[i]);
}
return btoa(bin);
},
"clone": function() {
var clone = new Blob(this._data.slice(0));
return clone;
},
"deserialize": function(ba) {
var length = ba.pop32();
this._data = new ArrayBuffer(length);
var view = new Uint8Array(this._data);
for (var i = 0; i < length; ++i) {
view[i] = ba.pop8();
}
},
"length": function() {
return this._data.byteLength;
},
"serialize": function(ba) {
ba.push32(this._data.byteLength);
var view = new Uint8Array(this._data);
for (var i = 0; i < view.length; ++i) {
ba.push8(view[i]);
}
},
"size": function() {
return this._data.byteLength + 4;
},
"toString": function() {
return "File <" + this._data.byteLength + ">";
},
"valueOf": function() {
return this._data;
}
});
module.exports = Blob;