From 26f624eb1dacf112729580f45fd21858ac489ca7 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Mon, 8 Jun 2015 14:14:42 +0200
Subject: [PATCH] [Filesystem] Fixed implementation of readAsText().
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
---
src/filesystem/js/file.js | 40 +++++++++++++++++++++------------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/src/filesystem/js/file.js b/src/filesystem/js/file.js
index 34b5f76a..1e031ab3 100755
--- a/src/filesystem/js/file.js
+++ b/src/filesystem/js/file.js
@@ -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) {
--
2.34.1