Example usage for com.google.common.io Closeables close

List of usage examples for com.google.common.io Closeables close

Introduction

In this page you can find the example usage for com.google.common.io Closeables close.

Prototype

public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException 

Source Link

Document

Closes a Closeable , with control over whether an IOException may be thrown.

Usage

From source file:org.apache.mahout.classifier.NewsgroupHelper.java

public Vector encodeFeatureVector(File file, int actual, int leakType, Multiset<String> overallCounts)
        throws IOException {
    long date = (long) (1000 * (DATE_REFERENCE + actual * MONTH + 1 * WEEK * rand.nextDouble()));
    Multiset<String> words = ConcurrentHashMultiset.create();

    BufferedReader reader = Files.newReader(file, Charsets.UTF_8);
    try {//from  w  w w  . j  a v a  2  s .co  m
        String line = reader.readLine();
        Reader dateString = new StringReader(DATE_FORMATS[leakType % 3].format(new Date(date)));
        countWords(analyzer, words, dateString, overallCounts);
        while (line != null && !line.isEmpty()) {
            boolean countHeader = (line.startsWith("From:") || line.startsWith("Subject:")
                    || line.startsWith("Keywords:") || line.startsWith("Summary:")) && leakType < 6;
            do {
                Reader in = new StringReader(line);
                if (countHeader) {
                    countWords(analyzer, words, in, overallCounts);
                }
                line = reader.readLine();
            } while (line != null && line.startsWith(" "));
        }
        if (leakType < 3) {
            countWords(analyzer, words, reader, overallCounts);
        }
    } finally {
        Closeables.close(reader, true);
    }

    Vector v = new RandomAccessSparseVector(FEATURES);
    bias.addToVector("", 1, v);
    for (String word : words.elementSet()) {
        encoder.addToVector(word, Math.log1p(words.count(word)), v);
    }

    return v;
}

From source file:org.apache.mahout.classifier.naivebayes.BayesUtils.java

/** Write the list of labels into a map file */
public static int writeLabelIndex(Configuration conf, Iterable<String> labels, Path indexPath)
        throws IOException {
    FileSystem fs = FileSystem.get(indexPath.toUri(), conf);
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, indexPath, Text.class, IntWritable.class);
    int i = 0;/*  w  w w  .  j av  a2  s  . co  m*/
    try {
        for (String label : labels) {
            writer.append(new Text(label), new IntWritable(i++));
        }
    } finally {
        Closeables.close(writer, false);
    }
    return i;
}

From source file:org.kitesdk.data.filesystem.ParquetFileSystemDatasetWriter.java

@Override
public void close() {
    if (state.equals(ReaderWriterState.OPEN)) {
        logger.debug("Closing pathTmp:{}", pathTmp);

        try {/*from  ww  w .  j a v a2  s.c  o m*/
            Closeables.close(avroParquetWriter, false);
        } catch (IOException e) {
            throw new DatasetWriterException(
                    "Unable to close writer:" + avroParquetWriter + " to path:" + pathTmp);
        }

        logger.debug("Committing pathTmp:{} to path:{}", pathTmp, path);

        try {
            if (!fileSystem.rename(pathTmp, path)) {
                throw new DatasetWriterException("Failed to move " + pathTmp + " to " + path);
            }
        } catch (IOException e) {
            throw new DatasetWriterException("Internal error while trying to commit path:" + pathTmp, e);
        }

        state = ReaderWriterState.CLOSED;
    }
}

From source file:org.asoem.greyfish.utils.persistence.Persisters.java

public static void serialize(final Persister persister, final Object object,
        final OutputSupplier<? extends OutputStream> outputSupplier) throws IOException {
    final OutputStream output = outputSupplier.getOutput();
    boolean threw = true;
    try {/*  w w  w. jav  a 2  s .c  om*/
        persister.serialize(object, output);
        threw = false;
    } finally {
        Closeables.close(output, threw);
    }
}

From source file:com.android.ide.common.resources.IdResourceParser.java

/**
 * Parse the given input and register ids with the given
 * {@link IValueResourceRepository}.//  w w  w.j av  a  2 s .  c  o  m
 *
 * @param type the type of resource being scanned
 * @param path the full OS path to the file being parsed
 * @param input the input stream of the XML to be parsed (will be closed by this method)
 * @return true if parsing succeeds and false if it fails
 * @throws IOException if reading the contents fails
 */
public boolean parse(ResourceType type, final String path, InputStream input) throws IOException {
    KXmlParser parser = new KXmlParser();
    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);

        if (input instanceof FileInputStream) {
            input = new BufferedInputStream(input);
        }
        parser.setInput(input, SdkConstants.UTF_8);

        return parse(type, path, parser);
    } catch (XmlPullParserException e) {
        String message = e.getMessage();

        // Strip off position description
        int index = message.indexOf("(position:"); //$NON-NLS-1$ (Hardcoded in KXml)
        if (index != -1) {
            message = message.substring(0, index);
        }

        String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$
                path, parser.getLineNumber(), message);
        mContext.addError(error);
        return false;
    } catch (RuntimeException e) {
        // Some exceptions are thrown by the KXmlParser that are not XmlPullParserExceptions,
        // such as this one:
        //    java.lang.RuntimeException: Undefined Prefix: w in org.kxml2.io.KXmlParser@...
        //        at org.kxml2.io.KXmlParser.adjustNsp(Unknown Source)
        //        at org.kxml2.io.KXmlParser.parseStartTag(Unknown Source)
        String message = e.getMessage();
        String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$
                path, parser.getLineNumber(), message);
        mContext.addError(error);
        return false;
    } finally {
        try {
            Closeables.close(input, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }
}

From source file:com.complexible.common.io.Files2.java

/**
 * Zip the given directory.//from ww  w  .java 2s.co m
 * @param theDir the directory to zip
 * @param theOutputFile the zip file to write to
 * @throws IOException thrown if there is an error while zipping the directory or while saving the results.
 */
public static void zipDirectory(File theDir, File theOutputFile) throws IOException {
    ZipOutputStream aZipOut = new ZipOutputStream(new FileOutputStream(theOutputFile));

    try {
        Collection<File> aFileList = listFiles(theDir);

        String aPathToRemove = theDir.getAbsolutePath().substring(0,
                theDir.getAbsolutePath().lastIndexOf(File.separator));

        for (File aFile : aFileList) {
            FileInputStream aFileIn = new FileInputStream(aFile);

            try {
                ZipEntry aZipEntry = new ZipEntry(aFile.getAbsolutePath().substring(
                        aFile.getAbsolutePath().indexOf(aPathToRemove) + aPathToRemove.length() + 1));

                aZipOut.putNextEntry(aZipEntry);

                ByteStreams.copy(aFileIn, aZipOut);
            } finally {
                aZipOut.closeEntry();
                Closeables.close(aFileIn, false);
            }
        }
    } finally {
        Closeables.close(aZipOut, false);
    }
}

From source file:com.puppycrawl.tools.checkstyle.PropertyCacheFile.java

/**
 * Flushes and closes output stream.//  w w w  .ja v  a2  s .c om
 * @param stream the output stream
 * @throws IOException  when there is a problems with file flush and close
 */
private static void flushAndCloseOutStream(OutputStream stream) throws IOException {
    if (stream != null) {
        Flushables.flush(stream, false);
    }
    Closeables.close(stream, false);
}

From source file:org.openqa.selenium.io.Zip.java

public void unzip(File source, File outputDir) throws IOException {
    FileInputStream fis = null;//from w  w  w  . j  a  v  a  2  s . co m

    try {
        fis = new FileInputStream(source);
        unzip(fis, outputDir);
    } finally {
        Closeables.close(fis, false);
    }
}

From source file:ch.ledcom.maven.sitespeed.SiteSpeedOrchestrator.java

public void siteSpeed() throws IOException {

    if (!outputDir.exists()) {
        outputDir.mkdirs();//  w w w .j  av a2 s  . co m
    }

    // crawl site to get the list of URLs to analyze
    crawler.crawl(new URICallback() {
        @Override
        public void submit(final URI uri) {
            try {
                final URL url = uri.toURL();
                final Future<Document> documentFuture = analyzerService.submit(new Callable<Document>() {
                    @Override
                    public Document call() throws Exception {
                        log.info("Received URL to analyze [" + url.toExternalForm() + "]");
                        return analyzer.analyze(url);
                    }
                });

                reportService.execute(new Runnable() {
                    @Override
                    public void run() {
                        Writer out = null;
                        boolean threw = true;
                        try {
                            out = createReportWriter(uri);
                            log.info("Creating report for URL [" + url.toExternalForm() + "]");
                            Document doc = documentFuture.get();
                            log.info("Document to report for URL [" + url.toExternalForm() + "]");
                            log.debug(XmlPrettyPrinter.prettyPrint(doc));
                            reporter.report(doc, out);
                            threw = false;
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                Closeables.close(out, threw);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } finally {
                crawler.shutdown();
            }
        }
    });

    new ResourceFiles().export(outputDir);

    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.geogig.osm.internal.coordcache.MappedIndex.java

public void close() {
    try {//from   w  w w  .  j av  a2  s. c om
        Closeables.close(indexChannel, true);
        Closeables.close(randomAccessFile, true);
    } catch (IOException e) {
        //
    }
    currBuffer = null;
    indexChannel = null;
    indexFile.delete();
}