/** * 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); }
@Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource_WithClassLoader() throws Exception { IOUtils.resourceToByteArray("non-existing-file.bin", ClassLoader.getSystemClassLoader()); }
@Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource() throws Exception { IOUtils.resourceToByteArray("/non-existing-file.bin"); }
@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_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 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_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_Null_WithClassLoader() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader()); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
@Test public void testResourceToByteArray_Null() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToByteArray(null); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
/** * 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); }
private static X509Certificate getCertificate(String filePath) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { byte[] bytes = IOUtils.resourceToByteArray(filePath, loader); return X509CertUtils.parse(bytes); } catch (IOException e) { log.debug(e.getMessage()); } return null; }
private static X509Certificate getCertificate(String filePath) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { byte[] bytes = IOUtils.resourceToByteArray(filePath, loader); return X509CertUtils.parse(bytes); } catch (IOException e) { log.debug(e.getMessage()); } return null; }
public static String getCertificateByName(String filename) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { byte[] bytes = IOUtils.resourceToByteArray("certificates/" + filename, loader); X509Certificate cert = X509CertUtils.parse(bytes); return X509CertUtils.toPEMString(cert); } catch (IOException e) { log.debug(e.getMessage()); } return null; }
public static String getCertificateByName(String filename) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { byte[] bytes = IOUtils.resourceToByteArray("certificates/" + filename, loader); X509Certificate cert = X509CertUtils.parse(bytes); return X509CertUtils.toPEMString(cert); } catch (IOException e) { log.debug(e.getMessage()); } return null; }