List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:com.eucalyptus.reporting.export.Import.java
public static ImportResult importData(final File exportFile, final Runnable preImportCallback) throws Exception { FileInputStream in = null;/*from w w w . ja v a 2 s. com*/ try { in = new FileInputStream(exportFile); return importData(in, preImportCallback); } finally { Closeables.closeQuietly(in); } }
From source file:com.metamx.druid.utils.SerializerUtils.java
public void writeString(OutputSupplier<? extends OutputStream> supplier, String name) throws IOException { OutputStream out = null;//from w w w .ja v a2s. c om try { out = supplier.getOutput(); writeString(out, name); } finally { Closeables.closeQuietly(out); } }
From source file:org.apache.mahout.graph.preprocessing.GraphUtils.java
public static List<Integer> readVerticesIndexes(Configuration conf, Path path) throws IOException { List<Integer> indexes = Lists.newArrayList(); InputStream in = HadoopUtil.openStream(path, conf); try {/* www.j a v a2 s .com*/ for (String line : new FileLineIterable(in)) { indexes.add(Integer.parseInt(line)); } } finally { Closeables.closeQuietly(in); } return indexes; }
From source file:org.jclouds.io.CopyInputStreamIntoSupplier.java
@SuppressWarnings("unchecked") @Override/*from www . j av a2 s . c om*/ public InputSupplier<InputStream> apply(InputStream from) { if (from == null) return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return null; } }; try { return InputSupplier.class.cast(ByteStreams.newInputStreamSupplier(ByteStreams.toByteArray(from))); } catch (Exception e) { logger.warn(e, "ignoring problem retrieving credentials"); return null; } finally { Closeables.closeQuietly(from); } }
From source file:com.github.benmanes.caffeine.cache.simulator.parser.cache2k.Cache2kTraceReader.java
@Override public LongStream events() throws IOException { DataInputStream input = new DataInputStream(new BufferedInputStream(readFiles())); LongStream stream = StreamSupport.longStream( Spliterators.spliteratorUnknownSize(new TraceIterator(input), Spliterator.ORDERED), /* parallel */ false); return stream.onClose(() -> Closeables.closeQuietly(input)); }
From source file:co.cask.cdap.internal.app.runtime.artifact.ArtifactClassLoaderFactory.java
CloseableClassLoader createClassLoader(Location artifactLocation) throws IOException { final File unpackDir = DirUtils.createTempDir(baseUnpackDir); BundleJarUtil.unpackProgramJar(artifactLocation, unpackDir); final ProgramClassLoader programClassLoader = ProgramClassLoader.create(unpackDir, getClass().getClassLoader()); return new CloseableClassLoader(programClassLoader, new Closeable() { @Override/*from w ww . ja va 2s.c o m*/ public void close() { try { Closeables.closeQuietly(programClassLoader); DirUtils.deleteDirectoryContents(unpackDir); } catch (IOException e) { LOG.warn("Failed to delete directory {}", unpackDir, e); } } }); }
From source file:eu.redzoo.reactive.kafka.rest.Environment.java
private static ImmutableMap<String, String> loadProperties(URL url) { Map<String, String> configs = Maps.newHashMap(); InputStream is = null;/*from w ww. j a v a 2 s .c o m*/ try { is = url.openStream(); Properties props = new Properties(); props.load(is); props.forEach((key, value) -> configs.put(key.toString(), value.toString())); } catch (IOException ioe) { LOG.warning("error occured reading properties file " + url + " " + ioe.toString()); } finally { Closeables.closeQuietly(is); } return ImmutableMap.copyOf(configs); }
From source file:org.johansv.maven.plugins.buildproperties.reader.AbstractPropertiesReader.java
/** * {@inheritDoc}/* w w w . ja v a 2s. co m*/ */ public Properties read(T source) { InputStream in = null; try { in = getInputStream(source); return readProperties(in); } catch (IOException exc) { throw new PropertiesReaderException("Failed reading properties from source: " + source, exc); } finally { Closeables.closeQuietly(in); } }
From source file:net.sourceforge.vaticanfetcher.model.Daemon.java
public Daemon(@NotNull IndexRegistry indexRegistry) { Util.checkNotNull(indexRegistry); this.indexRegistry = indexRegistry; File indexParentDir = indexRegistry.getIndexParentDir(); indexesFile = new File(indexParentDir, ".indexes.txt"); /* Open a FileOutputStream for writing. This lets the daemon know that VaticanFetcher is currently running. */ String lockPath = Util.getAbsPath(indexesFile) + ".lock"; try {/* w w w . ja v a2 s .c om*/ final FileOutputStream daemonLock = new FileOutputStream(lockPath); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { Closeables.closeQuietly(daemonLock); } }); } catch (FileNotFoundException e) { /* This can occur if two instances are running, if someone is using the file or if VaticanFetcher is run from a CD-ROM. */ Util.printErr(e); } }
From source file:org.jooby.JoobyJs.java
@SuppressWarnings({ "rawtypes", "unchecked" }) void eval(final Reader reader) throws Exception { Consumer closer = x -> Closeables.closeQuietly(reader); Try.run(() -> engine.eval(reader)).onFailure(closer).onSuccess(closer); }