List of usage examples for com.google.common.io Closeables close
public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException
From source file:com.twitter.hbc.httpclient.Connection.java
public void close() { if (this.request != null && !this.request.isAborted()) { // aborting the request this.request.abort(); }/*from w w w . ja v a2s .co m*/ if (client instanceof RestartableHttpClient) { // restart the entire client ((RestartableHttpClient) client).restart(); } try { Closeables.close(this.stream, true); } catch (IOException e) { throw new RuntimeException(e); // should never happen } }
From source file:tv.icntv.sender.compress.AbstractCompress.java
@Override public void close(OutputStream out) { try {//from www .ja v a 2 s . c o m Closeables.close(out, true); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }
From source file:com.imminentmeals.android.base.utilities.database.CursorWalker.java
/** * <p>Walks the {@link android.database.Cursor} and performs {@link #step(android.database.Cursor)} on each row.</p> * @return number of records in the query */// ww w.ja va 2 s . c o m public int walk() { Cursor cursor = null; try { cursor = createCursor(); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToNext(); step(cursor); } return cursor.getCount(); } finally { //noinspection EmptyCatchBlock try { Closeables.close(cursor, true); } catch (IOException _) { } } }
From source file:org.grouplens.lenskit.util.io.LKFileUtils.java
/** * Open a file for input, optionally compressed. * * @param file The file to open./*from w w w . j av a 2 s.c om*/ * @param charset The character set to use. * @param compression Whether to compress the file. * @return A reader opened on the file. * @throws IOException if there is an error opening the file. */ public static Reader openInput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); InputStream istream = new FileInputStream(file); try { InputStream wrapped = effComp.wrapInput(istream); return new InputStreamReader(wrapped, charset); } catch (Exception ex) { Closeables.close(istream, true); Throwables.propagateIfPossible(ex, IOException.class); throw Throwables.propagate(ex); } }
From source file:sg.atom.utils._beta.functional.ConcatSequence.java
@Override public <OutType> Yielder<OutType> toYielder(final OutType initValue, final YieldingAccumulator<OutType, T> accumulator) { Yielder<Sequence<T>> yielderYielder = baseSequences.toYielder(null, new YieldingAccumulator<Sequence<T>, Sequence<T>>() { @Override/*from ww w . ja va 2s .c o m*/ public Sequence<T> accumulate(Sequence<T> accumulated, Sequence<T> in) { yield(); return in; } }); try { return makeYielder(yielderYielder, initValue, accumulator); } catch (RuntimeException e) { try { Closeables.close(yielderYielder, true); //Closeables.closeQuietly(closeable); } catch (IOException ex) { Logger.getLogger(ResourceClosingSequence.class.getName()).log(Level.SEVERE, null, ex); } throw e; } //return null; }
From source file:com.android.tools.idea.gradle.util.AndroidGradleSettings.java
@Nullable public static String getAndroidHomeFromLocalPropertiesFile(@NotNull File projectDir) { File filePath = new File(projectDir, SdkConstants.FN_LOCAL_PROPERTIES); if (!filePath.isFile()) { return null; }//from w w w. j a v a 2s. c o m Properties properties = new Properties(); FileInputStream fileInputStream = null; try { //noinspection IOResourceOpenedButNotSafelyClosed fileInputStream = new FileInputStream(filePath); properties.load(fileInputStream); } catch (FileNotFoundException e) { return null; } catch (IOException e) { String msg = String.format("Failed to read file '%1$s'", filePath.getPath()); LOG.error(msg, e); return null; } finally { try { Closeables.close(fileInputStream, true); } catch (IOException e) { LOG.debug(e); } } return properties.getProperty(SdkConstants.SDK_DIR_PROPERTY); }
From source file:org.kitesdk.cli.commands.ShowRecordsCommand.java
@Override public int run() throws IOException { Preconditions.checkArgument(datasets != null && !datasets.isEmpty(), "Missing dataset name"); Preconditions.checkArgument(datasets.size() == 1, "Only one dataset name can be given"); View<GenericData.Record> dataset = load(datasets.get(0), GenericData.Record.class); DatasetReader<GenericData.Record> reader = null; boolean threw = true; try {//from www.j av a2 s . c om reader = dataset.newReader(); int i = 0; for (GenericData.Record record : reader) { if (i >= numRecords) { break; } console.info(record.toString()); i += 1; } threw = false; } finally { Closeables.close(reader, threw); } return 0; }
From source file:org.openqa.selenium.firefox.internal.ClasspathExtension.java
public void writeTo(File extensionsDir) throws IOException { if (!FileHandler.isZipped(loadFrom)) { throw new WebDriverException("Will only install zipped extensions for now"); }/*from ww w. j a v a 2 s . c om*/ File holdingPen = new File(extensionsDir, "webdriver-staging"); FileHandler.createDir(holdingPen); File extractedXpi = new File(holdingPen, loadFrom); File parentDir = extractedXpi.getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } URL resourceUrl = Resources.getResource(loadResourcesUsing, loadFrom); OutputStream stream = null; try { stream = new FileOutputStream(extractedXpi); Resources.copy(resourceUrl, stream); } finally { Closeables.close(stream, false); } new FileExtension(extractedXpi).writeTo(extensionsDir); }
From source file:org.celeria.minecraft.backup.Archive.java
private void writeToEntry(final FileContent content) throws IOException { final InputStream input = inputStreamFrom(content); boolean threw = true; try {/*from w w w . j a v a2s. c o m*/ ByteStreams.copy(input, output); threw = false; } finally { Closeables.close(input, threw); } }
From source file:org.apache.mahout.cf.taste.hadoop.als.ALS.java
public static OpenIntObjectHashMap<Vector> readMatrixByRowsFromDistributedCache(int numEntities, Configuration conf) throws IOException { IntWritable rowIndex = new IntWritable(); VectorWritable row = new VectorWritable(); OpenIntObjectHashMap<Vector> featureMatrix = numEntities > 0 ? new OpenIntObjectHashMap<Vector>(numEntities) : new OpenIntObjectHashMap<Vector>(); Path[] cachedFiles = HadoopUtil.getCachedFiles(conf); LocalFileSystem localFs = FileSystem.getLocal(conf); for (Path cachedFile : cachedFiles) { SequenceFile.Reader reader = null; try {//w w w . j a va2 s. c om reader = new SequenceFile.Reader(localFs, cachedFile, conf); while (reader.next(rowIndex, row)) { featureMatrix.put(rowIndex.get(), row.get()); } } finally { Closeables.close(reader, true); } } Preconditions.checkState(!featureMatrix.isEmpty(), "Feature matrix is empty"); return featureMatrix; }