You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2014/04/24 23:36:13 UTC

[3/6] js commit: CB-6388: Add base64.toArrayBuffer() method to support binary data from the LOAD_URL bridge

CB-6388: Add base64.toArrayBuffer() method to support binary data from the LOAD_URL bridge


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/53eae163
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/53eae163
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/53eae163

Branch: refs/heads/browserify
Commit: 53eae16307d5d94eb9654f95893e9f4f1816254d
Parents: 81f9a00
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Apr 2 12:09:21 2014 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Apr 2 12:11:31 2014 -0400

----------------------------------------------------------------------
 src/common/base64.js | 10 ++++++++++
 test/test.base64.js  | 14 ++++++++++++++
 2 files changed, 24 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/53eae163/src/common/base64.js
----------------------------------------------------------------------
diff --git a/src/common/base64.js b/src/common/base64.js
index be58f38..5181390 100644
--- a/src/common/base64.js
+++ b/src/common/base64.js
@@ -26,6 +26,16 @@ base64.fromArrayBuffer = function(arrayBuffer) {
     return uint8ToBase64(array);
 };
 
+base64.toArrayBuffer = function(str) {
+    var decodedStr = typeof atob != 'undefined' ? atob(str) : new Buffer(str,'base64').toString('binary');
+    var arrayBuffer = new ArrayBuffer(decodedStr.length);
+    var array = new Uint8Array(arrayBuffer);
+    for (var i=0, len=decodedStr.length; i < len; i++) {
+        array[i] = decodedStr.charCodeAt(i);
+    }
+    return arrayBuffer;
+};
+
 //------------------------------------------------------------------------------
 
 /* This code is based on the performance tests at http://jsperf.com/b64tests

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/53eae163/test/test.base64.js
----------------------------------------------------------------------
diff --git a/test/test.base64.js b/test/test.base64.js
index 563cff3..6248a36 100644
--- a/test/test.base64.js
+++ b/test/test.base64.js
@@ -60,4 +60,18 @@ describe("base64", function () {
 
         expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string);
     });
+
+    it("can decode a base64-encoded text string into an ArrayBuffer", function () {
+        var orig = 'Some Awesome Test This Is!',
+            base64string = typeof btoa != 'undefined' ? btoa(orig) : new Buffer(orig, 'binary').toString('base64');
+
+        var arrayBuffer = base64.toArrayBuffer(base64string);
+
+        var testString = "";
+        var view = new Uint8Array(arrayBuffer);
+        for (var i = 0; i < view.byteLength; i++) {
+            testString += String.fromCharCode(view[i]);
+        }
+        expect(testString).toEqual(orig);
+    });
 });