List of usage examples for org.apache.cordova.file InvalidModificationException InvalidModificationException
public InvalidModificationException(String message)
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * A setup method that handles the move/copy of files/directories * * @param fileName to be copied/moved//from w w w .j ava 2 s.c o m * @param newParent is the location where the file will be copied/moved to * @param newName for the file directory to be called, if null use existing file name * @param move if false do a copy, if true do a move * @return a Entry object * @throws NoModificationAllowedException * @throws IOException * @throws InvalidModificationException * @throws EncodingException * @throws JSONException */ private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException { fileName = stripFileProtocol(fileName); newParent = stripFileProtocol(newParent); // Check for invalid file name if (newName != null && newName.contains(":")) { throw new EncodingException("Bad file name"); } File source = new File(fileName); if (!source.exists()) { // The file/directory we are copying doesn't exist so we should fail. throw new FileNotFoundException("The source does not exist"); } File destinationDir = new File(newParent); if (!destinationDir.exists()) { // The destination does not exist so we should fail. throw new FileNotFoundException("The source does not exist"); } // Figure out where we should be copying to File destination = createDestination(newName, source, destinationDir); //Log.d(LOG_TAG, "Source: " + source.getAbsolutePath()); //Log.d(LOG_TAG, "Destin: " + destination.getAbsolutePath()); // Check to see if source and destination are the same file if (source.getAbsolutePath().equals(destination.getAbsolutePath())) { throw new InvalidModificationException("Can't copy a file onto itself"); } if (source.isDirectory()) { if (move) { return moveDirectory(source, destination); } else { return copyDirectory(source, destination); } } else { if (move) { return moveFile(source, destination); } else { return copyFile(source, destination); } } }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * Copy a file/*from ww w . j a v a 2s . c o m*/ * * @param srcFile file to be copied * @param destFile destination to be copied to * @return a FileEntry object * @throws IOException * @throws InvalidModificationException * @throws JSONException */ private JSONObject copyFile(File srcFile, File destFile) throws IOException, InvalidModificationException, JSONException { // Renaming a file to an existing directory should fail if (destFile.exists() && destFile.isDirectory()) { throw new InvalidModificationException("Can't rename a file to a directory"); } FileChannel input = new FileInputStream(srcFile).getChannel(); FileChannel output = new FileOutputStream(destFile).getChannel(); input.transferTo(0, input.size(), output); input.close(); output.close(); /* if (srcFile.length() != destFile.length()) { return false; } */ return getEntry(destFile); }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * Copy a directory//from www .ja v a 2s. c o m * * @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 direcotry"); } } 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); }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * Move a file//from www. j a v a 2 s . c o m * * @param srcFile file to be copied * @param destFile destination to be copied to * @return a FileEntry object * @throws IOException * @throws InvalidModificationException * @throws JSONException */ private JSONObject moveFile(File srcFile, File destFile) throws JSONException, InvalidModificationException { // Renaming a file to an existing directory should fail if (destFile.exists() && destFile.isDirectory()) { throw new InvalidModificationException("Can't rename a file to a directory"); } // Try to rename the file if (!srcFile.renameTo(destFile)) { // Trying to rename the file failed. Possibly because we moved across file system on the device. // Now we have to do things the hard way // 1) Copy all the old file // 2) delete the src file } return getEntry(destFile); }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * Move a directory/*from ww w . j av a 2 s . c om*/ * * @param srcDir directory to be copied * @param destinationDir destination to be copied to * @return a DirectoryEntry object * @throws JSONException * @throws IOException * @throws InvalidModificationException */ private JSONObject moveDirectory(File srcDir, File destinationDir) throws JSONException, 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 move itself into itself"); } // If the destination directory already exists and is empty then delete it. This is according to spec. if (destinationDir.exists()) { if (destinationDir.list().length > 0) { throw new InvalidModificationException("directory is not empty"); } } // Try to rename the directory if (!srcDir.renameTo(destinationDir)) { // Trying to rename the directory failed. Possibly because we moved across file system on the device. // Now we have to do things the hard way // 1) Copy all the old files // 2) delete the src directory } return getEntry(destinationDir); }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
License:Apache License
/** * Deletes a file or directory. It is an error to attempt to delete a directory that is not empty. * It is an error to attempt to delete the root directory of a filesystem. * * @param filePath file or directory to be removed * @return a boolean representing success of failure * @throws NoModificationAllowedException * @throws InvalidModificationException// w w w . j a v a 2 s. c o m */ private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException { File fp = createFileObject(filePath); // You can't delete the root directory. if (atRootDirectory(filePath)) { throw new NoModificationAllowedException("You can't delete the root directory"); } // You can't delete a directory that is not empty if (fp.isDirectory() && fp.list().length > 0) { throw new InvalidModificationException("You can't delete a directory that is not empty."); } return fp.delete(); }