use of org.apache.cordova.file.NoModificationAllowedException in project cordova-android-chromeview by thedracle.
the class FileUtils method write.
/**
* Write contents of file.
*
* @param filename The name of the file.
* @param data The contents of the file.
* @param offset The position to begin writing the file.
* @param isBinary True if the file contents are base64-encoded binary data
* @throws FileNotFoundException, IOException
* @throws NoModificationAllowedException
*/
/**/
public long write(String filename, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
if (filename.startsWith("content://")) {
throw new NoModificationAllowedException("Couldn't write to file given its content URI");
}
filename = FileHelper.getRealPath(filename, cordova);
boolean append = false;
if (offset > 0) {
this.truncateFile(filename, offset);
append = true;
}
byte[] rawData;
if (isBinary) {
rawData = Base64.decode(data, Base64.DEFAULT);
} else {
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 rawData.length;
}
use of org.apache.cordova.file.NoModificationAllowedException in project cordova-android-chromeview by thedracle.
the class FileUtils method truncateFile.
/**
* Truncate the file to size
*
* @param filename
* @param size
* @throws FileNotFoundException, IOException
* @throws NoModificationAllowedException
*/
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
if (filename.startsWith("content://")) {
throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
}
filename = FileHelper.getRealPath(filename, cordova);
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
try {
if (raf.length() >= size) {
FileChannel channel = raf.getChannel();
channel.truncate(size);
return size;
}
return raf.length();
} finally {
raf.close();
}
}
use of org.apache.cordova.file.NoModificationAllowedException in project cordova-android-chromeview by thedracle.
the class FileUtils method execute.
/**
* Executes the request and returns whether the action was valid.
*
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackContext The callback context used when calling back into JavaScript.
* @return True if the action was valid, false otherwise.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("testSaveLocationExists")) {
boolean b = DirectoryManager.testSaveLocationExists();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
} else if (action.equals("getFreeDiskSpace")) {
long l = DirectoryManager.getFreeDiskSpace(false);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
} else if (action.equals("testFileExists")) {
boolean b = DirectoryManager.testFileExists(args.getString(0));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
} else if (action.equals("testDirectoryExists")) {
boolean b = DirectoryManager.testFileExists(args.getString(0));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
} else if (action.equals("readAsText")) {
String encoding = args.getString(1);
int start = args.getInt(2);
int end = args.getInt(3);
this.readFileAs(args.getString(0), start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
} else if (action.equals("readAsDataURL")) {
int start = args.getInt(1);
int end = args.getInt(2);
this.readFileAs(args.getString(0), start, end, callbackContext, null, -1);
} else if (action.equals("readAsArrayBuffer")) {
int start = args.getInt(1);
int end = args.getInt(2);
this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
} else if (action.equals("readAsBinaryString")) {
int start = args.getInt(1);
int end = args.getInt(2);
this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
} else if (action.equals("write")) {
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
} else if (action.equals("truncate")) {
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
} else if (action.equals("requestFileSystem")) {
long size = args.optLong(1);
if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
} else {
JSONObject obj = requestFileSystem(args.getInt(0));
callbackContext.success(obj);
}
} else if (action.equals("resolveLocalFileSystemURI")) {
JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
callbackContext.success(obj);
} else if (action.equals("getMetadata")) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getMetadata(args.getString(0))));
} else if (action.equals("getFileMetadata")) {
JSONObject obj = getFileMetadata(args.getString(0));
callbackContext.success(obj);
} else if (action.equals("getParent")) {
JSONObject obj = getParent(args.getString(0));
callbackContext.success(obj);
} else if (action.equals("getDirectory")) {
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
callbackContext.success(obj);
} else if (action.equals("getFile")) {
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
callbackContext.success(obj);
} else if (action.equals("remove")) {
boolean success;
success = remove(args.getString(0));
if (success) {
notifyDelete(args.getString(0));
callbackContext.success();
} else {
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
}
} else if (action.equals("removeRecursively")) {
boolean success = removeRecursively(args.getString(0));
if (success) {
callbackContext.success();
} else {
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
}
} else if (action.equals("moveTo")) {
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
callbackContext.success(entry);
} else if (action.equals("copyTo")) {
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
callbackContext.success(entry);
} else if (action.equals("readEntries")) {
JSONArray entries = readEntries(args.getString(0));
callbackContext.success(entries);
} else {
return false;
}
} catch (FileNotFoundException e) {
callbackContext.error(FileUtils.NOT_FOUND_ERR);
} catch (FileExistsException e) {
callbackContext.error(FileUtils.PATH_EXISTS_ERR);
} catch (NoModificationAllowedException e) {
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
} catch (InvalidModificationException e) {
callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
} catch (MalformedURLException e) {
callbackContext.error(FileUtils.ENCODING_ERR);
} catch (IOException e) {
callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
} catch (EncodingException e) {
callbackContext.error(FileUtils.ENCODING_ERR);
} catch (TypeMismatchException e) {
callbackContext.error(FileUtils.TYPE_MISMATCH_ERR);
}
return true;
}
use of org.apache.cordova.file.NoModificationAllowedException in project cordova-android-chromeview by thedracle.
the class FileUtils method copyDirectory.
/**
* Copy a directory
*
* @param srcDir directory to be copied
* @param destinationDir destination to be copied to
* @return a DirectoryEntry object
* @throws JSONException
* @throws IOException
* @throws NoModificationAllowedException
* @throws InvalidModificationException
*/
private JSONObject copyDirectory(File srcDir, File destinationDir) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
// Renaming a file to an existing directory should fail
if (destinationDir.exists() && destinationDir.isFile()) {
throw new InvalidModificationException("Can't rename a file to a directory");
}
// Check to make sure we are not copying the directory into itself
if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
throw new InvalidModificationException("Can't copy itself into itself");
}
// See if the destination directory exists. If not create it.
if (!destinationDir.exists()) {
if (!destinationDir.mkdir()) {
// If we can't create the directory then fail
throw new NoModificationAllowedException("Couldn't create the destination directory");
}
}
for (File file : srcDir.listFiles()) {
if (file.isDirectory()) {
copyDirectory(file, destinationDir);
} else {
File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
copyFile(file, destination);
}
}
return getEntry(destinationDir);
}
Aggregations