Repository: commons-io Updated Branches: refs/heads/master b1ee00d70 -> 31c0d3a8e
[IO-513] Add convenience methods for reading class path resources. Apply and evlove patch from Behrang Saeedzadeh. Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/31c0d3a8 Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/31c0d3a8 Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/31c0d3a8 Branch: refs/heads/master Commit: 31c0d3a8e94b1016843596acea17a63bed9d4558 Parents: b1ee00d Author: Gary Gregory <garydgreg...@gmail.com> Authored: Tue Nov 22 22:59:46 2016 -0800 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Tue Nov 22 22:59:46 2016 -0800 ---------------------------------------------------------------------- src/changes/changes.xml | 3 + .../java/org/apache/commons/io/IOUtils.java | 128 ++++++++++- .../org/apache/commons/io/IOUtilsTestCase.java | 212 +++++++++++++++++++ src/test/resources/test-file-simple-utf8.bin | 1 + 4 files changed, 343 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-io/blob/31c0d3a8/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 631fa64..9b93a64 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -86,6 +86,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-519" dev="jochen" type="add"> Add MessageDigestCalculatingInputStream </action> + <action issue="IO-513" dev="ggregory" type="add" due-to="Behrang Saeedzadeh"> + Add convenience methods for reading class path resources. + </action> <action issue="IO-514" dev="pschumacher" type="remove"> Remove org.apache.commons.io.Java7Support </action> http://git-wip-us.apache.org/repos/asf/commons-io/blob/31c0d3a8/src/main/java/org/apache/commons/io/IOUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 6c068c6..daa9505 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -134,7 +134,7 @@ public class IOUtils { static { // avoid security issues - try (final StringBuilderWriter buf = new StringBuilderWriter(4); + try (final StringBuilderWriter buf = new StringBuilderWriter(4); final PrintWriter out = new PrintWriter(buf)) { out.println(); LINE_SEPARATOR = buf.toString(); @@ -1249,6 +1249,132 @@ public class IOUtils { return new String(input, Charsets.toCharset(encoding)); } + // resources + //----------------------------------------------------------------------- + + /** + * Gets the contents of a classpath resource as a String using the + * specified character encoding. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @param encoding the encoding to use, null means platform default + * @return the requested String + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static String resourceToString(final String name, final Charset encoding) throws IOException { + return resourceToString(name, encoding, null); + } + + /** + * Gets the contents of a classpath resource as a String using the + * specified character encoding. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @param encoding the encoding to use, null means platform default + * @param classLoader the class loader that the resolution of the resource is delegated to + * @return the requested String + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static String resourceToString(final String name, final Charset encoding, ClassLoader classLoader) throws IOException { + return toString(resourceToURL(name, classLoader), encoding); + } + + /** + * Gets the contents of a classpath resource as a byte array. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @return the requested byte array + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static byte[] resourceToByteArray(final String name) throws IOException { + return resourceToByteArray(name, null); + } + + /** + * Gets the contents of a classpath resource as a byte array. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @param classLoader the class loader that the resolution of the resource is delegated to + * @return the requested byte array + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static byte[] resourceToByteArray(final String name, final ClassLoader classLoader) throws IOException { + return toByteArray(resourceToURL(name, classLoader)); + } + + /** + * Gets a URL pointing to the given classpath resource. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @return the requested URL + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static URL resourceToURL(final String name) throws IOException { + return resourceToURL(name, null); + } + + /** + * Gets a URL pointing to the given classpath resource. + * + * <p> + * It is expected the given <code>name</code> to be absolute. The + * behavior is not well-defined otherwise. + * </p> + * + * @param name name of the desired resource + * @param classLoader the class loader that the resolution of the resource is delegated to + * @return the requested URL + * @throws IOException if an I/O error occurs + * + * @since 2.6 + */ + public static URL resourceToURL(final String name, final ClassLoader classLoader) throws IOException { + // What about the thread context class loader? + // What about the system class loader? + final URL resource = classLoader == null ? IOUtils.class.getResource(name) : classLoader.getResource(name); + + if (resource == null) { + throw new IOException("Resource not found: " + name); + } + + return resource; + } + // readLines //----------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-io/blob/31c0d3a8/src/test/java/org/apache/commons/io/IOUtilsTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java index 1734b20..a29a41e 100644 --- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java +++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java @@ -1140,6 +1140,218 @@ public class IOUtilsTestCase extends FileBasedTestCase { testToString_URL(null); } + @Test public void testResourceToString_ExistingResourceAtRootPackage() throws Exception { + final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").getFile()).length(); + final String content = IOUtils.resourceToString("/test-file-simple-utf8.bin", StandardCharsets.UTF_8); + + assertNotNull(content); + assertEquals(fileSize, content.getBytes().length); + } + + @Test public void testResourceToString_ExistingResourceAtRootPackage_WithClassLoader() throws Exception { + final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").getFile()).length(); + final String content = IOUtils.resourceToString( + "test-file-simple-utf8.bin", + StandardCharsets.UTF_8, + ClassLoader.getSystemClassLoader() + ); + + assertNotNull(content); + assertEquals(fileSize, content.getBytes().length); + } + + @Test public void testResourceToString_ExistingResourceAtSubPackage() throws Exception { + final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); + final String content = IOUtils.resourceToString("/org/apache/commons/io/FileUtilsTestDataCR.dat", StandardCharsets.UTF_8); + + assertNotNull(content); + assertEquals(fileSize, content.getBytes().length); + } + + @Test public void testResourceToString_ExistingResourceAtSubPackage_WithClassLoader() throws Exception { + final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); + final String content = IOUtils.resourceToString( + "org/apache/commons/io/FileUtilsTestDataCR.dat", + StandardCharsets.UTF_8, + ClassLoader.getSystemClassLoader() + ); + + assertNotNull(content); + assertEquals(fileSize, content.getBytes().length); + } + + @Test(expected = IOException.class) public void testResourceToString_NonExistingResource() throws Exception { + IOUtils.resourceToString("/non-existing-file.bin", StandardCharsets.UTF_8); + } + + @Test(expected = IOException.class) public void testResourceToString_NonExistingResource_WithClassLoader() throws Exception { + IOUtils.resourceToString("non-existing-file.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()); + } + + @Test public void testResourceToString_NullResource() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToString(null, StandardCharsets.UTF_8); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + + @Test public void testResourceToString_NullResource_WithClassLoader() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + + @Test public void testResourceToString_NullCharset() throws Exception { + IOUtils.resourceToString("/test-file-utf8.bin", null); + } + + @Test public void testResourceToString_NullCharset_WithClassLoader() throws Exception { + IOUtils.resourceToString("test-file-utf8.bin", null, ClassLoader.getSystemClassLoader()); + } + + @Test public void testResourceToByteArray_ExistingResourceAtRootPackage() throws Exception { + final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").getFile()).length(); + final byte[] bytes = IOUtils.resourceToByteArray("/test-file-utf8.bin"); + assertNotNull(bytes); + assertEquals(fileSize, bytes.length); + } + + @Test public void testResourceToByteArray_ExistingResourceAtRootPackage_WithClassLoader() throws Exception { + final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").getFile()).length(); + final byte[] bytes = IOUtils.resourceToByteArray("test-file-utf8.bin", ClassLoader.getSystemClassLoader()); + assertNotNull(bytes); + assertEquals(fileSize, bytes.length); + } + + @Test public void testResourceToByteArray_ExistingResourceAtSubPackage() throws Exception { + final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); + final byte[] bytes = IOUtils.resourceToByteArray("/org/apache/commons/io/FileUtilsTestDataCR.dat"); + assertNotNull(bytes); + assertEquals(fileSize, bytes.length); + } + + @Test public void testResourceToByteArray_ExistingResourceAtSubPackage_WithClassLoader() throws Exception { + final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); + final byte[] bytes = IOUtils.resourceToByteArray("org/apache/commons/io/FileUtilsTestDataCR.dat", ClassLoader.getSystemClassLoader()); + assertNotNull(bytes); + assertEquals(fileSize, bytes.length); + } + + @Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource() throws Exception { + IOUtils.resourceToByteArray("/non-existing-file.bin"); + } + + @Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource_WithClassLoader() throws Exception { + IOUtils.resourceToByteArray("non-existing-file.bin", ClassLoader.getSystemClassLoader()); + } + + @Test public void testResourceToByteArray_Null() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToByteArray(null); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + + @Test public void testResourceToByteArray_Null_WithClassLoader() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader()); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + + @Test public void testResourceToURL_ExistingResourceAtRootPackage() throws Exception { + final URL url = IOUtils.resourceToURL("/test-file-utf8.bin"); + assertNotNull(url); + assertTrue(url.getFile().endsWith("/test-file-utf8.bin")); + } + + @Test public void testResourceToURL_ExistingResourceAtRootPackage_WithClassLoader() throws Exception { + final URL url = IOUtils.resourceToURL("test-file-utf8.bin", ClassLoader.getSystemClassLoader()); + assertNotNull(url); + assertTrue(url.getFile().endsWith("/test-file-utf8.bin")); + } + + @Test public void testResourceToURL_ExistingResourceAtSubPackage() throws Exception { + final URL url = IOUtils.resourceToURL("/org/apache/commons/io/FileUtilsTestDataCR.dat"); + assertNotNull(url); + assertTrue(url.getFile().endsWith("/org/apache/commons/io/FileUtilsTestDataCR.dat")); + } + + @Test public void testResourceToURL_ExistingResourceAtSubPackage_WithClassLoader() throws Exception { + final URL url = IOUtils.resourceToURL( + "org/apache/commons/io/FileUtilsTestDataCR.dat", + ClassLoader.getSystemClassLoader() + ); + + assertNotNull(url); + assertTrue(url.getFile().endsWith("/org/apache/commons/io/FileUtilsTestDataCR.dat")); + } + + @Test(expected = IOException.class) public void testResourceToURL_NonExistingResource() throws Exception { + IOUtils.resourceToURL("/non-existing-file.bin"); + } + + @Test(expected = IOException.class) public void testResourceToURL_NonExistingResource_WithClassLoader() throws Exception { + IOUtils.resourceToURL("non-existing-file.bin", ClassLoader.getSystemClassLoader()); + } + + @Test public void testResourceToURL_Null() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToURL(null); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + + @Test public void testResourceToURL_Null_WithClassLoader() throws Exception { + boolean exceptionOccurred = false; + + try { + IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader()); + fail(); + } catch (NullPointerException npe) { + exceptionOccurred = true; + assertNotNull(npe); + } + + assertTrue(exceptionOccurred); + } + @Test public void testAsBufferedNull() { try { IOUtils.buffer((InputStream) null); http://git-wip-us.apache.org/repos/asf/commons-io/blob/31c0d3a8/src/test/resources/test-file-simple-utf8.bin ---------------------------------------------------------------------- diff --git a/src/test/resources/test-file-simple-utf8.bin b/src/test/resources/test-file-simple-utf8.bin new file mode 100644 index 0000000..83871a5 --- /dev/null +++ b/src/test/resources/test-file-simple-utf8.bin @@ -0,0 +1 @@ +ABC