Refactoring the checks for file:// into a convenience method

This commit is contained in:
macdonst 2012-02-22 15:56:03 -05:00
parent cc5cfad58d
commit e85394dd99

View File

@ -281,11 +281,7 @@ public class FileUtils extends Plugin {
* @throws JSONException * @throws JSONException
*/ */
private JSONArray readEntries(String fileName) throws FileNotFoundException, JSONException { private JSONArray readEntries(String fileName) throws FileNotFoundException, JSONException {
if (fileName.startsWith("file://")) { File fp = createFileObject(fileName);
fileName = fileName.substring(7);
}
File fp = new File(fileName);
if (!fp.exists()) { if (!fp.exists()) {
// The directory we are listing doesn't exist so we should fail. // The directory we are listing doesn't exist so we should fail.
@ -319,12 +315,8 @@ public class FileUtils extends Plugin {
* @throws JSONException * @throws JSONException
*/ */
private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException { private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException {
if (fileName.startsWith("file://")) { fileName = stripFileProtocol(fileName);
fileName = fileName.substring(7); newParent = stripFileProtocol(newParent);
}
if (newParent.startsWith("file://")) {
newParent = newParent.substring(7);
}
// Check for invalid file name // Check for invalid file name
@ -567,11 +559,7 @@ public class FileUtils extends Plugin {
* @throws FileExistsException * @throws FileExistsException
*/ */
private boolean removeRecursively(String filePath) throws FileExistsException { private boolean removeRecursively(String filePath) throws FileExistsException {
if (filePath.startsWith("file://")) { File fp = createFileObject(filePath);
filePath = filePath.substring(7);
}
File fp = new File(filePath);
// You can't delete the root directory. // You can't delete the root directory.
if (atRootDirectory(filePath)) { if (atRootDirectory(filePath)) {
@ -612,11 +600,7 @@ public class FileUtils extends Plugin {
* @throws InvalidModificationException * @throws InvalidModificationException
*/ */
private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException { private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException {
if (filePath.startsWith("file://")) { File fp = createFileObject(filePath);
filePath = filePath.substring(7);
}
File fp = new File(filePath);
// You can't delete the root directory. // You can't delete the root directory.
if (atRootDirectory(filePath)) { if (atRootDirectory(filePath)) {
@ -707,9 +691,7 @@ public class FileUtils extends Plugin {
if (fileName.startsWith("/")) { if (fileName.startsWith("/")) {
fp = new File(fileName); fp = new File(fileName);
} else { } else {
if (dirPath.startsWith("file://")) { dirPath = stripFileProtocol(dirPath);
dirPath = dirPath.substring(7);
}
fp = new File(dirPath + File.separator + fileName); fp = new File(dirPath + File.separator + fileName);
} }
return fp; return fp;
@ -724,9 +706,7 @@ public class FileUtils extends Plugin {
* @throws JSONException * @throws JSONException
*/ */
private JSONObject getParent(String filePath) throws JSONException { private JSONObject getParent(String filePath) throws JSONException {
if (filePath.startsWith("file://")) { filePath = stripFileProtocol(filePath);
filePath = filePath.substring(7);
}
if (atRootDirectory(filePath)) { if (atRootDirectory(filePath)) {
return getEntry(filePath); return getEntry(filePath);
@ -742,9 +722,7 @@ public class FileUtils extends Plugin {
* @return true if we are at the root, false otherwise. * @return true if we are at the root, false otherwise.
*/ */
private boolean atRootDirectory(String filePath) { private boolean atRootDirectory(String filePath) {
if (filePath.startsWith("file://")) { filePath = stripFileProtocol(filePath);
filePath = filePath.substring(7);
}
if (filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + ctx.getPackageName() + "/cache") || if (filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + ctx.getPackageName() + "/cache") ||
filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath()) || filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath()) ||
@ -754,6 +732,32 @@ public class FileUtils extends Plugin {
return false; return false;
} }
/**
* This method removes the "file://" from the passed in filePath
*
* @param filePath to be checked.
* @return
*/
private String stripFileProtocol(String filePath) {
if (filePath.startsWith("file://")) {
filePath = filePath.substring(7);
}
return filePath;
}
/**
* Create a File object from the passed in path
*
* @param filePath
* @return
*/
private File createFileObject(String filePath) {
filePath = stripFileProtocol(filePath);
File file = new File(filePath);
return file;
}
/** /**
* Look up metadata about this entry. * Look up metadata about this entry.
* *
@ -763,11 +767,7 @@ public class FileUtils extends Plugin {
* @throws JSONException * @throws JSONException
*/ */
private JSONObject getMetadata(String filePath) throws FileNotFoundException, JSONException { private JSONObject getMetadata(String filePath) throws FileNotFoundException, JSONException {
if (filePath.startsWith("file://")) { File file = createFileObject(filePath);
filePath = filePath.substring(7);
}
File file = new File(filePath);
if (!file.exists()) { if (!file.exists()) {
throw new FileNotFoundException("Failed to find file in getMetadata"); throw new FileNotFoundException("Failed to find file in getMetadata");
@ -788,11 +788,7 @@ public class FileUtils extends Plugin {
* @throws JSONException * @throws JSONException
*/ */
private JSONObject getFileMetadata(String filePath) throws FileNotFoundException, JSONException { private JSONObject getFileMetadata(String filePath) throws FileNotFoundException, JSONException {
if (filePath.startsWith("file://")) { File file = createFileObject(filePath);
filePath = filePath.substring(7);
}
File file = new File(filePath);
if (!file.exists()) { if (!file.exists()) {
throw new FileNotFoundException("File: " + filePath + " does not exist."); throw new FileNotFoundException("File: " + filePath + " does not exist.");
@ -975,9 +971,7 @@ public class FileUtils extends Plugin {
*/ */
/**/ /**/
public long write(String filename, String data, int offset) throws FileNotFoundException, IOException { public long write(String filename, String data, int offset) throws FileNotFoundException, IOException {
if (filename.startsWith("file://")) { filename = stripFileProtocol(filename);
filename = filename.substring(7);
}
boolean append = false; boolean append = false;
if (offset > 0) { if (offset > 0) {
@ -1005,9 +999,7 @@ public class FileUtils extends Plugin {
* @throws FileNotFoundException, IOException * @throws FileNotFoundException, IOException
*/ */
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException { private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
if (filename.startsWith("file://")) { filename = stripFileProtocol(filename);
filename = filename.substring(7);
}
RandomAccessFile raf = new RandomAccessFile(filename, "rw"); RandomAccessFile raf = new RandomAccessFile(filename, "rw");
@ -1033,9 +1025,7 @@ public class FileUtils extends Plugin {
return ctx.getContentResolver().openInputStream(uri); return ctx.getContentResolver().openInputStream(uri);
} }
else { else {
if (path.startsWith("file://")) { path = stripFileProtocol(path);
path = path.substring(7);
}
return new FileInputStream(path); return new FileInputStream(path);
} }
} }