public static long checksum(File file) throws java.io.IOException, FileNotFoundException { FileInputStream fis = null; byte[] bytes = new byte[16384]; int len; try { fis = new FileInputStream(file); CRC32 chkSum = new CRC32(); len = fis.read(bytes); while (len != -1) { chkSum.update(bytes, 0, len); len = fis.read(bytes); } return chkSum.getValue(); } finally { quietClose(fis); } }
ret = copyFile(fis, fout) > 0; } finally { quietClose(fis); quietClose(fout);
/** * Attempt to load file based on a string base filename. * * @param string * represents the file. * @return a loaded properties file. * @throws IOException * if there is an issue. */ public static Properties loadPropertiesFile(final String string) throws IOException { return loadPropertiesFile(new File(string)); } }
/** * 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; }
/** * 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; }
/** * 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(); } }
IOUtil.quietClose(fis); final String clazz = IOUtil.getResourceAsString(Log.class, serviceId); if (StringUtil.isNotBlank(clazz)) { return forName(clazz.trim());
return; Properties properties = IOUtil.loadPropertiesFile(propertiesFileName); run(properties); } else if (cmd.equalsIgnoreCase("-setkey")) { return; Properties properties = IOUtil.loadPropertiesFile(propertiesFileName); properties.put(PROP_KEY, SecurityUtil.computeBase64SHA1Hash(key.toCharArray())); IOUtil.storePropertiesFile(new File(propertiesFileName), properties); } else if (cmd.equalsIgnoreCase("-setDefaults")) { if (propertiesFileName == null || key != null) { return; IOUtil.extractResourceToFile(Main.class, "connectorserver.properties", new File(propertiesFileName)); } else { usage();
private void copyStream(final InputStream stream, final File toFile) throws IOException { try (FileOutputStream out = new FileOutputStream(toFile)) { IOUtil.copyFile(stream, out); } }
/** * Read the entire stream into a String and return it. * * @param clazz * @param res * @return */ public static String getResourceAsString(final Class<?> clazz, final String res) { assert clazz != null && StringUtil.isNotBlank(res); return getResourceAsString(clazz, res, Charset.forName("UTF-8")); }
/** * Reads the given file as bytes * * @param file * The file to read * @return The contents of the file * @throws IOException * if there is an issue reading the file. */ public static byte[] readFileBytes(final File file) throws IOException { final InputStream ins = new FileInputStream(file); return IOUtil.readInputStreamBytes(ins, true); }
@Test public void getReader() { StringWriter wrt = new StringWriter(); StringPrintWriter pwrt = new StringPrintWriter(); for (String data : DATA) { wrt.append(data); pwrt.append(data); } // get the string of the reader.. String actual = IOUtil.readerToString(pwrt.getReader()); assertEquals(actual, wrt.toString()); }
/** * Calculates the CRC32 checksum of the specified file. * * @param fileName the path to the file on which to calculate the checksum * @return */ public static long checksum(final String fileName) throws IOException, FileNotFoundException { return (checksum(new File(fileName))); }
/** * Recursively delete all the files in a directory and the directory. * * @throws RuntimeException * iff there is file that can not be deleted. */ public static void delete(File f) throws IOException { // determine if the file/directory exists.. if (f.exists()) { final String msg = "Failed to delete: " + f; // determine if this is a directory or file.. if (f.isDirectory()) { // delete all the files/directories File[] fs = f.listFiles(); for (int x = 0; x < fs.length; x++) { delete(fs[x]); } // delete the directory if (!f.delete()) { throw new IOException(msg); } } else if (f.isFile() && !f.delete()) { // failed to delete file.. throw new IOException(msg); } } }
/** * 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; }
IOUtil.quietClose(fis); String clazz = IOUtil.getResourceAsString(Log.class, serviceId); if (StringUtil.isNotBlank(clazz)) { return forName(clazz.trim());
IOUtil.loadPropertiesFile(propertiesFileName); run(properties); } else if (cmd.equalsIgnoreCase("-setkey")) { return; Properties properties = IOUtil.loadPropertiesFile(propertiesFileName); properties.put(PROP_KEY, SecurityUtil.computeBase64SHA1Hash(key.toCharArray())); IOUtil.storePropertiesFile(new File(propertiesFileName), properties); } else if (cmd.equalsIgnoreCase("-setDefaults")) { if (propertiesFileName == null || key != null) { return; IOUtil.extractResourceToFile(Main.class, "connectorserver.properties", new File(propertiesFileName));
/** * 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(); } }
private void copyStream(final InputStream stream, final File toFile) throws IOException { try (FileOutputStream out = new FileOutputStream(toFile)) { IOUtil.copyFile(stream, out); } }