From: Pawel Andruszkiewicz
Date: Wed, 9 Dec 2015 14:40:44 +0000 (+0100)
Subject: [Filesystem] Various fixes required by cordova.
X-Git-Tag: submit/tizen_mobile/20151215.080542^2~3^2
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fee0942d63adb668208b132d556d583418f30035;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git
[Filesystem] Various fixes required by cordova.
Update position after each read/write operation (adapted from http://165.213.149.170/gerrit/#/c/90736/).
Do not UTF8 encode/decode binary data.
[Verification] TCT pass rate (r20): 100% (289/289/0/0/0).
Cordova pass rate: 140 specs, 0 failures, 1 pending spec.
Change-Id: I9b065de5bd20233b2530f25fcca650832e19695f
Signed-off-by: Pawel Andruszkiewicz
---
diff --git a/src/filesystem/js/base64.js b/src/filesystem/js/base64.js
index cdbd5185..23248313 100755
--- a/src/filesystem/js/base64.js
+++ b/src/filesystem/js/base64.js
@@ -16,18 +16,24 @@
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);
@@ -47,8 +53,16 @@ var Base64 = {
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;
@@ -66,19 +80,17 @@ var Base64 = {
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) {
diff --git a/src/filesystem/js/file.js b/src/filesystem/js/file.js
index 624ab37f..8ff33d96 100644
--- a/src/filesystem/js/file.js
+++ b/src/filesystem/js/file.js
@@ -339,7 +339,7 @@ function readAsText() {
}
encoded = native_.getResultObject(result);
if (encoded.length) {
- str += Base64.decode(encoded);
+ str += Base64.decodeString(encoded);
data.offset += data.length;
}
} while (encoded.length);
diff --git a/src/filesystem/js/file_stream.js b/src/filesystem/js/file_stream.js
index 8ce1a9ad..94b3ed21 100644
--- a/src/filesystem/js/file_stream.js
+++ b/src/filesystem/js/file_stream.js
@@ -14,15 +14,20 @@
* 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() {
@@ -30,13 +35,19 @@ function FileStream(data, mode, encoding) {
},
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,
@@ -127,9 +138,8 @@ function read() {
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() {
@@ -167,14 +177,8 @@ function readBytes() {
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() {
@@ -253,7 +257,7 @@ function write() {
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);
@@ -261,7 +265,9 @@ function write() {
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() {
@@ -290,7 +296,7 @@ function writeBytes() {
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);
@@ -298,6 +304,9 @@ function writeBytes() {
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() {