List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:com.netflix.exhibitor.core.index.IndexMetaData.java
public static void write(IndexMetaData meta, File to) throws Exception { DateFormat format = DateFormat.getDateTimeInstance(); Properties properties = new Properties(); properties.setProperty(PROPERTY_FROM, format.format(meta.from)); properties.setProperty(PROPERTY_TO, format.format(meta.to)); properties.setProperty(PROPERTY_VERSION, Integer.toString(VERSION)); properties.setProperty(PROPERTY_COUNT, Integer.toString(meta.entryCount)); OutputStream out = new BufferedOutputStream(new FileOutputStream(to)); try {//from w ww .j av a 2s . com properties.store(out, "Auto-generated by Exhibitor"); } finally { Closeables.closeQuietly(out); } }
From source file:com.netflix.exhibitor.core.state.ManifestVersion.java
public ManifestVersion() { String localVersion = null;//from w w w .j a va 2 s.co m InputStream stream = null; try { for (URL manifestUrl : Collections .list(getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"))) { if (stream != null) { Closeables.closeQuietly(stream); } stream = manifestUrl.openStream(); Manifest manifest = new Manifest(stream); String title = manifest.getMainAttributes().getValue(IMPLEMENTATION_TITLE); String mainClass = manifest.getMainAttributes().getValue(MAIN_CLASS); if ("com.netflix.exhibitor.application.ExhibitorMain".equals(mainClass) || "exhibitor-core".equals(title)) { localVersion = manifest.getMainAttributes().getValue(IMPLEMENTATION_VERSION); if (localVersion != null) { localVersion = "v" + localVersion; if (localVersion.endsWith(SNAPSHOT)) { localVersion = localVersion.substring(0, localVersion.length() - SNAPSHOT.length()); } } break; } } } catch (Exception ignore) { // ignore } finally { Closeables.closeQuietly(stream); } version = (localVersion != null) ? localVersion : "dev"; }
From source file:com.netflix.exhibitor.core.index.IndexActivity.java
@Override public void completed(boolean wasSuccessful) { Closeables.closeQuietly(indexer); if (completionListener != null) { completionListener.completed();//from w w w. j a v a 2 s . c om } }
From source file:org.stjs.generator.executor.RhinoExecutor.java
private Object addScript(ScriptEngine engine, File scriptFile) throws ScriptException { Reader input = null;/* w ww .jav a2 s .c om*/ try { // XXX: here i may need to get the charset from configuration input = Files.newReader(scriptFile, Charsets.UTF_8); return engine.eval(input); } catch (FileNotFoundException e) { throw new ScriptException(e); } finally { Closeables.closeQuietly(input); } }
From source file:talkeeg.android.ImageUtils.java
private static Bitmap loadImage(File imageFile, BitmapFactory.Options opts) { FileInputStream fis = null;//from w ww . j a v a2 s . c o m try { fis = new FileInputStream(imageFile); return BitmapFactory.decodeStream(fis, null, opts); } catch (IOException e) { Log.e(TAG, "on file: " + imageFile, e); } finally { Closeables.closeQuietly(fis); } return null; }
From source file:com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader.java
/** Returns a stream of each line in the trace file. */ protected Stream<String> lines() throws IOException { InputStream input = readFiles(); Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8); return new BufferedReader(reader).lines().map(String::trim).onClose(() -> Closeables.closeQuietly(input)); }
From source file:com.pinterest.deployservice.common.TarUtils.java
static void addInputStreamToTar(TarArchiveOutputStream taos, InputStream is, String path, long size, int mode) throws Exception { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(size);//from w w w.ja va 2 s .c o m entry.setMode(mode); try { taos.putArchiveEntry(entry); IOUtils.copy(is, taos); } finally { taos.closeArchiveEntry(); Closeables.closeQuietly(is); } }
From source file:org.jetbrains.jet.objc.ObjCTestUtil.java
@NotNull @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") public static String runProcess(@NotNull String command) { try {/*ww w . j a va2s . c om*/ Process process = Runtime.getRuntime().exec(command); process.waitFor(); InputStreamReader output = new InputStreamReader(process.getInputStream()); String result = CharStreams.toString(output); Closeables.closeQuietly(output); InputStreamReader errorStream = new InputStreamReader(process.getErrorStream()); String error = CharStreams.toString(errorStream); Closeables.closeQuietly(errorStream); System.err.print(error); int exitCode = process.exitValue(); assert exitCode == 0 : "Process exited with code " + exitCode + ", result: " + result; return result; } catch (Exception e) { throw ExceptionUtils.rethrow(e); } }
From source file:org.kitesdk.apps.spi.jobs.StandardEventsJob.java
public void run(@DataIn(name = "source.events", type = StandardEvent.class) View<StandardEvent> input, @DataOut(name = "target.events", type = StandardEvent.class) View<StandardEvent> output) { DatasetReader<StandardEvent> reader = input.newReader(); DatasetWriter<StandardEvent> writer = output.newWriter(); try {// ww w . j av a2 s .c o m while (reader.hasNext()) { writer.write(reader.next()); } } finally { Closeables.closeQuietly(reader); Closeables.closeQuietly(writer); } }
From source file:org.renyan.leveldb.impl.DbLock.java
public DbLock(File lockFile) throws IOException { Preconditions.checkNotNull(lockFile, "lockFile is null"); this.lockFile = lockFile; // open and lock the file channel = new RandomAccessFile(lockFile, "rw").getChannel(); try {//from w w w . j a v a2 s. co m lock = channel.tryLock(); } catch (IOException e) { Closeables.closeQuietly(channel); throw e; } if (lock == null) { throw new IOException(format("Unable to acquire lock on '%s'", lockFile.getAbsolutePath())); } }