From 552885dbd3a05672b2f347b15307d4a902a0086a Mon Sep 17 00:00:00 2001 From: Braden Shepherdson Date: Wed, 2 Jan 2013 15:34:39 -0800 Subject: [PATCH 1/2] Add support for reading slices of text files. --- .../src/org/apache/cordova/FileUtils.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index 8aff2629..be5224e3 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -112,7 +112,16 @@ public class FileUtils extends CordovaPlugin { callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b)); } else if (action.equals("readAsText")) { - String s = this.readAsText(args.getString(0), args.getString(1)); + int start = 0; + int end = Integer.MAX_VALUE; + if (args.length() >= 3) { + start = args.getInt(2); + } + if (args.length() >= 4) { + end = args.getInt(3); + } + + String s = this.readAsText(args.getString(0), args.getString(1), start, end); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); } else if (action.equals("readAsDataURL")) { @@ -932,17 +941,27 @@ public class FileUtils extends CordovaPlugin { * @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 */ - public String readAsText(String filename, String encoding) throws FileNotFoundException, IOException { + 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(getPathFromUri(filename), 1024); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int numRead = 0; - while ((numRead = bis.read(bytes, 0, 1000)) >= 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); } + return new String(bos.toByteArray(), encoding); } From 4589bdd31ff28b0c7c98ec8eb3bda8a3eb99e287 Mon Sep 17 00:00:00 2001 From: Braden Shepherdson Date: Fri, 4 Jan 2013 15:00:55 -0500 Subject: [PATCH 2/2] And support for slicing in readAsDataURL. --- .../src/org/apache/cordova/FileUtils.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index be5224e3..5694b3d0 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -125,7 +125,16 @@ public class FileUtils extends CordovaPlugin { callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); } else if (action.equals("readAsDataURL")) { - String s = this.readAsDataURL(args.getString(0)); + int start = 0; + int end = Integer.MAX_VALUE; + if (args.length() >= 2) { + start = args.getInt(1); + } + if (args.length() >= 3) { + end = args.getInt(2); + } + + String s = this.readAsDataURL(args.getString(0), start, end); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s)); } else if (action.equals("write")) { @@ -972,12 +981,19 @@ public class FileUtils extends CordovaPlugin { * @return Contents of file = data:;base64, * @throws FileNotFoundException, IOException */ - public String readAsDataURL(String filename) throws FileNotFoundException, IOException { + public String readAsDataURL(String filename, int start, int end) throws FileNotFoundException, IOException { + int diff = end - start; byte[] bytes = new byte[1000]; BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int numRead = 0; - while ((numRead = bis.read(bytes, 0, 1000)) >= 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); }