diff --git a/framework/assets/js/file.js b/framework/assets/js/file.js index 1a79f0ea..d2bba512 100755 --- a/framework/assets/js/file.js +++ b/framework/assets/js/file.js @@ -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") { diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index b9945b64..a2ed7a11 100755 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -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 *