Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removing loop from File I/O

  • Loading branch information...
commit 64ab481366438a08907969c7568fdc707ff0a4cb 1 parent e918f0b
Joe Bowser authored
23 android/framework/src/com/phonegap/FileUtils.java
@@ -103,27 +103,22 @@ public String read(String filename)
103 103
104 104 public int write(String filename, String data, boolean append)
105 105 {
106   - int i=0;
107 106 String FilePath= filename;
108 107 try {
109   - ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
110   - byte buff[] = new byte[1024];
111   - FileOutputStream out=
112   - new FileOutputStream(FilePath, append);
113   - do {
114   - int numread = in.read(buff);
115   - if (numread <= 0)
116   - break;
117   - out.write(buff, 0, numread);
118   - System.out.println("numread" + numread);
119   - i++;
120   - } while (true);
  108 + byte [] rawData = data.getBytes();
  109 + ByteArrayInputStream in = new ByteArrayInputStream(rawData);
  110 + FileOutputStream out= new FileOutputStream(FilePath, false);
  111 + byte buff[] = new byte[rawData.length];
  112 + in.read(buff, 0, buff.length);
  113 + out.write(buff, 0, rawData.length);
121 114 out.flush();
122   - out.close();
  115 + out.close();
123 116 mView.loadUrl("javascript:navigator.file.winCallback('File written')");
124 117 } catch (Exception e) {
125 118 mView.loadUrl("javascript:navigator.file.failCallback('Fail')");
126 119 }
127 120 return 0;
128 121 }
  122 +
  123 +
129 124 }
11 android/js/device.js
@@ -18,13 +18,7 @@ function Device() {
18 18 this.gapVersion = window.DroidGap.getVersion();
19 19 this.platform = window.DroidGap.getPlatform();
20 20 this.name = window.DroidGap.getProductName();
21   - } else {
22   - this.platform = DeviceInfo.platform;
23   - this.version = DeviceInfo.version;
24   - this.name = DeviceInfo.name;
25   - this.gap = DeviceInfo.gap;
26   - this.uuid = DeviceInfo.uuid;
27   - }
  21 + }
28 22 } catch(e) {
29 23 this.available = false;
30 24 }
@@ -32,7 +26,4 @@ function Device() {
32 26
33 27 PhoneGap.addConstructor(function() {
34 28 navigator.device = window.device = new Device();
35   - var event = document.createEvent("Events");
36   - event.initEvent('deviceReady', false, false);
37   - document.dispatchEvent(event);
38 29 });
106 android/js/phonegap.js.base
@@ -17,7 +17,7 @@ PhoneGap = {
17 17
18 18 /**
19 19 * Boolean flag indicating if the PhoneGap API is available and initialized.
20   - */
  20 + */ // TODO: Remove this, it is unused here ... -jm
21 21 PhoneGap.available = DeviceInfo.uuid != undefined;
22 22
23 23 /**
@@ -27,28 +27,51 @@ PhoneGap.available = DeviceInfo.uuid != undefined;
27 27 */
28 28 PhoneGap.addConstructor = function(func) {
29 29 var state = document.readyState;
30   - if (state != 'loaded' && state != 'complete')
31   - PhoneGap._constructors.push(func);
  30 + if ( state == 'loaded' || state == 'complete' )
  31 + {
  32 + func();
  33 + }
32 34 else
33   - func();
  35 + {
  36 + PhoneGap._constructors.push(func);
  37 + }
34 38 };
35   -(function() {
36   - var timer = setInterval(function() {
37   - var state = document.readyState;
38   - if (state != 'loaded' && state != 'complete')
39   - return;
40   - clearInterval(timer);
41   - while (PhoneGap._constructors.length > 0) {
42   - var constructor = PhoneGap._constructors.shift();
43   - try {
44   - constructor();
45   - } catch(e) {
46   - if (typeof(debug['log']) == 'function')
47   - debug.log("Failed to run constructor: " + debug.processMessage(e));
48   - else
49   - alert("Failed to run constructor: " + e.message);
  39 +
  40 +(function()
  41 + {
  42 + var timer = setInterval(function()
  43 + {
  44 +
  45 + var state = document.readyState;
  46 +
  47 + if ( state == 'loaded' || state == 'complete' )
  48 + {
  49 + clearInterval(timer); // stop looking
  50 + // run our constructors list
  51 + while (PhoneGap._constructors.length > 0)
  52 + {
  53 + var constructor = PhoneGap._constructors.shift();
  54 + try
  55 + {
  56 + constructor();
  57 + }
  58 + catch(e)
  59 + {
  60 + if (typeof(debug['log']) == 'function')
  61 + {
  62 + debug.log("Failed to run constructor: " + debug.processMessage(e));
  63 + }
  64 + else
  65 + {
  66 + alert("Failed to run constructor: " + e.message);
  67 + }
  68 + }
50 69 }
51   - }
  70 + // all constructors run, now fire the deviceready event
  71 + var e = document.createEvent('Events');
  72 + e.initEvent('deviceready');
  73 + document.dispatchEvent(e);
  74 + }
52 75 }, 1);
53 76 })();
54 77
@@ -65,11 +88,48 @@ PhoneGap.exec = function() {
65 88 if (PhoneGap.queue.timer == null)
66 89 PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10);
67 90 };
  91 +
68 92 /**
69   - * Internal function used to dispatch the request to PhoneGap. This needs to be implemented per-platform to
70   - * ensure that methods are called on the phone in a way appropriate for that device.
  93 + * Internal function used to dispatch the request to PhoneGap. It processes the
  94 + * command queue and executes the next command on the list. If one of the
  95 + * arguments is a JavaScript object, it will be passed on the QueryString of the
  96 + * url, which will be turned into a dictionary on the other end.
71 97 * @private
72 98 */
73 99 PhoneGap.run_command = function() {
74   -};
  100 + if (!PhoneGap.available || !PhoneGap.queue.ready)
  101 + return;
  102 +
  103 + PhoneGap.queue.ready = false;
  104 +
  105 + var args = PhoneGap.queue.commands.shift();
  106 + if (PhoneGap.queue.commands.length == 0) {
  107 + clearInterval(PhoneGap.queue.timer);
  108 + PhoneGap.queue.timer = null;
  109 + }
75 110
  111 + var uri = [];
  112 + var dict = null;
  113 + for (var i = 1; i < args.length; i++) {
  114 + var arg = args[i];
  115 + if (arg == undefined || arg == null)
  116 + arg = '';
  117 + if (typeof(arg) == 'object')
  118 + dict = arg;
  119 + else
  120 + uri.push(encodeURIComponent(arg));
  121 + }
  122 + var url = "gap://" + args[0] + "/" + uri.join("/");
  123 + if (dict != null) {
  124 + var query_args = [];
  125 + for (var name in dict) {
  126 + if (typeof(name) != 'string')
  127 + continue;
  128 + query_args.push(encodeURIComponent(name) + "=" + encodeURIComponent(dict[name]));
  129 + }
  130 + if (query_args.length > 0)
  131 + url += "?" + query_args.join("&");
  132 + }
  133 + document.location = url;
  134 +
  135 +};

0 comments on commit 64ab481

Please sign in to comment.
Something went wrong with that request. Please try again.