/** * 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 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, final 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 * @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)); }
@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_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 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_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_Null_WithClassLoader() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader()); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
@Test public void testResourceToURL_Null() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToURL(null); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
/** * 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 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 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, final ClassLoader classLoader) throws IOException { return toString(resourceToURL(name, classLoader), encoding); }