-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce robust URL to java.nio.file.Path conversion #691
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
import java.io.File; | ||
import java.net.*; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* A utility class for manipulating URIs. This class works around some of the | ||
|
@@ -312,11 +313,12 @@ public static URI toURI(URL url) throws URISyntaxException { | |
// URL behaves differently across platforms so for file: URLs we parse from | ||
// string form | ||
if (SCHEME_FILE.equals(url.getProtocol())) { | ||
String pathString = url.toExternalForm().substring(5); | ||
String pathString = url.toExternalForm().substring(SCHEME_FILE.length() + 1); | ||
// ensure there is a leading slash to handle common malformed URLs such as | ||
// file:c:/tmp | ||
if (pathString.indexOf('/') != 0) | ||
if (!pathString.startsWith("/")) { //$NON-NLS-1$ | ||
pathString = '/' + pathString; | ||
} | ||
return toURI(SCHEME_FILE, null, pathString, null); | ||
} | ||
try { | ||
|
@@ -328,6 +330,33 @@ public static URI toURI(URL url) throws URISyntaxException { | |
} | ||
} | ||
|
||
/** | ||
* Returns the {@code Path} specified by the given {@code file} {@link URL}. | ||
* | ||
* @throws IllegalArgumentException if the specified URL does not use the | ||
* {@code file} protocol | ||
* | ||
* @since 3.20 | ||
*/ | ||
public static Path toFilePath(URL url) { | ||
if (!SCHEME_FILE.equals(url.getProtocol())) { | ||
throw new IllegalArgumentException("Not a 'file' url: " + url); //$NON-NLS-1$ | ||
} | ||
Comment on lines
+342
to
+344
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively this method could return an empty optional if the URL is not a file-URL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional seem much nicer, calles can throw exception if the want anyways with Then we can easily handel other error conditions here as well for example if invalid characters are in the path. |
||
try { | ||
return Path.of(url.toURI()); | ||
} catch (URISyntaxException | IllegalArgumentException e) { | ||
try { | ||
return Path.of(toURI(url)); | ||
} catch (URISyntaxException e1) { | ||
String pathString = url.toExternalForm().substring(SCHEME_FILE.length() + 1); | ||
if (pathString.length() > 1 && pathString.charAt(0) == '/' && pathString.charAt(1) != '/') { | ||
pathString = pathString.substring(1); | ||
} | ||
return Path.of(pathString); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* An UNC-safe factory the the URI-ctor | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That the name contains
File
andPath
seems to be a duplication at first, but since any kind of URL can have a path I think it should be made clear that it's a file-system path (although nio-Path can also handle non-classical file-systems).On the other hand
toFileSystemPath()
seemed to be a bit verbose to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only food for thought, this suggestion isn't common convention. Could use
Path fromFileUrl(URL url)
.Cannot say I like that, but it seems to make it clear.