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.moe.designer.gradle.PropertiesUtil.java

public static void savePropertiesToFile(@NotNull Properties properties, @NotNull File filePath,
        @Nullable String comments) throws IOException {
    FileUtilRt.createParentDirs(filePath);
    FileOutputStream out = null;//w w  w  .  j  a  v a2s. com
    try {
        //noinspection IOResourceOpenedButNotSafelyClosed
        out = new FileOutputStream(filePath);
        // Note that we don't write the properties files in UTF-8; this will *not* write the
        // files with the default platform encoding; instead, it will write it using ISO-8859-1 and
        // \\u escaping syntax for other characters. This will work with older versions of the Gradle
        // plugin which does not read the .properties file with UTF-8 encoding. In the future when
        // nobody is using older (0.7.x) versions of the Gradle plugin anymore we can upgrade this
        properties.store(out, comments);
    } finally {
        Closeables.close(out, true);
    }
}

From source file:org.apache.mahout.clustering.spectral.VectorCache.java

/**
 * @param key    SequenceFile key/* w ww  . ja  va  2 s .co m*/
 * @param vector Vector to save, to be wrapped as VectorWritable
 */
public static void save(Writable key, Vector vector, Path output, Configuration conf, boolean overwritePath,
        boolean deleteOnExit) throws IOException {

    FileSystem fs = FileSystem.get(output.toUri(), conf);
    output = fs.makeQualified(output);
    if (overwritePath) {
        HadoopUtil.delete(conf, output);
    }

    // set the cache
    DistributedCache.setCacheFiles(new URI[] { output.toUri() }, conf);

    // set up the writer
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, output, IntWritable.class,
            VectorWritable.class);
    try {
        writer.append(key, new VectorWritable(vector));
    } finally {
        Closeables.close(writer, false);
    }

    if (deleteOnExit) {
        fs.deleteOnExit(output);
    }
}

From source file:com.uber.hoodie.metrics.Metrics.java

private Metrics(HoodieWriteConfig metricConfig) throws ConfigurationException {
    registry = new MetricRegistry();

    reporter = MetricsReporterFactory.createReporter(metricConfig, registry);
    if (reporter == null) {
        throw new RuntimeException("Cannot initialize Reporter.");
    }//w w w  .j a  v a 2  s  .c  o  m
    //        reporter.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                reporter.report();
                Closeables.close(reporter.getReporter(), true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:cc.recommenders.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;/* w w w . j av a 2s .  com*/
    try {
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        Collection<File> files = FileUtils.listFiles(directory, FILE, DIRECTORY);
        for (File f : files) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/");
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            byte[] data = Files.toByteArray(f);
            zos.write(data);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:minium.web.internal.compressor.ResourceFunctions.java

private static void closeQuietly(Closeable closeable) {
    try {/*from w w w.  j a  v  a 2s.c  om*/
        Closeables.close(closeable, true);
    } catch (IOException e) {
        // it will never get here
    }
}

From source file:org.apache.parquet.cli.util.Schemas.java

public static Schema fromAvro(InputStream in) throws IOException {
    GenericDatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>();
    DataFileStream<GenericRecord> stream = null;
    boolean threw = true;

    try {//from   ww  w  .  j a v a  2 s.  co  m
        stream = new DataFileStream<>(in, datumReader);
        Schema schema = stream.getSchema();
        threw = false;
        return schema;
    } finally {
        Closeables.close(stream, threw);
    }
}

From source file:org.apache.mahout.math.hadoop.stochasticsvd.SSVDHelper.java

/**
 * load single vector from an hdfs file (possibly presented as glob).
 */// w  w  w.j ava2  s . c o  m
static Vector loadVector(Path glob, Configuration conf) throws IOException {

    SequenceFileDirValueIterator<VectorWritable> iter = new SequenceFileDirValueIterator<VectorWritable>(glob,
            PathType.GLOB, null, null, true, conf);

    try {
        if (!iter.hasNext()) {
            throw new IOException("Empty input while reading vector");
        }
        VectorWritable vw = iter.next();

        if (iter.hasNext()) {
            throw new IOException("Unexpected data after the end of vector file");
        }

        return vw.get();

    } finally {
        Closeables.close(iter, true);
    }
}

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

public void zip(File inputDir, File output) throws IOException {
    if (output.exists()) {
        throw new IOException("File already exists: " + output);
    }//  w w w. j a  v  a  2  s .c  o m

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(output);
        zip(inputDir, fos);
    } finally {
        Closeables.close(fos, false);
    }
}

From source file:com.vilt.minium.impl.debug.WindowScreenshotInteraction.java

@Override
protected void doPerform() {
    try {/*from  w  w  w .  j  a  v  a2s. c  o m*/
        CoreWebElements<?> coreWebElements = getSource();
        WebElementsDriver<?> webDriver = coreWebElements.as(WebElementsDriverProvider.class).webDriver();
        byte[] screenshot = webDriver.getScreenshotAs(OutputType.BYTES);
        ByteSource.wrap(screenshot).copyTo(stream);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            Closeables.close(stream, true);
        } catch (IOException e) {
        }
    }
}

From source file:tv.icntv.sender.compress.AbstractCompress.java

@Override
public void close(BufferedWriter writer) {
    try {//from  w w w  . j a  va 2s . c  o m
        Closeables.close(writer, true);
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
}