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:org.apache.mahout.clustering.streaming.tools.ClusterQualitySummarizer.java
public int run(String[] args) throws IOException { if (!parseArgs(args)) { return -1; }// w ww . jav a2s .c o m Configuration conf = new Configuration(); try { // Configuration.dumpConfiguration(conf, new OutputStreamWriter(System.out)); fileOut = new PrintWriter(new FileOutputStream(outputFile)); fileOut.printf("cluster,distance.mean,distance.sd,distance.q0,distance.q1,distance.q2,distance.q3," + "distance.q4,count,is.train\n"); // Reading in the centroids (both pairs, if they exist). List<Centroid> centroids; List<Centroid> centroidsCompare = null; if (mahoutKMeansFormat) { SequenceFileDirValueIterable<ClusterWritable> clusterIterable = new SequenceFileDirValueIterable<ClusterWritable>( new Path(centroidFile), PathType.GLOB, conf); centroids = Lists.newArrayList(IOUtils.getCentroidsFromClusterWritableIterable(clusterIterable)); } else { SequenceFileDirValueIterable<CentroidWritable> centroidIterable = new SequenceFileDirValueIterable<CentroidWritable>( new Path(centroidFile), PathType.GLOB, conf); centroids = Lists.newArrayList(IOUtils.getCentroidsFromCentroidWritableIterable(centroidIterable)); } if (centroidCompareFile != null) { if (mahoutKMeansFormatCompare) { SequenceFileDirValueIterable<ClusterWritable> clusterCompareIterable = new SequenceFileDirValueIterable<ClusterWritable>( new Path(centroidCompareFile), PathType.GLOB, conf); centroidsCompare = Lists .newArrayList(IOUtils.getCentroidsFromClusterWritableIterable(clusterCompareIterable)); } else { SequenceFileDirValueIterable<CentroidWritable> centroidCompareIterable = new SequenceFileDirValueIterable<CentroidWritable>( new Path(centroidCompareFile), PathType.GLOB, conf); centroidsCompare = Lists.newArrayList( IOUtils.getCentroidsFromCentroidWritableIterable(centroidCompareIterable)); } } // Reading in the "training" set. SequenceFileDirValueIterable<VectorWritable> trainIterable = new SequenceFileDirValueIterable<VectorWritable>( new Path(trainFile), PathType.GLOB, conf); Iterable<Vector> trainDatapoints = IOUtils.getVectorsFromVectorWritableIterable(trainIterable); Iterable<Vector> datapoints = trainDatapoints; printSummaries(ClusteringUtils.summarizeClusterDistances(trainDatapoints, centroids, new SquaredEuclideanDistanceMeasure()), "train"); // Also adding in the "test" set. if (testFile != null) { SequenceFileDirValueIterable<VectorWritable> testIterable = new SequenceFileDirValueIterable<VectorWritable>( new Path(testFile), PathType.GLOB, conf); Iterable<Vector> testDatapoints = IOUtils.getVectorsFromVectorWritableIterable(testIterable); printSummaries(ClusteringUtils.summarizeClusterDistances(testDatapoints, centroids, new SquaredEuclideanDistanceMeasure()), "test"); datapoints = Iterables.concat(trainDatapoints, testDatapoints); } // At this point, all train/test CSVs have been written. We now compute quality metrics. List<OnlineSummarizer> summaries = ClusteringUtils.summarizeClusterDistances(datapoints, centroids, distanceMeasure); List<OnlineSummarizer> compareSummaries = null; if (centroidsCompare != null) { compareSummaries = ClusteringUtils.summarizeClusterDistances(datapoints, centroidsCompare, distanceMeasure); } System.out.printf("[Dunn Index] First: %f", ClusteringUtils.dunnIndex(centroids, distanceMeasure, summaries)); if (compareSummaries != null) { System.out.printf(" Second: %f\n", ClusteringUtils.dunnIndex(centroidsCompare, distanceMeasure, compareSummaries)); } else { System.out.printf("\n"); } System.out.printf("[Davies-Bouldin Index] First: %f", ClusteringUtils.daviesBouldinIndex(centroids, distanceMeasure, summaries)); if (compareSummaries != null) { System.out.printf(" Second: %f\n", ClusteringUtils.daviesBouldinIndex(centroidsCompare, distanceMeasure, compareSummaries)); } else { System.out.printf("\n"); } } catch (IOException e) { System.out.println(e.getMessage()); } finally { Closeables.close(fileOut, false); } return 0; }
From source file:com.eucalyptus.blockstorage.san.common.ShellSessionManager.java
/** * * Caller must be synchronized//from ww w .j a v a 2 s. c o m */ private TaskRunner getTaskRunner(final String description, @Nullable final Map<String, Long> timings) { return new TaskRunner() { private boolean connected = false; private int taskNumber = 1; private Map<String, Long> timingMap = timings == null ? Maps.<String, Long>newLinkedHashMap() : timings; private void init() { try { connect(); connected = true; } catch (EucalyptusCloudException e) { LOG.error(e); } } @Override public String runTask(final AbstractSANTask task) throws InterruptedException { String returnValue = ""; if (!connected) { timingMap.put("begin", System.currentTimeMillis()); init(); timingMap.put("connect", System.currentTimeMillis()); } if (!connected) return returnValue; timingMap.put("pre-task-" + taskNumber, System.currentTimeMillis()); try { writer.write("" + task.getCommand() + task.getEOFCommand()); writer.flush(); for (String line = null; (line = reader.readLine()) != null;) { line = line + "\r"; if (line.contains("" + task.getEOFCommand())) break; returnValue += line; } } catch (IOException e) { LOG.error(e, e); } finally { timingMap.put("task-" + taskNumber, System.currentTimeMillis()); taskNumber++; } return returnValue; } @Override public void close() { timingMap.put("pre-close", System.currentTimeMillis()); try { if (writer != null) try { writer.write("logout\r"); writer.flush(); } catch (Exception e) { LOG.warn("Error logging out of session", e); } if (reader != null) { Closeables.close(reader, true); reader = null; } if (writer != null) { Closeables.close(writer, true); writer = null; } // Tear it down. Do not persist session. // Doing so causes more issues than it is worth. // EQL serializes anyway and the overhead is // minor. if (channel != null) { channel.getSession().disconnect(); channel.disconnect(); channel = null; } } catch (JSchException | IOException e) { LOG.error(e, e); } finally { timingMap.put("close", System.currentTimeMillis()); if (logTiming) dumpTiming(timingMap, description); } } }; }
From source file:com.imminentmeals.android.base.utilities.database.DefaultContentProviderActions.java
@SuppressWarnings("unchecked") @Override// w ww . ja v a 2 s.c om public <T extends ActiveRecord> List<T> selectRecords(BaseContentProvider content, Uri uri, QueryBuilder query, @Nullable String sort_order) { assert _active_record_factory != null; final SQLiteDatabase database = content.getOpenHelper().getReadableDatabase(); if (database == null) return new ArrayList<>(); Cursor cursor = null; final ArrayList<T> items = newArrayList(); try { cursor = database.query(_table, _active_record_factory.projection(), query.toString(), query.argumentsAsArray(), null, null, sort_order); while (cursor.moveToNext()) items.add((T) _active_record_factory.create(cursor)); } finally { try { Closeables.close(cursor, true); } catch (IOException ignored) { } } return items; }
From source file:com.nesscomputing.db.postgres.embedded.EmbeddedPostgreSQL.java
private Thread newCloserThread() { final Thread closeThread = new Thread(new Runnable() { @Override/*from www . j a va 2 s . com*/ public void run() { try { Closeables.close(EmbeddedPostgreSQL.this, true); } catch (IOException ex) { LOG.error("Unexpected IOException from Closeables.close", ex); } } }); closeThread.setName("postgres-" + instanceId + "-closer"); return closeThread; }
From source file:BlockStorage.java
@Override public void close() throws IOException { Closeables.close(novaApi, true); Closeables.close(cinderApi, true); }
From source file:com.android.assetstudiolib.GraphicGenerator.java
/** * Returns the icon (32x32) for a given clip art image. * * @param name the name of the image to be loaded (which can be looked up via * {@link #getClipartNames()}) * @return the icon image//from w w w. j ava 2s . c o m * @throws IOException if the image cannot be loaded */ public static BufferedImage getClipartIcon(String name) throws IOException { InputStream is = GraphicGenerator.class.getResourceAsStream("/images/clipart/small/" + name); try { return ImageIO.read(is); } finally { Closeables.close(is, true /* swallowIOException */); } }
From source file:org.apache.jackrabbit.oak.plugins.blob.MarkSweepGarbageCollector.java
/** * Returns the stats related to GC for all repos * // w w w . j a v a 2 s . c om * @return a list of GarbageCollectionRepoStats objects * @throws Exception */ @Override public List<GarbageCollectionRepoStats> getStats() throws Exception { List<GarbageCollectionRepoStats> stats = newArrayList(); if (SharedDataStoreUtils.isShared(blobStore)) { // Get all the references available List<DataRecord> refFiles = ((SharedDataStore) blobStore) .getAllMetadataRecords(SharedStoreRecordType.REFERENCES.getType()); Map<String, DataRecord> references = Maps.uniqueIndex(refFiles, new Function<DataRecord, String>() { @Override public String apply(DataRecord input) { return SharedStoreRecordType.REFERENCES.getIdFromName(input.getIdentifier().toString()); } }); // Get all the markers available List<DataRecord> markerFiles = ((SharedDataStore) blobStore) .getAllMetadataRecords(SharedStoreRecordType.MARKED_START_MARKER.getType()); Map<String, DataRecord> markers = Maps.uniqueIndex(markerFiles, new Function<DataRecord, String>() { @Override public String apply(DataRecord input) { return SharedStoreRecordType.MARKED_START_MARKER .getIdFromName(input.getIdentifier().toString()); } }); // Get all the repositories registered List<DataRecord> repoFiles = ((SharedDataStore) blobStore) .getAllMetadataRecords(SharedStoreRecordType.REPOSITORY.getType()); for (DataRecord repoRec : repoFiles) { String id = SharedStoreRecordType.REFERENCES.getIdFromName(repoRec.getIdentifier().toString()); GarbageCollectionRepoStats stat = new GarbageCollectionRepoStats(); stat.setRepositoryId(id); if (id != null && id.equals(repoId)) { stat.setLocal(true); } if (references.containsKey(id)) { DataRecord refRec = references.get(id); stat.setEndTime(refRec.getLastModified()); stat.setLength(refRec.getLength()); if (markers.containsKey(id)) { stat.setStartTime(markers.get(id).getLastModified()); } LineNumberReader reader = null; try { reader = new LineNumberReader(new InputStreamReader(refRec.getStream())); while (reader.readLine() != null) { } stat.setNumLines(reader.getLineNumber()); } finally { Closeables.close(reader, true); } } stats.add(stat); } } return stats; }
From source file:com.orange.clara.cloud.servicedbdumper.controllers.ManagerController.java
@RequestMapping(value = Routes.DOWNLOAD_DUMP_FILE_ROOT + "/{dumpFileId:[0-9]+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public @ResponseBody void download(@PathVariable Integer dumpFileId, HttpServletRequest request, HttpServletResponse resp, @RequestParam(value = "original", required = false) String original) throws IOException, DumpFileDeletedException { DatabaseDumpFile databaseDumpFile = getDatabaseDumpFile(dumpFileId); this.checkDbDumperServiceInstance(databaseDumpFile.getDbDumperServiceInstance()); this.checkDatabaseDumpFile(databaseDumpFile); String userRequest = ""; String passwordRequest = ""; String authorization = request.getHeader("Authorization"); if (authorization != null && authorization.startsWith("Basic")) { String base64Credentials = authorization.substring("Basic".length()).trim(); String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8")); String[] values = credentials.split(":", 2); userRequest = values[0];/*www . j a va 2 s .c o m*/ passwordRequest = values[1]; } else { this.getErrorResponseEntityBasicAuth(resp); return; } if (!userRequest.equals(databaseDumpFile.getUser()) || !passwordRequest.equals(databaseDumpFile.getPassword())) { this.getErrorResponseEntityBasicAuth(resp); return; } String fileName = DumpFileHelper.getFilePath(databaseDumpFile); resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); resp.setContentLengthLong(this.filer.getContentLength(fileName)); InputStream inputStream = null; if (original == null || original.isEmpty()) { inputStream = filer.retrieveWithOriginalStream(fileName); } else { inputStream = filer.retrieveWithStream(fileName); File file = new File(fileName); String[] filenames = file.getName().split("\\."); if (filenames.length >= 2) { fileName = filenames[0] + "." + filenames[1]; } } inputStream = new BufferedInputStream(inputStream); File file = new File(fileName); resp.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); OutputStream outputStream = null; outputStream = resp.getOutputStream(); try { ByteStreams.copy(inputStream, outputStream); } finally { Closeables.closeQuietly(inputStream); outputStream.flush(); resp.flushBuffer(); Closeables.close(outputStream, true); } }
From source file:org.calrissian.mango.collect.CloseableIterators.java
/** * Autoclose the iterator when exhausted or if an exception is thrown. It is currently set to protected, so that only * classes in this package can use.//w ww .j av a2 s. c om * * Note that when using this method the order of calls matters. {@code limit()} is an example of one method which can * prevent the completion of an iterator. For instance limit(autoClose(iterator), 1) will not close the * resource if there is more than 1 element, but autoClose(limit(iterator, 1)) will close the underlying * resource. */ public static <T> CloseableIterator<T> autoClose(final CloseableIterator<? extends T> iterator) { checkNotNull(iterator); return new CloseableIterator<T>() { private boolean closed = false; @Override public void closeQuietly() { try { Closeables.close(this, true); } catch (IOException e) { // IOException should not have been thrown } } @Override public void close() throws IOException { if (closed) return; closed = true; iterator.close(); } @Override public boolean hasNext() { try { if (closed) return false; if (!iterator.hasNext()) { closeQuietly(); return false; } return true; } catch (RuntimeException re) { closeQuietly(); throw re; } } @Override public T next() { if (hasNext()) { try { return iterator.next(); } catch (RuntimeException re) { closeQuietly(); throw re; } } throw new NoSuchElementException(); } @Override public void remove() { try { if (hasNext()) { iterator.remove(); } } catch (RuntimeException re) { closeQuietly(); throw re; } } }; }
From source file:com.google.dart.compiler.backend.js.ClosureJsBackend.java
private void packageAppOptimized(LibrarySource app, Collection<LibraryUnit> libraries, DartCompilerContext context, CoreTypeProvider typeProvider) throws IOException { List<CompilerInput> inputs = Lists.newLinkedList(); Map<String, Source> sourcesByName = Maps.newHashMap(); DepsWritingCallback callback = new DepsWritingCallback(context, typeProvider, inputs, sourcesByName); DependencyBuilder.build(context.getAppLibraryUnit(), callback); // Lastly, add the entry point. inputs.add(getCompilerInputForEntry(context)); // Currently, there is only a single module, add all the sources to it. JSModule mainModule = new JSModule("main"); for (CompilerInput input : inputs) { if (input != null) { mainModule.add(input);/* w w w . ja v a 2 s . co m*/ } } Writer out = context.getArtifactWriter(app, "", getAppExtension()); boolean failed = true; try { compileModule(app, context, mainModule, sourcesByName, out); failed = false; } finally { Closeables.close(out, failed); } }