Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7273DEEBF for ; Fri, 15 Mar 2013 20:01:17 +0000 (UTC) Received: (qmail 77034 invoked by uid 500); 15 Mar 2013 20:01:17 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 76985 invoked by uid 500); 15 Mar 2013 20:01:16 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 76978 invoked by uid 99); 15 Mar 2013 20:01:16 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 Mar 2013 20:01:16 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1A5CD3500F; Fri, 15 Mar 2013 20:01:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: agrieve@apache.org To: commits@cordova.apache.org X-Mailer: ASF-Git Admin Mailer Subject: android commit: [CB-2546] Moved read calls to a background thread. Message-Id: <20130315200116.1A5CD3500F@tyr.zones.apache.org> Date: Fri, 15 Mar 2013 20:01:15 +0000 (UTC) Updated Branches: refs/heads/master ac2969c3f -> d25b73f47 [CB-2546] Moved read calls to a background thread. Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/d25b73f4 Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/d25b73f4 Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/d25b73f4 Branch: refs/heads/master Commit: d25b73f47d4503993d2f910789020515501e6ff6 Parents: ac2969c Author: Max Woghiren Authored: Fri Mar 15 14:23:39 2013 -0400 Committer: Andrew Grieve Committed: Fri Mar 15 16:01:02 2013 -0400 ---------------------------------------------------------------------- framework/src/org/apache/cordova/FileUtils.java | 120 ++++++++++++------ 1 files changed, 81 insertions(+), 39 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-android/blob/d25b73f4/framework/src/org/apache/cordova/FileUtils.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index afcabfb..a137193 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -122,8 +122,7 @@ public class FileUtils extends CordovaPlugin { end = args.getInt(3); } - String s = this.readAsText(args.getString(0), args.getString(1), start, end); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); + this.readAsText(args.getString(0), args.getString(1), start, end, callbackContext); } else if (action.equals("readAsDataURL")) { int start = 0; @@ -135,8 +134,7 @@ public class FileUtils extends CordovaPlugin { end = args.getInt(2); } - String s = this.readAsDataURL(args.getString(0), start, end); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); + this.readAsDataURL(args.getString(0), start, end, callbackContext); } else if (action.equals("readAsArrayBuffer")) { int start = 0; @@ -148,8 +146,7 @@ public class FileUtils extends CordovaPlugin { end = args.getInt(2); } - byte[] s = this.readAsBinary(args.getString(0), start, end); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); + this.readAsBinary(args.getString(0), start, end, callbackContext); } else if (action.equals("write")) { long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2)); @@ -959,45 +956,81 @@ public class FileUtils extends CordovaPlugin { //-------------------------------------------------------------------------- /** - * Read content of text file. + * Read the contents of a file as text. + * This is done in a background thread; the result is sent to the callback. * - * @param filename The name of the file. - * @param encoding The encoding to return contents as. Typical value is UTF-8. - * (see http://www.iana.org/assignments/character-sets) - * @param start Start position in the file. - * @param end End position to stop at (exclusive). - * @return Contents of file. - * @throws FileNotFoundException, IOException + * @param filename The name of the file. + * @param encoding The encoding to return contents as. Typical value is UTF-8. (see http://www.iana.org/assignments/character-sets) + * @param start Start position in the file. + * @param end End position to stop at (exclusive). + * @return Contents of file. */ - public String readAsText(String filename, String encoding, int start, int end) throws FileNotFoundException, IOException { - int diff = end - start; - byte[] bytes = new byte[1000]; - BufferedInputStream bis = new BufferedInputStream(FileHelper.getInputStreamFromUriString(filename, cordova), 1024); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int numRead = 0; + public void readAsText(final String filename, final String encoding, final int start, final int end, final CallbackContext callbackContext) { + Runnable readAsTextRunnable = new Runnable() { + public void run() { + try { + int diff = end - start; + byte[] bytes = new byte[1000]; + BufferedInputStream bis = new BufferedInputStream(FileHelper.getInputStreamFromUriString(filename, cordova), 1024); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int numRead = 0; + + if (start > 0) { + bis.skip(start); + } + + while ( diff > 0 && (numRead = bis.read(bytes, 0, Math.min(1000, diff))) >= 0) { + diff -= numRead; + bos.write(bytes, 0, numRead); + } + + String result = new String(bos.toByteArray(), encoding); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result)); + } catch (IOException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getLocalizedMessage())); + } + } + }; - if (start > 0) { - bis.skip(start); - } + this.cordova.getThreadPool().execute(readAsTextRunnable); + } - while ( diff > 0 && (numRead = bis.read(bytes, 0, Math.min(1000, diff))) >= 0) { - diff -= numRead; - bos.write(bytes, 0, numRead); - } + /** + * Read the contents of a file as binary. + * This is done in a background thread; the result is sent to the callback. + * + * @param filename The name of the file. + * @param encoding The encoding to return contents as. Typical value is UTF-8. (see http://www.iana.org/assignments/character-sets) + * @param start Start position in the file. + * @param end End position to stop at (exclusive). + * @return Contents of file. + */ + public void readAsBinary(final String filename, final int start, final int end, final CallbackContext callbackContext) { + Runnable readAsBinaryRunnable = new Runnable() { + public void run() { + try { + byte[] result = readAsBinaryHelper(filename, start, end); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result)); + } catch (IOException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getLocalizedMessage())); + } + } + }; - return new String(bos.toByteArray(), encoding); + this.cordova.getThreadPool().execute(readAsBinaryRunnable); } /** - * Helper method to read a text file and return its contents as a byte[]. + * Read the contents of a file as binary. + * This is done synchronously; the result is returned. * * @param filename The name of the file. * @param start Start position in the file. * @param end End position to stop at (exclusive). * @return Contents of the file as a byte[]. - * @throws FileNotFoundException, IOException + * @throws IOException */ - public byte[] readAsBinary(String filename, int start, int end) throws FileNotFoundException, IOException { + private byte[] readAsBinaryHelper(String filename, int start, int end) throws IOException { int diff = end - start; byte[] bytes = new byte[1000]; BufferedInputStream bis = new BufferedInputStream(FileHelper.getInputStreamFromUriString(filename, cordova), 1024); @@ -1017,21 +1050,30 @@ public class FileUtils extends CordovaPlugin { } /** - * Read content of a file and return as base64 encoded data url. + * Read the contents of a file as a base64 encoded data URL. * * @param filename The name of the file. * @param start Start position in the file. * @param end End position to stop at (exclusive). * @return Contents of file = data:;base64, - * @throws FileNotFoundException, IOException */ - public String readAsDataURL(String filename, int start, int end) throws FileNotFoundException, IOException { - // Determine content type from file name - String contentType = FileHelper.getMimeType(filename, cordova); + public void readAsDataURL(final String filename, final int start, final int end, final CallbackContext callbackContext) { + Runnable readAsDataUrlRunnable = new Runnable() { + public void run() { + try { + // Determine content type from file name + String contentType = FileHelper.getMimeType(filename, cordova); + + byte[] base64 = Base64.encodeBase64(readAsBinaryHelper(filename, start, end)); + String result = "data:" + contentType + ";base64," + new String(base64); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result)); + } catch (IOException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getLocalizedMessage())); + } + } + }; - byte[] base64 = Base64.encodeBase64(readAsBinary(filename, start, end)); - String data = "data:" + contentType + ";base64," + new String(base64); - return data; + this.cordova.getThreadPool().execute(readAsDataUrlRunnable); } /**