List of usage examples for com.google.common.io Closer close
@Override public void close() throws IOException
From source file:com.facebook.buck.rules.MergeAndroidResourcesStep.java
public static String generateJavaCodeForPackageAndResources(String packageName, SortedSet<Resource> resources) { StringBuilder b = new StringBuilder(); Closer closer = Closer.create(); PrintWriter writer = closer.register(new PrintWriter(CharStreams.asWriter(b))); try {/* w w w. ja v a 2s . c o m*/ writeJavaCodeForPackageAndResources(writer, packageName, resources); } catch (IOException e) { // Impossible. throw new RuntimeException(e); } finally { try { closer.close(); } catch (IOException e) { Throwables.propagate(e); } } return b.toString(); }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.util.VFSZipper.java
public static void zip(FileObject root, List<FileObject> files, OutputStream out) throws IOException { String basePath = root.getName().getPath(); Closer closer = Closer.create(); try {/*from w w w. jav a 2 s . c om*/ ZipOutputStream zos = new ZipOutputStream(out); closer.register(zos); for (FileObject fileToCopy : files) { ZipEntry zipEntry = zipEntry(basePath, fileToCopy); zos.putNextEntry(zipEntry); copyFileContents(fileToCopy, zos); zos.flush(); zos.closeEntry(); } } catch (IOException e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.clank.launcher.LauncherUtils.java
public static Properties loadProperties(Class<?> clazz, String name, String extraProperty) throws IOException { Closer closer = Closer.create(); Properties prop = new Properties(); try {/* w ww . ja v a 2 s. c o m*/ InputStream in = closer.register(clazz.getResourceAsStream(name)); prop.load(in); String extraPath = System.getProperty(extraProperty); if (extraPath != null) { log.info("Loading extra properties for " + clazz.getCanonicalName() + ":" + name + " from " + extraPath + "..."); in = closer.register(new BufferedInputStream(closer.register(new FileInputStream(extraPath)))); prop.load(in); } } finally { closer.close(); } return prop; }
From source file:org.stem.db.FatFileAllocator.java
public static void allocateFile(String filePath, long sizeInMB, boolean mark) throws IOException { long started = System.currentTimeMillis(); Closer closer = Closer.create(); try {//from w w w. j av a2 s . co m File file = new File(filePath); if (file.exists()) throw new IOException(String.format("File already exists: %s", filePath)); RandomAccessFile rw = closer.register(new RandomAccessFile(file, "rw")); rw.setLength(sizeInMB * 1024 * 1024); if (mark) { rw.seek(0); rw.writeByte(FatFile.MARKER_BLANK); rw.seek(rw.length() - 1); rw.writeByte(FatFile.MARKER_BLANK); } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); logger.debug("{} was allocated in {} ms", filePath, System.currentTimeMillis() - started); } }
From source file:utils.PasswordManager.java
public static Optional<String> getMasterPassword(String masterPwdLoc) { Closer closer = Closer.create(); try {/*from w w w .j a v a 2 s . c om*/ File file = new File(masterPwdLoc); if (!file.exists() || file.isDirectory()) { LOG.warn(masterPwdLoc + " does not exist or is not a file. Cannot decrypt any encrypted password."); return Optional.absent(); } InputStream in = new FileInputStream(file); return Optional.of(new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine()); } catch (IOException e) { throw new RuntimeException("Failed to obtain master password from " + masterPwdLoc, e); } finally { try { closer.close(); } catch (IOException e) { throw new RuntimeException("Failed to close inputstream for " + masterPwdLoc, e); } } }
From source file:tachyon.util.CommonUtils.java
/** * Blocking operation that copies the processes stdout/stderr to this JVM's stdout/stderr. */// w w w. j a v a 2s . co m private static void redirectIO(final Process process) throws IOException { // Because chmod doesn't have a lot of error or output messages, its safe to process the output // after the process is done. As of java 7, you can have the process redirect to System.out // and System.err without forking a process. // TODO when java 6 support is dropped, switch to // http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO() Closer closer = Closer.create(); try { ByteStreams.copy(closer.register(process.getInputStream()), System.out); ByteStreams.copy(closer.register(process.getErrorStream()), System.err); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.google.auto.service.processor.ServicesFiles.java
/** * Reads the set of service classes from a service file. * * @param input not {@code null}. Closed after use. * @return a not {@code null Set} of service class names. * @throws IOException//w ww . ja va2s . c o m */ static Set<String> readServiceFile(InputStream input) throws IOException { HashSet<String> serviceClasses = new HashSet<String>(); Closer closer = Closer.create(); try { // TODO(gak): use CharStreams BufferedReader r = closer.register(new BufferedReader(new InputStreamReader(input, UTF_8))); String line; while ((line = r.readLine()) != null) { int commentStart = line.indexOf('#'); if (commentStart >= 0) { line = line.substring(0, commentStart); } line = line.trim(); if (!line.isEmpty()) { serviceClasses.add(line); } } return serviceClasses; } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:org.jmattr.meta.impl.ServicesFiles.java
/** * Reads the set of service classes from a service file. * * @param input not {@code null}. Closed after use. * @return a not {@code null Set} of service class names. * @throws IOException/*www .j a v a 2 s . c o m*/ */ static Set<String> readServiceFile(InputStream input) throws IOException { HashSet<String> serviceClasses = new HashSet<String>(); Closer closer = Closer.create(); try { // TODO(gak): use CharStreams BufferedReader r = closer.register(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8))); String line; while ((line = r.readLine()) != null) { int commentStart = line.indexOf('#'); if (commentStart >= 0) { line = line.substring(0, commentStart); } line = line.trim(); if (!line.isEmpty()) { serviceClasses.add(line); } } return serviceClasses; } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:com.github.sdorra.buildfrontend.AbstractNodeMojo.java
/** * Method description/* w w w. jav a 2 s . c o m*/ * * * @param urlString * @param target * * @throws IOException */ private static void download(String urlString, File target) throws IOException { URL url = new URL(urlString); URLConnection connection = url.openConnection(); Closer closer = Closer.create(); try { InputStream input = closer.register(connection.getInputStream()); OutputStream output = closer.register(new FileOutputStream(target)); ByteStreams.copy(input, output); } catch (IOException ex) { throw closer.rethrow(ex); } finally { closer.close(); } }
From source file:net.usikkert.kouchat.android.util.FileUtils.java
private static void copyFileToDevice(final File fileToStore, final Instrumentation instrumentation) { final Closer closer = Closer.create(); final AssetManager assets = instrumentation.getContext().getResources().getAssets(); try {//ww w . ja v a 2 s .c o m final InputStream inputStream = closer.register(assets.open(fileToStore.getName())); final FileOutputStream outputStream = closer.register(new FileOutputStream(fileToStore)); ByteStreams.copy(inputStream, outputStream); outputStream.flush(); assertTrue("Should exist: " + fileToStore, fileToStore.exists()); } catch (IOException e) { throw new RuntimeException(e); } finally { try { closer.close(); } catch (IOException e) { e.printStackTrace(); } } }