diff options
author | Sven Gothel <[email protected]> | 2013-10-07 01:06:42 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-10-07 01:06:42 +0200 |
commit | 41fce94f95448bbea110a455ea508740fac8d8c9 (patch) | |
tree | 5ca6e6e004c27397ddb2a50f0959f05b53c7cca6 /src/java/com/jogamp/common/util/IOUtil.java | |
parent | 0dc49ba20f889a0e5ef3de3d750c39f82f464fc1 (diff) |
Bug 820: Escape SPACE in filenames for URI ctor, use decoded URI components when compiling new URI.
Add IOUtil:
- String encodeToURI(String)
- String decodeFromURI(String)
Both only handle escaping of SPACE only. Determine whether other chars need to be treated.
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, 29 insertions, 6 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 5918213..242f300 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -43,6 +43,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; +import java.util.regex.Pattern; import jogamp.common.Debug; import jogamp.common.os.AndroidUtils; @@ -312,7 +313,7 @@ public class IOUtil { * @throws URISyntaxException if the resulting string does not comply w/ an RFC 2396 URI */ public static URI toURISimple(File file) throws URISyntaxException { - return new URI(FILE_SCHEME, null, slashify(file.getAbsolutePath(), true, file.isDirectory()), null); + return new URI(FILE_SCHEME, null, encodeToURI(slashify(file.getAbsolutePath(), true, file.isDirectory())), null); } /** @@ -321,7 +322,7 @@ public class IOUtil { * @throws URISyntaxException if the resulting string does not comply w/ an RFC 2396 URI */ public static URI toURISimple(String protocol, String file, boolean isDirectory) throws URISyntaxException { - return new URI(protocol, null, slashify(file, true, isDirectory), null); + return new URI(protocol, null, encodeToURI(slashify(file, true, isDirectory)), null); } /** @@ -462,7 +463,7 @@ public class IOUtil { * <i>protocol</i> may be "file", "http", etc.. * </p> * - * @param uri "<i>protocol</i>:/some/path/gluegen-rt.jar" + * @param uri "<i>protocol</i>:/some/path/gluegen-rt.jar" (URI encoded) * @return "<i>protocol</i>:/some/path/" * @throws IllegalArgumentException if the URI doesn't match the expected formatting, or is null * @throws URISyntaxException @@ -727,7 +728,7 @@ public class IOUtil { * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code> */ public static URI getRelativeOf(URI baseURI, String relativePath) throws URISyntaxException { - return compose(baseURI.getScheme(), baseURI.getRawSchemeSpecificPart(), relativePath, baseURI.getRawFragment()); + return compose(baseURI.getScheme(), baseURI.getSchemeSpecificPart(), encodeToURI(relativePath), baseURI.getFragment()); } /** @@ -753,9 +754,11 @@ public class IOUtil { * </p> * * @param scheme scheme of the resulting URI - * @param schemeSpecificPart may include a query, which is separated while processing - * @param relativePath denotes a relative file to the baseLocation's parent directory + * @param schemeSpecificPart may include a query, which is separated while processing (URI encoded) + * @param relativePath denotes a relative file to the baseLocation's parent directory (URI encoded) + * @param fragment the URI fragment (URI encoded) * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code> + * @see #encodeToURI(String) */ public static URI compose(String scheme, String schemeSpecificPart, String relativePath, String fragment) throws URISyntaxException { // cut off optional query in scheme-specific-part @@ -777,6 +780,26 @@ public class IOUtil { return new URI(scheme, null == query ? schemeSpecificPart : schemeSpecificPart + "?" + query, fragment); } + private static final Pattern patternSpaceRaw = Pattern.compile(" "); + private static final Pattern patternSpaceEnc = Pattern.compile("%20"); + + /** + * Escapes characters not complying w/ RFC 2396 and the {@link URI#URI(String)} ctor. + * <ul> + * <li>SPACE -> %20</li> + * </ul> + */ + public static String encodeToURI(String s) { + return patternSpaceRaw.matcher(s).replaceAll("%20"); + } + + /** + * Reverses escaping of characters as performed via {@link #encodeToURI(String)}. + */ + public static String decodeFromURI(String s) { + return patternSpaceEnc.matcher(s).replaceAll(" "); + } + /** * Returns the connected URLConnection, or null if not url is not available */ |