var Base64 = {
_b64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
+ encodeString: function(data) {
+ data = this._utf8_encode(data);
+ var input = [];
+ for (var i = 0; i < data.length; ++i) {
+ input.push(data.charCodeAt(i));
+ }
+ return this.encode(input);
+ },
encode: function(data) {
var output = '';
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
- data = this._utf8_encode(data);
-
while (i < data.length) {
- chr1 = data.charCodeAt(i++);
- chr2 = data.charCodeAt(i++);
- chr3 = data.charCodeAt(i++);
+ chr1 = data[i++];
+ chr2 = data[i++];
+ chr3 = data[i++];
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
return output;
},
- decode: function(data) {
+ decodeString: function(data) {
+ data = this.decode(data);
var output = '';
+ for (var i = 0; i < data.length; ++i) {
+ output += String.fromCharCode(data[i]);
+ }
+ return this._utf8_decode(output);
+ },
+ decode: function(data) {
+ var output = [];
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
- output += String.fromCharCode(chr1);
+ output.push(chr1);
if (enc3 !== 64) {
- output += String.fromCharCode(chr2);
+ output.push(chr2);
}
if (enc4 !== 64) {
- output += String.fromCharCode(chr3);
+ output.push(chr3);
}
}
- output = this._utf8_decode(output);
-
return output;
},
_utf8_encode: function(str) {
* limitations under the License.
*/
+var can_change_size = false;
+
function FileStream(data, mode, encoding) {
var _totalBytes = data.fileSize || 0;
var _position = mode === 'a' ? _totalBytes : 0;
Object.defineProperties(this, {
eof: {
- value: false,
- enumerable: true,
- writable: false
+ get: function() {
+ return _totalBytes < _position;
+ },
+ set: function(v) {
+ },
+ enumerable: true
},
position: {
get: function() {
},
set: function(v) {
_position = Math.max(0, v);
+ if (can_change_size) {
+ _totalBytes = Math.max(_position, _totalBytes);
+ }
},
enumerable: true
},
bytesAvailable: {
- value: this.eof ? -1 : Math.max(0, _totalBytes - _position),
- enumerable: true,
- writable: false
+ get: function() {
+ return this.eof ? -1 : Math.max(0, _totalBytes - _position);
+ },
+ set: function(v) {
+ },
+ enumerable: true
},
_mode: {
value: mode,
throw new WebAPIException(WebAPIException.IO_ERR, 'Could not read');
}
var encoded = native_.getResultObject(result);
- var decoded = Base64.decode(encoded);
- return decoded;
+ return Base64.decodeString(encoded);
};
FileStream.prototype.read = function() {
throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Could not read');
}
var encoded = native_.getResultObject(result);
- var decoded = Base64.decode(encoded);
- var bytes = [];
-
- for (var i = 0; i < decoded.length; ++i) {
- bytes.push(decoded.charCodeAt(i));
- }
- return bytes;
+ return Base64.decode(encoded);
};
FileStream.prototype.readBytes = function() {
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: Base64.encode(args.stringData)
+ data: Base64.encodeString(args.stringData)
};
var result = native_.callSync('File_writeSync', data);
if (native_.isFailure(result)) {
throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write');
}
- this.position = args.stringData.length;
+ can_change_size = true;
+ this.position = this.position + args.stringData.length;
+ can_change_size = false;
};
FileStream.prototype.write = function() {
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: Base64.encode(String.fromCharCode.apply(String, args.byteData))
+ data: Base64.encode(args.byteData)
};
var result = native_.callSync('File_writeSync', data);
if (native_.isFailure(result)) {
throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write');
}
+ can_change_size = true;
+ this.position = this.position + args.byteData.length;
+ can_change_size = false;
};
FileStream.prototype.writeBytes = function() {