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.b2international.snowowl.snomed.reasoner.net4j.SnomedOntologyExportRequest.java
@Override protected Void confirming(final ExtendedDataInputStream in, final OMMonitor monitor) throws Exception { OutputStream outputStream = null; try {/* ww w . j av a 2 s . c o m*/ final long length = in.readLong(); outputStream = IOUtil.openOutputStream(exportPath); outputStream = new BufferedOutputStream(outputStream); IOUtil.copyBinary(in, outputStream, length); } finally { Closeables.close(outputStream, true); monitor.done(); } return null; }
From source file:org.apache.mahout.classifier.NewsgroupHelper.java
public static void countWords(Analyzer analyzer, Collection<String> words, Reader in, Multiset<String> overallCounts) throws IOException { TokenStream ts = analyzer.tokenStream("text", in); ts.addAttribute(CharTermAttribute.class); ts.reset();// w w w . ja v a 2 s .com while (ts.incrementToken()) { String s = ts.getAttribute(CharTermAttribute.class).toString(); words.add(s); } overallCounts.addAll(words); ts.end(); Closeables.close(ts, true); }
From source file:org.apache.mahout.utils.vectors.RowIdJob.java
@Override public int run(String[] args) throws Exception { addInputOption();//from w ww . j av a 2 s . c o m addOutputOption(); Map<String, List<String>> parsedArgs = parseArguments(args); if (parsedArgs == null) { return -1; } Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); Path outputPath = getOutputPath(); Path indexPath = new Path(outputPath, "docIndex"); Path matrixPath = new Path(outputPath, "matrix"); SequenceFile.Writer indexWriter = SequenceFile.createWriter(fs, conf, indexPath, IntWritable.class, Text.class); SequenceFile.Writer matrixWriter = SequenceFile.createWriter(fs, conf, matrixPath, IntWritable.class, VectorWritable.class); try { IntWritable docId = new IntWritable(); int i = 0; int numCols = 0; for (Pair<Text, VectorWritable> record : new SequenceFileDirIterable<Text, VectorWritable>( getInputPath(), PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) { VectorWritable value = record.getSecond(); docId.set(i); indexWriter.append(docId, record.getFirst()); matrixWriter.append(docId, value); i++; numCols = value.get().size(); } log.info("Wrote out matrix with {} rows and {} columns to {}", i, numCols, matrixPath); return 0; } finally { Closeables.close(indexWriter, false); Closeables.close(matrixWriter, false); } }
From source file:org.jclouds.examples.rackspace.cloudloadbalancers.UpdateLoadBalancers.java
/** * Always close your service when you're done with it. * * Note that closing quietly like this is not necessary in Java 7. * You would use try-with-resources in the main method instead. *//* w w w. j a v a 2 s . c o m*/ public void close() throws IOException { Closeables.close(clbApi, true); }
From source file:eu.redzoo.article.planetcassandra.reactive.EmbeddedCassandra.java
@SuppressWarnings("unchecked") protected static int prepare() throws IOException { String cassandraDirName = "target" + File.separator + "cassandra-junit-" + new Random().nextInt(1000000); File cassandraDir = new File(cassandraDirName); if (!cassandraDir.exists()) { boolean isCreated = cassandraDir.mkdirs(); if (!isCreated) { throw new RuntimeException("could not create cassandra dir " + cassandraDir.getAbsolutePath()); }/*from ww w. j av a2 s . c o m*/ } InputStream cassandraConfigurationInput = null; Writer cassandraConfigurationOutput = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); cassandraConfigurationInput = loader.getResourceAsStream(CASSANDRA_YAML_FILE); Yaml yaml = new Yaml(); Map<String, Object> cassandraConfiguration = (Map<String, Object>) yaml .load(cassandraConfigurationInput); int rpcPort = findUnusedLocalPort(); if (rpcPort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("rpc_port", rpcPort); int storagePort = findUnusedLocalPort(); if (storagePort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("storage_port", storagePort); int nativeTransportPort = findUnusedLocalPort(); if (nativeTransportPort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("native_transport_port", nativeTransportPort); cassandraConfiguration.put("start_native_transport", "true"); cassandraConfigurationOutput = new OutputStreamWriter( new FileOutputStream(cassandraDirName + File.separator + CASSANDRA_YAML_FILE), Charsets.UTF_8); yaml.dump(cassandraConfiguration, cassandraConfigurationOutput); System.setProperty("cassandra.config", new File(cassandraDirName, CASSANDRA_YAML_FILE).toURI().toString()); System.setProperty("cassandra-foreground", "true"); DatabaseDescriptor.createAllDirectories(); return nativeTransportPort; } finally { Closeables.closeQuietly(cassandraConfigurationInput); Closeables.close(cassandraConfigurationOutput, true); } }
From source file:com.uber.stream.kafka.mirrormaker.controller.reporter.HelixKafkaMirrorMakerMetricsReporter.java
HelixKafkaMirrorMakerMetricsReporter(ControllerConf config) { final String environment = config.getEnvironment(); final String clientId = config.getInstanceId(); String[] dcNenv = parse(environment); if (dcNenv == null) { LOGGER.error("Error parsing environment info"); _registry = null;/*w w w. j a va 2 s. co m*/ _graphiteReporter = null; _jmxReporter = null; _reporterMetricPrefix = null; return; } _reporterMetricPrefix = String.format(KAFKA_MIRROR_MAKER_METRICS_REPORTER_PREFIX_FORMAT, dcNenv[0], dcNenv[1], clientId); LOGGER.info("Reporter Metric Prefix is : " + _reporterMetricPrefix); _registry = new MetricRegistry(); final Boolean enabledGraphiteReporting = true; final Boolean enabledJmxReporting = true; final long graphiteReportFreqSec = 10L; // Init jmx reporter if (enabledJmxReporting) { _jmxReporter = JmxReporter.forRegistry(this._registry).build(); _jmxReporter.start(); } else { _jmxReporter = null; } // Init graphite reporter if (enabledGraphiteReporting) { Graphite graphite = getGraphite(config); if (graphite == null) { _graphiteReporter = null; } else { _graphiteReporter = GraphiteReporter.forRegistry(_registry).prefixedWith(_reporterMetricPrefix) .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL).build(graphite); _graphiteReporter.start(graphiteReportFreqSec, TimeUnit.SECONDS); } } else { _graphiteReporter = null; } Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { if (enabledJmxReporting) Closeables.close(_jmxReporter, true); if (enabledGraphiteReporting) Closeables.close(_graphiteReporter, true); } catch (Exception e) { LOGGER.error("Error while closing Jmx and Graphite reporters.", e); } } }); }
From source file:net.sf.lucis.core.impl.AbstractStore.java
private void shutdown() { try {// ww w . j a v a2s . co m Closeables.close(reader, true); reader = null; lastManaged = false; watch.reset(); } catch (IOException e) { // TODO: Log } }
From source file:org.gpfvic.mahout.common.MahoutTestCase.java
protected static void writeLines(File file, String... lines) throws IOException { Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8); try {//from w w w . j a va 2s. c o m for (String line : lines) { writer.write(line); writer.write('\n'); } } finally { Closeables.close(writer, false); } }
From source file:org.grouplens.lenskit.util.io.LKFileUtils.java
/** * Open a file for input, optionally compressed. * * @param file The file to open.//w ww. j ava 2 s . co m * @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 Writer openOutput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); OutputStream ostream = new FileOutputStream(file); try { OutputStream wrapped = effComp.wrapOutput(ostream); return new OutputStreamWriter(wrapped, charset); } catch (Exception ex) { Closeables.close(ostream, true); Throwables.propagateIfPossible(ex, IOException.class); throw Throwables.propagate(ex); } }
From source file:org.openqa.selenium.io.Zip.java
public void unzip(String source, File outputDir) throws IOException { byte[] bytes = new Base64Encoder().decode(source); ByteArrayInputStream bis = null; try {/* w w w. j av a 2 s. co m*/ bis = new ByteArrayInputStream(bytes); unzip(bis, outputDir); } finally { Closeables.close(bis, false); } }