Search in sources :

Example 1 with NoModificationAllowedException

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException)

Example 2 with NoModificationAllowedException

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();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException)

Example 3 with NoModificationAllowedException

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) PluginResult(org.apache.cordova.api.PluginResult) EncodingException(org.apache.cordova.file.EncodingException) TypeMismatchException(org.apache.cordova.file.TypeMismatchException) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) InvalidModificationException(org.apache.cordova.file.InvalidModificationException) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) FileExistsException(org.apache.cordova.file.FileExistsException)

Example 4 with NoModificationAllowedException

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);
}
Also used : InvalidModificationException(org.apache.cordova.file.InvalidModificationException) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

NoModificationAllowedException (org.apache.cordova.file.NoModificationAllowedException)4 RandomAccessFile (java.io.RandomAccessFile)2 InvalidModificationException (org.apache.cordova.file.InvalidModificationException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 FileChannel (java.nio.channels.FileChannel)1 PluginResult (org.apache.cordova.api.PluginResult)1 EncodingException (org.apache.cordova.file.EncodingException)1 FileExistsException (org.apache.cordova.file.FileExistsException)1 TypeMismatchException (org.apache.cordova.file.TypeMismatchException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1