Removing loop from File I/O

This commit is contained in:
Joe Bowser 2009-12-03 17:07:07 -08:00
parent 4bd73fc38e
commit 573dffb853
3 changed files with 93 additions and 47 deletions

View File

@ -103,21 +103,14 @@ public class FileUtils {
public int write(String filename, String data, boolean append) public int write(String filename, String data, boolean append)
{ {
int i=0;
String FilePath= filename; String FilePath= filename;
try { try {
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); byte [] rawData = data.getBytes();
byte buff[] = new byte[1024]; ByteArrayInputStream in = new ByteArrayInputStream(rawData);
FileOutputStream out= FileOutputStream out= new FileOutputStream(FilePath, false);
new FileOutputStream(FilePath, append); byte buff[] = new byte[rawData.length];
do { in.read(buff, 0, buff.length);
int numread = in.read(buff); out.write(buff, 0, rawData.length);
if (numread <= 0)
break;
out.write(buff, 0, numread);
System.out.println("numread" + numread);
i++;
} while (true);
out.flush(); out.flush();
out.close(); out.close();
mView.loadUrl("javascript:navigator.file.winCallback('File written')"); mView.loadUrl("javascript:navigator.file.winCallback('File written')");
@ -126,4 +119,6 @@ public class FileUtils {
} }
return 0; return 0;
} }
} }

View File

@ -18,12 +18,6 @@ function Device() {
this.gapVersion = window.DroidGap.getVersion(); this.gapVersion = window.DroidGap.getVersion();
this.platform = window.DroidGap.getPlatform(); this.platform = window.DroidGap.getPlatform();
this.name = window.DroidGap.getProductName(); this.name = window.DroidGap.getProductName();
} else {
this.platform = DeviceInfo.platform;
this.version = DeviceInfo.version;
this.name = DeviceInfo.name;
this.gap = DeviceInfo.gap;
this.uuid = DeviceInfo.uuid;
} }
} catch(e) { } catch(e) {
this.available = false; this.available = false;
@ -32,7 +26,4 @@ function Device() {
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device(); navigator.device = window.device = new Device();
var event = document.createEvent("Events");
event.initEvent('deviceReady', false, false);
document.dispatchEvent(event);
}); });

View File

@ -17,7 +17,7 @@ PhoneGap = {
/** /**
* Boolean flag indicating if the PhoneGap API is available and initialized. * Boolean flag indicating if the PhoneGap API is available and initialized.
*/ */ // TODO: Remove this, it is unused here ... -jm
PhoneGap.available = DeviceInfo.uuid != undefined; PhoneGap.available = DeviceInfo.uuid != undefined;
/** /**
@ -27,28 +27,51 @@ PhoneGap.available = DeviceInfo.uuid != undefined;
*/ */
PhoneGap.addConstructor = function(func) { PhoneGap.addConstructor = function(func) {
var state = document.readyState; var state = document.readyState;
if (state != 'loaded' && state != 'complete') if ( state == 'loaded' || state == 'complete' )
PhoneGap._constructors.push(func); {
else
func(); func();
}; }
(function() {
var timer = setInterval(function() {
var state = document.readyState;
if (state != 'loaded' && state != 'complete')
return;
clearInterval(timer);
while (PhoneGap._constructors.length > 0) {
var constructor = PhoneGap._constructors.shift();
try {
constructor();
} catch(e) {
if (typeof(debug['log']) == 'function')
debug.log("Failed to run constructor: " + debug.processMessage(e));
else else
{
PhoneGap._constructors.push(func);
}
};
(function()
{
var timer = setInterval(function()
{
var state = document.readyState;
if ( state == 'loaded' || state == 'complete' )
{
clearInterval(timer); // stop looking
// run our constructors list
while (PhoneGap._constructors.length > 0)
{
var constructor = PhoneGap._constructors.shift();
try
{
constructor();
}
catch(e)
{
if (typeof(debug['log']) == 'function')
{
debug.log("Failed to run constructor: " + debug.processMessage(e));
}
else
{
alert("Failed to run constructor: " + e.message); alert("Failed to run constructor: " + e.message);
} }
} }
}
// all constructors run, now fire the deviceready event
var e = document.createEvent('Events');
e.initEvent('deviceready');
document.dispatchEvent(e);
}
}, 1); }, 1);
})(); })();
@ -65,11 +88,48 @@ PhoneGap.exec = function() {
if (PhoneGap.queue.timer == null) if (PhoneGap.queue.timer == null)
PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10); PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10);
}; };
/** /**
* Internal function used to dispatch the request to PhoneGap. This needs to be implemented per-platform to * Internal function used to dispatch the request to PhoneGap. It processes the
* ensure that methods are called on the phone in a way appropriate for that device. * command queue and executes the next command on the list. If one of the
* arguments is a JavaScript object, it will be passed on the QueryString of the
* url, which will be turned into a dictionary on the other end.
* @private * @private
*/ */
PhoneGap.run_command = function() { PhoneGap.run_command = function() {
}; if (!PhoneGap.available || !PhoneGap.queue.ready)
return;
PhoneGap.queue.ready = false;
var args = PhoneGap.queue.commands.shift();
if (PhoneGap.queue.commands.length == 0) {
clearInterval(PhoneGap.queue.timer);
PhoneGap.queue.timer = null;
}
var uri = [];
var dict = null;
for (var i = 1; i < args.length; i++) {
var arg = args[i];
if (arg == undefined || arg == null)
arg = '';
if (typeof(arg) == 'object')
dict = arg;
else
uri.push(encodeURIComponent(arg));
}
var url = "gap://" + args[0] + "/" + uri.join("/");
if (dict != null) {
var query_args = [];
for (var name in dict) {
if (typeof(name) != 'string')
continue;
query_args.push(encodeURIComponent(name) + "=" + encodeURIComponent(dict[name]));
}
if (query_args.length > 0)
url += "?" + query_args.join("&");
}
document.location = url;
};