You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2013/01/08 00:21:50 UTC

[2/3] android commit: And support for slicing in readAsDataURL.

And support for slicing in readAsDataURL.


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/4589bdd3
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/4589bdd3
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/4589bdd3

Branch: refs/heads/master
Commit: 4589bdd31ff28b0c7c98ec8eb3bda8a3eb99e287
Parents: 552885d
Author: Braden Shepherdson <br...@chromium.org>
Authored: Fri Jan 4 15:00:55 2013 -0500
Committer: Braden Shepherdson <br...@chromium.org>
Committed: Fri Jan 4 15:00:55 2013 -0500

----------------------------------------------------------------------
 framework/src/org/apache/cordova/FileUtils.java |   22 +++++++++++++++--
 1 files changed, 19 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/4589bdd3/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 be5224e..5694b3d 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:<media type>;base64,<data>
      * @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);
         }