[Filesystem] Fixed implementation of readAsText().
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 8 Jun 2015 12:14:42 +0000 (14:14 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 8 Jun 2015 12:14:42 +0000 (14:14 +0200)
Read operation is now performed asynchronously on main thread.
Recursive call removed.

[Verification] Pass rate: 289/289.
Warning: Due to nature of implementation and current architecture, the test
File_readAsText_with_encoding may block. If there are large files present in
/opt/usr/media/Documents/ folder (i.e. created by Download test suite) the test
will not be able to finish in time.

Change-Id: I031ac1d7c6d3ab223a0f93a0788938d5067b8a8c
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/filesystem/js/file.js

index 34b5f76a582242bfd47b53a2a748514e366183bc..1e031ab38c305899cb6159ad13d10c55cc898af5 100755 (executable)
@@ -311,26 +311,30 @@ File.prototype.readAsText = function(onsuccess, onerror, encoding) {
     encoding: args.encoding
   };
 
-  var result, encoded, str = '';
-
   function readFile() {
-    result = native_.callSync('File_readSync', data);
-    if (native_.isFailure(result)) {
-      native_.callIfPossible(args.onerror, native_.getErrorObject(result));
-      return;
-    }
-    encoded = native_.getResultObject(result);
-    if (!encoded.length) {
-      setTimeout(function() {
-        native_.callIfPossible(args.onsuccess, Base64.decode(str));
-      }, 0);
-    } else {
-      str += encoded;
-      data.offset += data.length;
-      readFile();
-    }
+    var result, encoded, str = '';
+
+    do {
+      result = native_.callSync('File_readSync', data);
+      if (native_.isFailure(result)) {
+        setTimeout(function() {
+          native_.callIfPossible(args.onerror, native_.getErrorObject(result));
+        }, 0);
+        return;
+      }
+      encoded = native_.getResultObject(result);
+      if (encoded.length) {
+        str += Base64.decode(encoded);
+        data.offset += data.length;
+      }
+    } while (encoded.length);
+
+    setTimeout(function() {
+      native_.callIfPossible(args.onsuccess, str);
+    }, 0);
   }
-  readFile();
+
+  setTimeout(readFile, 0);
 };
 
 File.prototype.copyTo = function(originFilePath, destinationFilePath, overwrite, onsuccess, onerror) {