diff --git a/lang/src/main/java/org/apache/shiro/lang/io/ResourceUtils.java b/lang/src/main/java/org/apache/shiro/lang/io/ResourceUtils.java index b7c241cc1f..3a1a224cb4 100644 --- a/lang/src/main/java/org/apache/shiro/lang/io/ResourceUtils.java +++ b/lang/src/main/java/org/apache/shiro/lang/io/ResourceUtils.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.file.Paths; /** * Static helper methods for loading {@code Stream}-backed resources. @@ -120,7 +121,6 @@ public static boolean resourceExists(String resourcePath) { * @throws IOException if there is a problem acquiring the resource at the specified path. */ public static InputStream getInputStreamForPath(String resourcePath) throws IOException { - URL url = getURLForPath(resourcePath); if (url == null) { throw new IOException("Resource [" + resourcePath + "] could not be found."); @@ -135,7 +135,7 @@ public static InputStream getInputStreamForPath(String resourcePath) throws IOEx * ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX}, * {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}). If the path is not prefixed by one * of these schemes, the path is assumed to be a file-based path that can be loaded with a - * call to {@link URI#create(String)}. + * call to {@link Paths#get(String, String...)}. * * @param resourcePath the String path representing the resource to obtain. * @return the URL for the specified resource. @@ -148,8 +148,10 @@ public static URL getURLForPath(String resourcePath) throws IOException { url = ClassUtils.getResource(stripPrefix(resourcePath)); } else if (resourcePath.startsWith(URL_PREFIX)) { url = URI.create(stripPrefix(resourcePath)).toURL(); + } else if (resourcePath.startsWith(FILE_PREFIX)) { + url = Paths.get(stripPrefix(resourcePath)).toUri().toURL(); } else { - url = URI.create(resourcePath).toURL(); + url = Paths.get(resourcePath).toUri().toURL(); } if (url == null) { diff --git a/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java b/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java index e1988276ee..d190f6dd7f 100644 --- a/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java +++ b/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java @@ -20,13 +20,14 @@ import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.io.InputStream; +import static org.apache.shiro.lang.io.ResourceUtils.getInputStreamForPath; import static org.assertj.core.api.Assertions.assertThat; class ClassUtilsTest { - @Test void testGetPrimitiveClasses() throws UnknownClassException { - assertThat(ClassUtils.forName("boolean")).isEqualTo(boolean.class); assertThat(ClassUtils.forName("byte")).isEqualTo(byte.class); assertThat(ClassUtils.forName("char")).isEqualTo(char.class); @@ -84,4 +85,17 @@ void testGetClass() { assertThat(ClassUtils.forName(ClassUtilsTest.class.getName())).isEqualTo(ClassUtilsTest.class); assertThat(ClassUtils.forName(ClassUtilsTest[].class.getName())).isEqualTo(ClassUtilsTest[].class); } + + @Test + void inputStreamFileLoading() throws IOException { + try (InputStream is = getInputStreamForPath("classpath:org/apache/shiro/lang/util/ClassUtilsTest.class")) { + assertThat(is.readAllBytes()).isNotEmpty(); + } + try (InputStream is = getInputStreamForPath("target/test-classes/test-data/file.json")) { + assertThat(is.readAllBytes()).isNotEmpty(); + } + try (InputStream is = getInputStreamForPath("file:target/test-classes/test-data/file.json")) { + assertThat(is.readAllBytes()).isNotEmpty(); + } + } } diff --git a/lang/src/test/resources/test-data/file.json b/lang/src/test/resources/test-data/file.json new file mode 100644 index 0000000000..16d8c22fa0 --- /dev/null +++ b/lang/src/test/resources/test-data/file.json @@ -0,0 +1,4 @@ +{ + "id": 1, + "name": "example" +}