mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Fixing a number of issues in File API
Issue #82: The RandomAccessFile class in Android's version of Java does not write non-ASCII characters very well. I've switched to using a FileOutputStream which seems to work just great. Tested by myself and folks from Egypt and the Netherlands. Issue #87: Fixed a problem where the file errors were being returned as evt.target.result.code.code.
This commit is contained in:
parent
e99f75d59b
commit
6618015151
@ -249,9 +249,7 @@ FileReader.prototype.readAsText = function(file, encoding) {
|
||||
}
|
||||
|
||||
// Save error
|
||||
var fileError = new FileError();
|
||||
fileError.code = e;
|
||||
me.error = fileError;
|
||||
me.error = e;
|
||||
|
||||
// If onerror callback
|
||||
if (typeof me.onerror === "function") {
|
||||
@ -333,9 +331,7 @@ FileReader.prototype.readAsDataURL = function(file) {
|
||||
}
|
||||
|
||||
// Save error
|
||||
var fileError = new FileError();
|
||||
fileError.code = e;
|
||||
me.error = fileError;
|
||||
me.error = e;
|
||||
|
||||
// If onerror callback
|
||||
if (typeof me.onerror === "function") {
|
||||
@ -482,10 +478,10 @@ FileWriter.prototype.write = function(text) {
|
||||
return;
|
||||
}
|
||||
|
||||
// So if the user wants to keep appending to the file
|
||||
me.length = Math.max(me.length, me.position + r);
|
||||
// position always increases by bytes written because file would be extended
|
||||
me.position += r;
|
||||
// The length of the file is now where we are done writing.
|
||||
me.length = me.position;
|
||||
|
||||
// If onwrite callback
|
||||
if (typeof me.onwrite === "function") {
|
||||
@ -511,9 +507,7 @@ FileWriter.prototype.write = function(text) {
|
||||
}
|
||||
|
||||
// Save error
|
||||
var fileError = new FileError();
|
||||
fileError.code = e;
|
||||
me.error = fileError;
|
||||
me.error = e;
|
||||
|
||||
// If onerror callback
|
||||
if (typeof me.onerror === "function") {
|
||||
@ -626,9 +620,7 @@ FileWriter.prototype.truncate = function(size) {
|
||||
}
|
||||
|
||||
// Save error
|
||||
var fileError = new FileError();
|
||||
fileError.code = e;
|
||||
me.error = fileError;
|
||||
me.error = e;
|
||||
|
||||
// If onerror callback
|
||||
if (typeof me.onerror === "function") {
|
||||
|
@ -97,48 +97,23 @@ public class FileUtils extends Plugin {
|
||||
return new PluginResult(status, b);
|
||||
}
|
||||
else if (action.equals("readAsText")) {
|
||||
try {
|
||||
String s = this.readAsText(args.getString(0), args.getString(1));
|
||||
return new PluginResult(status, s);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
String s = this.readAsText(args.getString(0), args.getString(1));
|
||||
return new PluginResult(status, s);
|
||||
}
|
||||
else if (action.equals("readAsDataURL")) {
|
||||
try {
|
||||
String s = this.readAsDataURL(args.getString(0));
|
||||
return new PluginResult(status, s);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
String s = this.readAsDataURL(args.getString(0));
|
||||
return new PluginResult(status, s);
|
||||
}
|
||||
else if (action.equals("writeAsText")) {
|
||||
try {
|
||||
this.writeAsText(args.getString(0), args.getString(1), args.getBoolean(2));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
this.writeAsText(args.getString(0), args.getString(1), args.getBoolean(2));
|
||||
}
|
||||
else if (action.equals("write")) {
|
||||
try {
|
||||
long fileSize = this.write(args.getString(0), args.getString(1), args.getLong(2));
|
||||
return new PluginResult(status, fileSize);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2));
|
||||
return new PluginResult(status, fileSize);
|
||||
}
|
||||
else if (action.equals("truncate")) {
|
||||
try {
|
||||
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
||||
return new PluginResult(status, fileSize);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
||||
return new PluginResult(status, fileSize);
|
||||
}
|
||||
else if (action.equals("requestFileSystem")) {
|
||||
long size = args.optLong(1);
|
||||
@ -945,16 +920,26 @@ public class FileUtils extends Plugin {
|
||||
* @param offset The position to begin writing the file.
|
||||
* @throws FileNotFoundException, IOException
|
||||
*/
|
||||
public long write(String filename, String data, long offset) throws FileNotFoundException, IOException {
|
||||
RandomAccessFile file = new RandomAccessFile(filename, "rw");
|
||||
file.seek(offset);
|
||||
file.writeBytes(data);
|
||||
file.close();
|
||||
/**/
|
||||
public long write(String filename, String data, int offset) throws FileNotFoundException, IOException {
|
||||
boolean append = false;
|
||||
if (offset > 0) {
|
||||
this.truncateFile(filename, offset);
|
||||
append = true;
|
||||
}
|
||||
|
||||
byte [] rawData = data.getBytes();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
|
||||
FileOutputStream out = new FileOutputStream(filename, append);
|
||||
byte buff[] = new byte[rawData.length];
|
||||
in.read(buff, 0, buff.length);
|
||||
out.write(buff, 0, rawData.length);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
return data.length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Truncate the file to size
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user