mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Adding truncate to FileWriter
This commit is contained in:
parent
b7024ad1f5
commit
2e5d6f5b74
@ -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,15 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
|
||||
// Save error
|
||||
me.error = e;
|
||||
|
||||
// DONE state
|
||||
me.readyState = FileWriter.DONE;
|
||||
|
||||
// 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);
|
||||
@ -459,3 +487,81 @@ FileWriter.prototype.writeAsText = function(file, text, bAppend) {
|
||||
|
||||
};
|
||||
|
||||
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);
|
||||
me.onwriteend(evt);
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -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 @@ import com.phonegap.api.PluginResult;
|
||||
* 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;
|
||||
@ -114,6 +117,17 @@ public class FileUtils extends Plugin {
|
||||
e.printStackTrace();
|
||||
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) {
|
||||
@ -210,5 +224,21 @@ public class FileUtils extends Plugin {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user