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:net.derquinse.common.io.DurableFiles.java
/** * Copies to a file all characters from a {@link CharSource} and {@link Closeable} object supplied * by a factory, using the given character set. The file is sync'd before being closed. * @param from source data/*from w w w. ja v a 2 s .c om*/ * @param to the destination file * @param charset the character set used when writing the file * @throws IOException if an I/O error occurs */ public static <R extends Readable & Closeable> void copy(CharSource from, File to, Charset charset) throws IOException { checkNotNull(from); checkNotNull(to); checkNotNull(charset); boolean threw = true; FileOutputStream os = new FileOutputStream(to); try { OutputStreamWriter w = new OutputStreamWriter(os, charset); try { from.copyTo(w); w.flush(); os.flush(); os.getFD().sync(); threw = false; } finally { Closeables.close(w, threw); } } finally { Closeables.close(os, threw); } }
From source file:net.opentsdb.contrib.tsquare.web.view.AbstractJsonResponseWriter.java
@Override public void onError(final ResponseContext context, final Throwable ex) { JsonGenerator json = getJsonGenerator(context); try {/*from w w w .jav a 2 s. com*/ Closeables.close(json, true); } catch (IOException e) { // Is swallowed above. } }
From source file:org.apache.mahout.cf.taste.example.bookcrossing.BookCrossingDataModel.java
private static File convertBCFile(File originalFile, boolean ignoreRatings) throws IOException { if (!originalFile.exists()) { throw new FileNotFoundException(originalFile.toString()); }/* www . ja va 2 s . c o m*/ File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "taste.bookcrossing.txt"); resultFile.delete(); Writer writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8); for (String line : new FileLineIterable(originalFile, true)) { // 0 ratings are basically "no rating", ignore them (thanks h.9000) if (line.endsWith("\"0\"")) { continue; } // Delete replace anything that isn't numeric, or a semicolon delimiter. Make comma the delimiter. String convertedLine = NON_DIGIT_SEMICOLON_PATTERN.matcher(line).replaceAll("").replace(';', ','); // If this means we deleted an entire ID -- few cases like that -- skip the line if (convertedLine.contains(",,")) { continue; } if (ignoreRatings) { // drop rating convertedLine = convertedLine.substring(0, convertedLine.lastIndexOf(',')); } writer.write(convertedLine); writer.write('\n'); } writer.flush(); } catch (IOException ioe) { resultFile.delete(); throw ioe; } finally { Closeables.close(writer, false); } return resultFile; }
From source file:com.stitchgalaxy.cdn.Publisher.java
/** * Always close your service when you're done with it. */ public void close() throws IOException { Closeables.close(cloudFilesClient, true); }
From source file:mlbench.bayes.BayesUtils.java
static Pair<Long[], List<Path>> createDictionaryChunks(Path featureCountPath, Path dictionaryPathBase, Configuration baseConf, int chunkSizeInMegabytes) throws IOException { List<Path> chunkPaths = Lists.newArrayList(); Configuration conf = new Configuration(baseConf); FileSystem fs = FileSystem.get(featureCountPath.toUri(), conf); long chunkSizeLimit = chunkSizeInMegabytes * 1024L * 1024L; int chunkIndex = 0; Path chunkPath = new Path(dictionaryPathBase, FREQUENCY_FILE + chunkIndex); chunkPaths.add(chunkPath);// www . ja v a2 s . c o m SequenceFile.Writer freqWriter = new SequenceFile.Writer(fs, conf, chunkPath, IntWritable.class, LongWritable.class); try { long currentChunkSize = 0; long featureCount = 0; long vectorCount = Long.MAX_VALUE; Path filesPattern = new Path(featureCountPath, OUTPUT_FILES_PATTERN); for (Pair<IntWritable, LongWritable> record : new SequenceFileDirIterable<IntWritable, LongWritable>( filesPattern, PathType.GLOB, null, null, true, conf)) { if (currentChunkSize > chunkSizeLimit) { Closeables.close(freqWriter, false); chunkIndex++; chunkPath = new Path(dictionaryPathBase, FREQUENCY_FILE + chunkIndex); chunkPaths.add(chunkPath); freqWriter = new SequenceFile.Writer(fs, conf, chunkPath, IntWritable.class, LongWritable.class); currentChunkSize = 0; } int fieldSize = SEQUENCEFILE_BYTE_OVERHEAD + Integer.SIZE / 8 + Long.SIZE / 8; currentChunkSize += fieldSize; IntWritable key = record.getFirst(); LongWritable value = record.getSecond(); if (key.get() >= 0) { freqWriter.append(key, value); } else if (key.get() == -1) { vectorCount = value.get(); } featureCount = Math.max(key.get(), featureCount); } featureCount++; Long[] counts = { featureCount, vectorCount }; return new Pair<Long[], List<Path>>(counts, chunkPaths); } finally { Closeables.close(freqWriter, false); } }
From source file:com.rackspacecloud.blueflood.outputs.cloudfiles.CloudFilesPublisher.java
public void close() throws IOException { Closeables.close(blobStore.getContext(), true); }
From source file:io.ssc.trackthetrackers.extraction.hadoop.util.DomainIndex.java
private Long2IntMap hashIndexFile(FileSystem fs, Path indexFile) throws IOException { // hardcoded to handle 2012 payleveldomain index Long2IntMap hashesToIndices = new Long2IntOpenHashMap(42889800); Pattern SEP = Pattern.compile("\t"); int linesRead = 0; BufferedReader reader = null; try {/*from www. j a v a 2 s . co m*/ reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(fs.open(indexFile)), "UTF8")); String line = null; while ((line = reader.readLine()) != null) { String[] tokens = SEP.split(line); long hash = hash(tokens[0]); int index = Integer.parseInt(tokens[1]); int previous = hashesToIndices.put(hash, index); if (previous != 0) { throw new IllegalStateException("Hash collision encountered!"); } if (++linesRead % 100000 == 0) { System.out.println(linesRead + " lines read..."); } } } finally { Closeables.close(reader, false); } System.out.println(linesRead + " lines read. done."); return hashesToIndices; }
From source file:hudson.plugins.timestamper.annotator.ConsoleLogParserImpl.java
/** * {@inheritDoc}/*from w w w . j av a 2 s . c o m*/ */ @Override public ConsoleLogParser.Result seek(Run<?, ?> build) throws IOException { ConsoleLogParser.Result result = new ConsoleLogParser.Result(); result.atNewLine = true; InputStream inputStream = new BufferedInputStream(build.getLogInputStream()); boolean threw = true; try { long posFromStart = pos; if (pos < 0) { posFromStart = build.getLogText().length() + pos; } for (long i = 0; i < posFromStart; i++) { int value = inputStream.read(); if (value == -1) { result.endOfFile = true; break; } result.atNewLine = value == 0x0A; if (result.atNewLine) { result.lineNumber++; } } threw = false; } finally { Closeables.close(inputStream, threw); } return result; }
From source file:org.apache.mahout.cf.taste.impl.recommender.svd.FilePersistenceStrategy.java
@Override public void maybePersist(Factorization factorization) throws IOException { DataOutputStream out = null;// w w w . java2 s. co m try { log.info("Writing factorization to {}...", file.getAbsolutePath()); out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); writeBinary(factorization, out); } finally { Closeables.close(out, false); } }
From source file:org.apache.mahout.common.iterator.sequencefile.SequenceFileIterator.java
@Override public void close() throws IOException { key = null; value = null; Closeables.close(reader, true); endOfData(); }