aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/FileUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/utils/FileUtils.java')
-rw-r--r--main/src/cgeo/geocaching/utils/FileUtils.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java
index 4a349a8..979820c 100644
--- a/main/src/cgeo/geocaching/utils/FileUtils.java
+++ b/main/src/cgeo/geocaching/utils/FileUtils.java
@@ -23,6 +23,8 @@ import java.util.List;
*/
public final class FileUtils {
+ private static final String FILE_PROTOCOL = "file://";
+
private FileUtils() {
// utility class
}
@@ -153,4 +155,32 @@ public final class FileUtils {
}
return true;
}
+
+ /**
+ * Check if the URL represents a file on the local file system.
+ *
+ * @return <tt>true</tt> if the URL scheme is <tt>file</tt>, <tt>false</tt> otherwise
+ */
+ public static boolean isFileUrl(final String url) {
+ return StringUtils.startsWith(url, FILE_PROTOCOL);
+ }
+
+ /**
+ * Build an URL from a file name.
+ *
+ * @param file a local file name
+ * @return an URL with the <tt>file</tt> scheme
+ */
+ public static String fileToUrl(final File file) {
+ return FILE_PROTOCOL + file.getAbsolutePath();
+ }
+
+ /**
+ * Local file name when {@link #isFileUrl(String)} is <tt>true</tt>.
+ *
+ * @return the local file
+ */
+ public static File urlToFile(final String url) {
+ return new File(StringUtils.substring(url, FILE_PROTOCOL.length()));
+ }
}