private void destroy() { // close input stream which will normally interrupt the reader Closeables.closeQuietly(inputStream); if (outputFuture != null) { outputFuture.cancel(true); } } }
static void copy(InputStream is, OutputStream os) throws IOException { try { byte[] b = new byte[4096]; int l = is.read(b); while (l >= 0) { os.write(b, 0, l); l = is.read(b); } } finally { Closeables.closeQuietly(os); } }
/** * Copy the {@link InputStream} to the {@link OutputStream}. */ public static void copy(InputStream is, OutputStream os) throws IOException { try { byte[] buffer = new byte[BUFFER_SIZE]; int len; for (; ; ) { len = is.read(buffer); if (len > 0) { os.write(buffer, 0, len); } else { break; } } } finally { Closeables.closeQuietly(is); Closeables.closeQuietly(os); } }
/** * Reads a {@link File} and returns the data as a byte array */ public static byte[] readBytes(File file) throws IOException { FileInputStream fis = null; ByteArrayOutputStream bos = null; if (file == null) { throw new FileNotFoundException("No file specified"); } try { fis = new FileInputStream(file); bos = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int remaining; while ((remaining = fis.read(buffer)) > 0) { bos.write(buffer, 0, remaining); } return bos.toByteArray(); } finally { Closeables.closeQuietly(fis); Closeables.closeQuietly(bos); } }
/** * Reads an {@link InputStream} and returns the data as a byte array */ public static byte[] readBytes(InputStream in) throws IOException { ByteArrayOutputStream bos = null; if (in == null) { throw new FileNotFoundException("No InputStream specified"); } try { bos = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int remaining; while ((remaining = in.read(buffer)) > 0) { bos.write(buffer, 0, remaining); } return bos.toByteArray(); } finally { Closeables.closeQuietly(in); Closeables.closeQuietly(bos); } }
/** * Writes {@link String} content to {@link File}. */ public static void writeToFile(File file, byte[] content) throws IOException { FileOutputStream fos = null; try { if (file == null) { throw new FileNotFoundException("No file specified."); } else if (!file.exists() && !file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new FileNotFoundException("Could not find or create file:" + file.getName()); } fos = new FileOutputStream(file); fos.write(content); } finally { Closeables.closeQuietly(fos); } }
public static void createZipFile(Logger log, File sourceDir, File outputZipFile, FileFilter filter) throws IOException { outputZipFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(outputZipFile); ZipOutputStream zos = new ZipOutputStream(os); try { //zos.setLevel(Deflater.DEFAULT_COMPRESSION); //zos.setLevel(Deflater.NO_COMPRESSION); String path = ""; zipDirectory(log, sourceDir, zos, path, filter); } finally { Closeables.closeQuietly(zos); } }
protected static void processOutput(InputStream inputStream, Function<String, Void> function, String errrorMessage) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { while (true) { String line = reader.readLine(); if (line == null) break; function.apply(line); } } catch (Exception e) { LOG.error("Failed to process " + errrorMessage + ": " + e, e); throw e; } finally { Closeables.closeQuietly(reader); } } }
/** * Writes {@link String} content to {@link File}. */ public static void writeToFile(File file, String content, Charset charset) throws IOException { FileOutputStream fos = null; OutputStreamWriter writer = null; try { if (file == null) { throw new FileNotFoundException("No file specified."); } else if (!file.exists() && !file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new FileNotFoundException("Could not find or create file:" + file.getName()); } fos = new FileOutputStream(file); writer = new OutputStreamWriter(fos, charset); writer.write(content, 0, content.length()); writer.flush(); } finally { Closeables.closeQuietly(fos); Closeables.closeQuietly(writer); } }
/** * Substitutes a placeholder with the checksum:[url] format with the checksum of the urls target. * @return The checksum or 0. */ public static String substituteChecksum(String key) { InputStream is = null; try { URL url = new URL(key.substring("checksum:".length())); is = url.openStream(); return String.valueOf(ChecksumUtils.checksum(is)); } catch (Exception ex) { return "0"; } finally { Closeables.closeQuietly(is); } }
@Override public String call() throws IOException { StringBuffer buffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8)); try { while (true) { String line = reader.readLine(); if (line == null) { break; } buffer.append(line).append("\n"); System.out.println(line); } return buffer.toString(); } finally { Closeables.closeQuietly(reader); } } });
public void destroy() { if (active.compareAndSet(true, false)) { treeCache.getListenable().removeListener(treeListener); Closeables.closeQuietly(treeCache); treeCache = null; treeCacheExecutor.shutdownNow(); } }
/** * Writes the ports to the ports.properties file */ public static void writePorts(File portFile, Map<String, String> portMap) throws IOException { // lets add the old ports if there were any Map<String, String> oldPorts = readPorts(portFile); // new ports should override the old Map<String, String> fullPortMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); fullPortMap.putAll(oldPorts); fullPortMap.putAll(portMap); PrintStream writer = new PrintStream(new FileOutputStream(portFile)); try { writer.println(); Set<Map.Entry<String, String>> entries = fullPortMap.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); writer.println(name.toUpperCase() + " = " + value); } writer.println(); } finally { Closeables.closeQuietly(writer); } }
/** * Unzips the given input stream of a ZIP to the given directory */ public static void unzip(InputStream in, File toDir) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in)); try { ZipEntry entry = zis.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { String entryName = entry.getName(); File toFile = new File(toDir, entryName); toFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(toFile); try { try { copy(zis, os); } finally { zis.closeEntry(); } } finally { Closeables.closeQuietly(os); } } entry = zis.getNextEntry(); } } finally { closeQuietly(zis); } }
/** * Writes the ports to the ports.properties file */ public static void writePorts(File portFile, Map<String, String> portMap) throws IOException { // lets add the old ports if there were any Map<String, String> oldPorts = readPorts(portFile); // new ports should override the old Map<String, String> fullPortMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); fullPortMap.putAll(oldPorts); fullPortMap.putAll(portMap); PrintStream writer = new PrintStream(new FileOutputStream(portFile)); try { writer.println(); Set<Map.Entry<String, String>> entries = fullPortMap.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); writer.println(name.toUpperCase() + " = " + value); } writer.println(); } finally { Closeables.closeQuietly(writer); } }
@PreDestroy public void destroy() { disableTimer(); group.remove(groupListener); Closeables.closeQuietly(group); group = null; disableTimer(); }
throw e; } finally { Closeables.closeQuietly(reader);
protected static void processOutput(Logger logger, InputStream inputStream, boolean error) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { while (true) { String line = reader.readLine(); if (line == null) break; if (error) { logger.error(line); } else { logger.info(line); } } } catch (Exception e) { logger.error("Failed to process " + (error ? "stderr" : "stdout") + ": " + e); throw e; } finally { Closeables.closeQuietly(reader); } }