[Filesystem] Various fixes required by cordova.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 9 Dec 2015 14:40:44 +0000 (15:40 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 9 Dec 2015 14:40:44 +0000 (15:40 +0100)
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 <p.andruszkie@samsung.com>
src/filesystem/js/base64.js
src/filesystem/js/file.js
src/filesystem/js/file_stream.js

index cdbd5185884edb638e10371ee164178031186f43..23248313270798205dd244cbcdaa0ba8bccca679 100755 (executable)
 
 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) {
index 624ab37ff64eef470501bad81d4a0bfd41a1d192..8ff33d967510219284b15df1e2a3982c661c2257 100644 (file)
@@ -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);
index 8ce1a9ad5c4d92ee0ee876d9fc41363bb8315891..94b3ed211ba41ed87e451d0178f112b0c8179be1 100644 (file)
  *    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() {