Skip to content
Browse files

Adding truncate to FileWriter

  • Loading branch information...
1 parent b7024ad commit 2e5d6f5b74cce19c72f53b68e92fcd27e44b506e @macdonst macdonst committed
Showing with 152 additions and 16 deletions.
  1. +116 −10 framework/assets/js/file.js
  2. +36 −6 framework/src/com/phonegap/FileUtils.java
View
126 framework/assets/js/file.js
@@ -46,13 +46,16 @@ File._createEvent = function(type, target) {
function FileError() {
// File error codes
// Found in DOMException
- this.NOT_FOUND_ERR = 8;
- this.SECURITY_ERR = 18;
- this.ABORT_ERR = 20;
+ this.NOT_FOUND_ERR = 1;
+ this.SECURITY_ERR = 2;
+ this.ABORT_ERR = 3;
// Added by this specification
- this.NOT_READABLE_ERR = 24;
- this.ENCODING_ERR = 26;
+ this.NOT_READABLE_ERR = 4;
+ this.ENCODING_ERR = 5;
+ this.NO_MODIFICATION_ALLOWED_ERR = 6;
+ this.INVALID_STATE_ERR = 7;
+ this.SYNTAX_ERR = 8;
this.code = null;
};
@@ -99,6 +102,10 @@ FileMgr.prototype.writeAsText = function(fileName, data, append, successCallback
PhoneGap.exec(successCallback, errorCallback, "File", "writeAsText", [fileName, data, append]);
};
+FileMgr.prototype.truncate = function(fileName, size, successCallback, errorCallback) {
+ PhoneGap.exec(successCallback, errorCallback, "File", "truncate", [fileName, size]);
+};
+
FileMgr.prototype.readAsText = function(fileName, encoding, successCallback, errorCallback) {
PhoneGap.exec(successCallback, errorCallback, "File", "readAsText", [fileName, encoding]);
};
@@ -337,6 +344,16 @@ FileReader.prototype.readAsBinaryString = function(file) {
this.fileName = file;
};
+/**
+ * Read file and return data as a binary data.
+ *
+ * @param file The name of the file
+ */
+FileReader.prototype.readAsArrayBuffer = function(file) {
+ // TODO - Can't return binary data to browser.
+ this.fileName = file;
+};
+
//-----------------------------------------------------------------------------
// File Writer
//-----------------------------------------------------------------------------
@@ -388,7 +405,12 @@ FileWriter.prototype.abort = function() {
};
FileWriter.prototype.writeAsText = function(file, text, bAppend) {
- if (bAppend != true) {
+ // Throw an exception if we are already writing a file
+ if (this.readyState == FileWriter.WRITING) {
+ throw FileError.INVALID_STATE_ERR;
+ }
+
+ if (bAppend != true) {
bAppend = false; // for null values
}
@@ -399,6 +421,12 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
var me = this;
+ // If onwritestart callback
+ if (typeof me.onwritestart == "function") {
+ var evt = File._createEvent("writestart", me);
+ me.onwritestart(evt);
+ }
+
// Write file
navigator.fileMgr.writeAsText(file, text, bAppend,
@@ -413,15 +441,15 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
// Save result
me.result = r;
- // DONE state
- me.readyState = FileWriter.DONE;
-
// If onwrite callback
if (typeof me.onwrite == "function") {
var evt = File._createEvent("write", me);
me.onwrite(evt);
}
+ // DONE state
+ me.readyState = FileWriter.DONE;
+
// If onwriteend callback
if (typeof me.onwriteend == "function") {
var evt = File._createEvent("writeend", me);
@@ -440,15 +468,94 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
// Save error
me.error = e;
+ // If onerror callback
+ if (typeof me.onerror == "function") {
+ var evt = File._createEvent("error", me);
+ me.onerror(evt);
+ }
+
+ // DONE state
+ me.readyState = FileWriter.DONE;
+
+ // If onwriteend callback
+ if (typeof me.onwriteend == "function") {
+ var evt = File._createEvent("writeend", me);
+ me.onwriteend(evt);
+ }
+ }
+ );
+
+};
+
+FileWriter.prototype.truncate = function(file, size) {
+ // Throw an exception if we are already writing a file
+ if (this.readyState == FileWriter.WRITING) {
+ throw FileError.INVALID_STATE_ERR;
+ }
+
+ this.fileName = file;
+
+ // WRITING state
+ this.readyState = FileWriter.WRITING;
+
+ var me = this;
+
+ // If onwritestart callback
+ if (typeof me.onwritestart == "function") {
+ var evt = File._createEvent("writestart", me);
+ me.onwritestart(evt);
+ }
+
+ // Write file
+ navigator.fileMgr.truncate(file, size,
+
+ // Success callback
+ function(r) {
+
+ // If DONE (cancelled), then don't do anything
+ if (me.readyState == FileWriter.DONE) {
+ return;
+ }
+
+ // Save result
+ me.result = r;
+
+ // If onwrite callback
+ if (typeof me.onwrite == "function") {
+ var evt = File._createEvent("write", me);
+ me.onwrite(evt);
+ }
+
// DONE state
me.readyState = FileWriter.DONE;
+ // If onwriteend callback
+ if (typeof me.onwriteend == "function") {
+ var evt = File._createEvent("writeend", me);
+ me.onwriteend(evt);
+ }
+ },
+
+ // Error callback
+ function(e) {
+
+ // If DONE (cancelled), then don't do anything
+ if (me.readyState == FileWriter.DONE) {
+ return;
+ }
+
+ // Save error
+ me.error = e;
+
// If onerror callback
if (typeof me.onerror == "function") {
var evt = File._createEvent("error", me);
me.onerror(evt);
}
+ // DONE state
+ me.readyState = FileWriter.DONE;
+
// If onwriteend callback
if (typeof me.onwriteend == "function") {
var evt = File._createEvent("writeend", me);
@@ -456,6 +563,5 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
}
}
);
-
};
View
42 framework/src/com/phonegap/FileUtils.java
@@ -8,6 +8,7 @@
package com.phonegap;
import java.io.*;
+import java.nio.channels.FileChannel;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
@@ -21,13 +22,15 @@
* Only files on the SD card can be accessed.
*/
public class FileUtils extends Plugin {
+ public static int NOT_FOUND_ERR = 1;
+ public static int SECURITY_ERR = 2;
+ public static int ABORT_ERR = 3;
- public static int NOT_FOUND_ERR = 8;
- public static int SECURITY_ERR = 18;
- public static int ABORT_ERR = 20;
-
- public static int NOT_READABLE_ERR = 24;
- public static int ENCODING_ERR = 26;
+ public static int NOT_READABLE_ERR = 4;
+ public static int ENCODING_ERR = 5;
+ public static int NO_MODIFICATION_ALLOWED_ERR = 6;
+ public static int INVALID_STATE_ERR = 7;
+ public static int SYNTAX_ERR = 8;
FileReader f_in;
FileWriter f_out;
@@ -115,6 +118,17 @@ else if (action.equals("writeAsText")) {
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
}
}
+ else if (action.equals("truncate")) {
+ try {
+ this.truncateFile(args.getString(0), args.getLong(1));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
+ }
+ }
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
@@ -210,5 +224,21 @@ public void writeAsText(String filename, String data, boolean append) throws Fil
out.close();
}
+
+ /**
+ * Truncate the file to size
+ *
+ * @param filename
+ * @param size
+ * @throws FileNotFoundException, IOException
+ */
+ private void truncateFile(String filename, long size) throws FileNotFoundException, IOException {
+ RandomAccessFile raf = new RandomAccessFile(filename, "rw");
+
+ if (raf.length() >= size) {
+ FileChannel channel = raf.getChannel();
+ channel.truncate(size);
+ }
+ }
}

0 comments on commit 2e5d6f5

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