Skip to content

Commit b633b83

Browse files
committed
Support invalid filename string to URI conversion
Not all filenames on all systems are valid URIs, especially on Windows, whose filenames have backslashes. If an error is thrown trying to create a URI from such an invalid String we attempt to resolve the issue by explicitly making it into a File first.
1 parent 746b74f commit b633b83

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/main/java/org/scijava/io/location/LocationService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ public interface LocationService extends HandlerService<URI, LocationResolver>,
5858
* @throws URISyntaxException if the URI is malformed
5959
*/
6060
default Location resolve(final String uriString) throws URISyntaxException {
61-
return resolve(new URI(uriString));
61+
try {
62+
return resolve(new URI(uriString));
63+
}
64+
catch (final URISyntaxException exc) {
65+
// In general, filenames are not valid URI strings.
66+
// Particularly on Windows, there are backslashes, which are invalid in URIs.
67+
// So we explicitly turn this string into a file if an error happens above.
68+
return resolve(new File(uriString).toURI());
69+
}
6270
}
6371

6472
/**

0 commit comments

Comments
 (0)