Skip to content

Commit 74163ef

Browse files
committed
Rework resources to handle uri
1 parent 5ba7883 commit 74163ef

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/main/java/org/apache/ibatis/migration/io/Resources.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.io.InputStream;
2121
import java.io.InputStreamReader;
2222
import java.io.Reader;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
2325
import java.net.URL;
2426
import java.net.URLConnection;
2527
import java.nio.charset.Charset;
26-
import java.nio.file.Paths;
28+
import java.nio.file.Path;
2729
import java.util.Properties;
2830

2931
/**
@@ -60,7 +62,7 @@ public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
6062
}
6163

6264
/**
63-
* Returns the URL of the resource on the classpath
65+
* Returns the URI of the resource on the classpath
6466
*
6567
* @param resource
6668
* The resource to find
@@ -70,13 +72,13 @@ public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
7072
* @throws java.io.IOException
7173
* If the resource cannot be found or read
7274
*/
73-
public static URL getResourceURL(String resource) throws IOException {
75+
public static URI getResourceURI(String resource) throws IOException {
7476
// issue #625
75-
return getResourceURL(null, resource);
77+
return getResourceURI(null, resource);
7678
}
7779

7880
/**
79-
* Returns the URL of the resource on the classpath
81+
* Returns the URI of the resource on the classpath
8082
*
8183
* @param loader
8284
* The classloader used to fetch the resource
@@ -88,12 +90,16 @@ public static URL getResourceURL(String resource) throws IOException {
8890
* @throws java.io.IOException
8991
* If the resource cannot be found or read
9092
*/
91-
public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
93+
public static URI getResourceURI(ClassLoader loader, String resource) throws IOException {
9294
URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
9395
if (url == null) {
9496
throw new IOException("Could not find resource " + resource);
9597
}
96-
return url;
98+
try {
99+
return url.toURI();
100+
} catch (URISyntaxException e) {
101+
throw new IOException("Could not find resource " + resource);
102+
}
97103
}
98104

99105
/**
@@ -228,7 +234,7 @@ public static Reader getResourceAsReader(ClassLoader loader, String resource) th
228234
* If the resource cannot be found or read
229235
*/
230236
public static File getResourceAsFile(String resource) throws IOException {
231-
return Paths.get(getResourceURL(resource).getFile()).toFile();
237+
return Path.of(getResourceURI(resource)).toFile();
232238
}
233239

234240
/**
@@ -245,7 +251,7 @@ public static File getResourceAsFile(String resource) throws IOException {
245251
* If the resource cannot be found or read
246252
*/
247253
public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
248-
return Paths.get(getResourceURL(loader, resource).getFile()).toFile();
254+
return Path.of(getResourceURI(loader, resource)).toFile();
249255
}
250256

251257
/**

0 commit comments

Comments
 (0)