diff options
author | Sven Gothel <[email protected]> | 2011-11-29 04:59:42 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-11-29 04:59:42 +0100 |
commit | 04391a3f417e10e1b6dafbd8becc63659af633c3 (patch) | |
tree | 2b9a5f8dc81b7631d34d306894387d3bac4b9bec /src/java/com/jogamp/common/util/IOUtil.java | |
parent | 60ea6727f1a089f6afd17fcea3bd7d29353af9b4 (diff) |
JarUtil: Improve Robustness (Bug 522) and API doc, prepare for Jar-In-Jar. Add unit test.
Misc.:
- IOUtil: Add toURL* methods
- TempJarCache: Add 'URL getResource(String)'
Diffstat (limited to 'src/java/com/jogamp/common/util/IOUtil.java')
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index f9a34f0..7583172 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -37,6 +37,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.security.AccessController; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; @@ -184,6 +185,31 @@ public class IOUtil { * */ + public static String slashify(String path, boolean startWithSlash, boolean endWithSlash) { + String p = path.replace('\\', '/'); // unify file seperator + if (startWithSlash && !p.startsWith("/")) { + p = "/" + p; + } + if (endWithSlash && !p.endsWith("/")) { + p = p + "/"; + } + return p; + } + + /** Using the proper advertised conversion via File -> URI -> URL */ + public static URL toURL(File file) throws MalformedURLException { + return file.toURI().toURL(); + } + + /** Using the simple conversion via File -> URL, assuming proper characters. */ + public static URL toURLSimple(File file) throws MalformedURLException { + return new URL("file", "", slashify(file.getAbsolutePath(), true, file.isDirectory())); + } + + public static URL toURLSimple(String protocol, String file, boolean isDirectory) throws MalformedURLException { + return new URL(protocol, "", slashify(file, true, isDirectory)); + } + /** * Returns the lowercase suffix of the given file name (the text * after the last '.' in the file name). Returns null if the file @@ -236,7 +262,7 @@ public class IOUtil { * Returns the basename of the given fname w/o directory part */ public static String getBasename(String fname) { - fname = fname.replace('\\', '/'); // unify file seperator + fname = slashify(fname, false, false); int lios = fname.lastIndexOf('/'); // strip off dirname if(lios>=0) { fname = fname.substring(lios+1); @@ -248,7 +274,7 @@ public class IOUtil { * Returns unified '/' dirname including the last '/' */ public static String getDirname(String fname) { - fname = fname.replace('\\', '/'); // unify file seperator + fname = slashify(fname, false, false); int lios = fname.lastIndexOf('/'); // strip off dirname if(lios>=0) { fname = fname.substring(0, lios+1); @@ -318,7 +344,6 @@ public class IOUtil { * @see URL#URL(String) * @see File#File(String) */ - @SuppressWarnings("deprecation") public static URL getResource(String resourcePath, ClassLoader cl) { if(null == resourcePath) { return null; @@ -356,7 +381,7 @@ public class IOUtil { try { File file = new File(resourcePath); if(file.exists()) { - url = file.toURL(); + url = toURLSimple(file); } } catch (Throwable e) { if(DEBUG) { @@ -389,7 +414,7 @@ public class IOUtil { if (baseLocation != null) { final File file = new File(baseLocation, relativeFile); // Handle things on Windows - return file.getPath().replace('\\', '/'); + return slashify(file.getPath(), false, true); } return null; } |