/** * Extracts the resource to a file. * * @param clazz * The class, relative to which path is resolved * @param path * The path to the resource * @param file * The file to extract to * @throws IOException * If an error occurs reading it */ public static void extractResourceToFile(final Class<?> clazz, final String path, final File file) throws IOException { final InputStream in = getResourceAsStream(clazz, path); if (in == null) { throw new IOException("Missing resource: " + path); } OutputStream out = null; try { out = new FileOutputStream(file); IOUtil.copyFile(in, out); } finally { if (out != null) { out.close(); } in.close(); } }
/** * Extracts the resource to a file. * * @param clazz * The class, relative to which path is resolved * @param path * The path to the resource * @param file * The file to extract to * @throws IOException * If an error occurs reading it */ public static void extractResourceToFile(final Class<?> clazz, final String path, final File file) throws IOException { final InputStream in = getResourceAsStream(clazz, path); if (in == null) { throw new IOException("Missing resource: " + path); } OutputStream out = null; try { out = new FileOutputStream(file); IOUtil.copyFile(in, out); } finally { if (out != null) { out.close(); } in.close(); } }
String path, File file) throws IOException { InputStream in = getResourceAsStream(clazz, path); if ( in == null ) { throw new IOException("Missing resource: "+path);
/** * Get the resource as a byte array. */ public static byte[] getResourceAsBytes(Class<?> clazz, String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Get the resource as a byte array. * * @param clazz * @param res * @return */ public static byte[] getResourceAsBytes(final Class<?> clazz, final String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. final InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } final byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Read the entire stream into a String and return it. */ public static String getResourceAsString(Class<?> clazz, String res, Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }
/** * Read the entire stream into a String and return it. * * @param clazz * @param res * @param charset * @return */ public static String getResourceAsString(final Class<?> clazz, final String res, final Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; final InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { final InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }
/** * Get the resource as a byte array. * * @param clazz * @param res * @return */ public static byte[] getResourceAsBytes(final Class<?> clazz, final String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. final InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } final byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Read the entire stream into a String and return it. * * @param clazz * @param res * @param charset * @return */ public static String getResourceAsString(final Class<?> clazz, final String res, final Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; final InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { final InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }