mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 07:02:51 +08:00
Removing loop from File I/O
This commit is contained in:
parent
4bd73fc38e
commit
573dffb853
@ -103,27 +103,22 @@ public class FileUtils {
|
||||
|
||||
public int write(String filename, String data, boolean append)
|
||||
{
|
||||
int i=0;
|
||||
String FilePath= filename;
|
||||
try {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
|
||||
byte buff[] = new byte[1024];
|
||||
FileOutputStream out=
|
||||
new FileOutputStream(FilePath, append);
|
||||
do {
|
||||
int numread = in.read(buff);
|
||||
if (numread <= 0)
|
||||
break;
|
||||
out.write(buff, 0, numread);
|
||||
System.out.println("numread" + numread);
|
||||
i++;
|
||||
} while (true);
|
||||
byte [] rawData = data.getBytes();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
|
||||
FileOutputStream out= new FileOutputStream(FilePath, false);
|
||||
byte buff[] = new byte[rawData.length];
|
||||
in.read(buff, 0, buff.length);
|
||||
out.write(buff, 0, rawData.length);
|
||||
out.flush();
|
||||
out.close();
|
||||
out.close();
|
||||
mView.loadUrl("javascript:navigator.file.winCallback('File written')");
|
||||
} catch (Exception e) {
|
||||
mView.loadUrl("javascript:navigator.file.failCallback('Fail')");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
11
js/device.js
11
js/device.js
@ -18,13 +18,7 @@ function Device() {
|
||||
this.gapVersion = window.DroidGap.getVersion();
|
||||
this.platform = window.DroidGap.getPlatform();
|
||||
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) {
|
||||
this.available = false;
|
||||
}
|
||||
@ -32,7 +26,4 @@ function Device() {
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
navigator.device = window.device = new Device();
|
||||
var event = document.createEvent("Events");
|
||||
event.initEvent('deviceReady', false, false);
|
||||
document.dispatchEvent(event);
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ PhoneGap = {
|
||||
|
||||
/**
|
||||
* Boolean flag indicating if the PhoneGap API is available and initialized.
|
||||
*/
|
||||
*/ // TODO: Remove this, it is unused here ... -jm
|
||||
PhoneGap.available = DeviceInfo.uuid != undefined;
|
||||
|
||||
/**
|
||||
@ -27,28 +27,51 @@ PhoneGap.available = DeviceInfo.uuid != undefined;
|
||||
*/
|
||||
PhoneGap.addConstructor = function(func) {
|
||||
var state = document.readyState;
|
||||
if (state != 'loaded' && state != 'complete')
|
||||
PhoneGap._constructors.push(func);
|
||||
if ( state == 'loaded' || state == 'complete' )
|
||||
{
|
||||
func();
|
||||
}
|
||||
else
|
||||
func();
|
||||
{
|
||||
PhoneGap._constructors.push(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
|
||||
alert("Failed to run constructor: " + e.message);
|
||||
|
||||
(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// all constructors run, now fire the deviceready event
|
||||
var e = document.createEvent('Events');
|
||||
e.initEvent('deviceready');
|
||||
document.dispatchEvent(e);
|
||||
}
|
||||
}, 1);
|
||||
})();
|
||||
|
||||
@ -65,11 +88,48 @@ PhoneGap.exec = function() {
|
||||
if (PhoneGap.queue.timer == null)
|
||||
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
|
||||
* ensure that methods are called on the phone in a way appropriate for that device.
|
||||
* Internal function used to dispatch the request to PhoneGap. It processes the
|
||||
* 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
|
||||
*/
|
||||
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;
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user