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/03/04 23:36:52 UTC
android commit: Add readAsBinaryString and readAsArrayBuffer support
Updated Branches:
refs/heads/master fb81f3e77 -> 7187f87ea
Add readAsBinaryString and readAsArrayBuffer support
Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/7187f87e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/7187f87e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/7187f87e
Branch: refs/heads/master
Commit: 7187f87eaea3fdd0c3b94bfdec668a13c524afcf
Parents: fb81f3e
Author: Braden Shepherdson <br...@gmail.com>
Authored: Mon Mar 4 15:11:49 2013 -0500
Committer: Braden Shepherdson <br...@gmail.com>
Committed: Mon Mar 4 17:36:40 2013 -0500
----------------------------------------------------------------------
framework/src/org/apache/cordova/FileUtils.java | 41 +++++++++++++++---
1 files changed, 34 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7187f87e/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 b461b02..69e11f6 100755
--- a/framework/src/org/apache/cordova/FileUtils.java
+++ b/framework/src/org/apache/cordova/FileUtils.java
@@ -136,6 +136,19 @@ public class FileUtils extends CordovaPlugin {
String s = this.readAsDataURL(args.getString(0), start, end);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s));
}
+ else if (action.equals("readAsArrayBuffer")) {
+ int start = 0;
+ int end = Integer.MAX_VALUE;
+ if (args.length() >= 2) {
+ start = args.getInt(1);
+ }
+ if (args.length() >= 3) {
+ end = args.getInt(2);
+ }
+
+ byte[] s = this.readAsBinary(args.getString(0), start, end);
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s));
+ }
else if (action.equals("write")) {
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
@@ -987,13 +1000,14 @@ public class FileUtils extends CordovaPlugin {
}
/**
- * Read content of text file and return as base64 encoded data url.
- *
- * @param filename The name of the file.
- * @return Contents of file = data:<media type>;base64,<data>
+ * Helper method to read a text file and return its contents as a byte[].
+ * @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
*/
- public String readAsDataURL(String filename, int start, int end) throws FileNotFoundException, IOException {
+ public byte[] readAsBinary(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);
@@ -1009,7 +1023,20 @@ public class FileUtils extends CordovaPlugin {
bos.write(bytes, 0, numRead);
}
- // Determine content type from file name
+ return bos.toByteArray();
+ }
+
+ /**
+ * Read content of a file and return as 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:<media type>;base64,<data>
+ * @throws FileNotFoundException, IOException
+ */
+ public String readAsDataURL(String filename, int start, int end) throws FileNotFoundException, IOException {
+ // Determine content type from file name
String contentType = null;
if (filename.startsWith("content:")) {
Uri fileUri = Uri.parse(filename);
@@ -1019,7 +1046,7 @@ public class FileUtils extends CordovaPlugin {
contentType = getMimeType(filename);
}
- byte[] base64 = Base64.encodeBase64(bos.toByteArray());
+ byte[] base64 = Base64.encodeBase64(readAsBinary(filename, start, end));
String data = "data:" + contentType + ";base64," + new String(base64);
return data;
}