CB 4004: Adding base64 encoding for array buffers, while removing the String expansion
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/5ba835cf Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/5ba835cf Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/5ba835cf Branch: refs/heads/master Commit: 5ba835cf64a50f48dcdc8ced49fed1a714c098cc Parents: ff21821 Author: Chris Barton <[email protected]> Authored: Wed Jun 26 11:23:29 2013 -0700 Committer: Ian Clelland <[email protected]> Committed: Wed Jul 3 16:37:40 2013 -0400 ---------------------------------------------------------------------- lib/android/exec.js | 3 ++- test/test.base64.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-js/blob/5ba835cf/lib/android/exec.js ---------------------------------------------------------------------- diff --git a/lib/android/exec.js b/lib/android/exec.js index 206c09a..f55b5ad 100644 --- a/lib/android/exec.js +++ b/lib/android/exec.js @@ -36,6 +36,7 @@ var cordova = require('cordova'), nativeApiProvider = require('cordova/plugin/android/nativeapiprovider'), utils = require('cordova/utils'), + base64 = require('cordova/base64'), jsToNativeModes = { PROMPT: 0, JS_OBJECT: 1, @@ -74,7 +75,7 @@ function androidExec(success, fail, service, action, args) { // Process any ArrayBuffers in the args into a string. for (var i = 0; i < args.length; i++) { if (utils.typeName(args[i]) == 'ArrayBuffer') { - args[i] = window.btoa(String.fromCharCode.apply(null, new Uint8Array(args[i]))); + args[i] = utils.encodeBase64(args[i]); } } http://git-wip-us.apache.org/repos/asf/cordova-js/blob/5ba835cf/test/test.base64.js ---------------------------------------------------------------------- diff --git a/test/test.base64.js b/test/test.base64.js index 666cafe..f9b1912 100644 --- a/test/test.base64.js +++ b/test/test.base64.js @@ -47,4 +47,17 @@ describe("base64", function () { expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string); }); + + it("can base64 encode an text string in an ArrayBuffer", function () { + var buffer = new Buffer('Some Awesome Test This Is!', 'binary') + , base64string = buffer.toString('base64') + , arrayBuffer = new ArrayBuffer(buffer.length) + , view = new Uint8Array(arrayBuffer); + + for (var i = 0; i < buffer.length; i++) { + view[i] = buffer[i]; + } + + expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string); + }); });
