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

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

Introduction

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

Prototype

public static void closeQuietly(@Nullable Reader reader) 

Source Link

Document

Closes the given Reader , logging any IOException that's thrown rather than propagating it.

Usage

From source file:io.druid.query.ReferenceCountingSegmentQueryRunner.java

@Override
public Sequence<T> run(final Query<T> query) {
    final Closeable closeable = adapter.increment();
    try {//from   www.  j a va2 s . c  om
        final Sequence<T> baseSequence = factory.createRunner(adapter).run(query);

        return new ResourceClosingSequence<T>(baseSequence, closeable);
    } catch (RuntimeException e) {
        Closeables.closeQuietly(closeable);
        throw e;
    }
}

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

/**
 * /*from www .  j a va  2 s .c  o  m*/
 * @param key SequenceFile key
 * @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.closeQuietly(writer);
    }

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

From source file:com.netflix.exhibitor.core.controlpanel.FileBasedPreferences.java

/**
 * @param file file to use as backing store
 * @throws IOException errors// w ww  .  j a  v  a 2 s . c  om
 */
public FileBasedPreferences(File file) throws IOException {
    super(null, "");
    this.file = file;

    if (file.exists()) {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        try {
            properties.load(in);
        } finally {
            Closeables.closeQuietly(in);
        }
    }
}

From source file:org.trancecode.asciidoc.Archives.java

private static File unzipEntry(final ZipFile archive, final ZipEntry entry, final File targetDirectory)
        throws IOException {
    if (entry.isDirectory()) {
        final File directory = new File(targetDirectory, entry.getName());
        mkdirs(directory);/* w  w w. j av  a 2 s . c  o m*/
        return directory;
    }

    final File outputFile = new File(targetDirectory, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        mkdirs(outputFile.getParentFile());
    }

    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(archive.getInputStream(entry));
        out = new BufferedOutputStream(new FileOutputStream(outputFile));
        ByteStreams.copy(in, out);
    } finally {
        Closeables.closeQuietly(out);
        Closeables.closeQuietly(in);
    }

    return outputFile;
}

From source file:org.apache.mahout.graph.GraphTestCase.java

protected <T extends WritableComparable> void writeComponents(File destination, Configuration conf,
        Class<T> componentClass, T... components) throws IOException {
    Path path = new Path(destination.getAbsolutePath());
    FileSystem fs = FileSystem.get(path.toUri(), conf);

    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, componentClass, NullWritable.class);
    try {//from w  ww . ja v  a  2s .  c o m
        for (T component : components) {
            writer.append(component, NullWritable.get());
        }
    } finally {
        Closeables.closeQuietly(writer);
    }
}

From source file:com.andreasfink.utils.text.Charsets.java

public static String detectCharset(final File file) {
    final FileInputStream fis;

    try {//w w  w  . j  ava2  s.  c  om
        fis = new FileInputStream(file);
    } catch (final FileNotFoundException e) {
        throw new IllegalArgumentException("Unable to detect charset!", e);
    }

    final String encoding = detectCharset(fis);
    Closeables.closeQuietly(fis);

    return encoding;
}

From source file:com.facebook.tsdb.tsdash.server.PlotImageEndpoint.java

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("image/png");

    final String filename = new File(req.getPathInfo()).getName();

    File file = new File(TsdbServlet.plotsDir, filename);
    FileInputStream in = new FileInputStream(file);

    resp.setBufferSize(BUFFER_SIZE);//w ww  . j  a va 2s .  co m

    final ServletOutputStream out = resp.getOutputStream();

    try {
        byte[] buffer = new byte[1024];
        while ((in.read(buffer)) != -1) {
            out.write(buffer);
        }
    } finally {
        Closeables.closeQuietly(in);
    }

    out.flush();
    resp.flushBuffer();
}

From source file:com.nesscomputing.migratory.loader.UTF8StringContentConverter.java

@Override
public String convert(HttpRequest httpClientRequest, HttpResponse httpClientResponse, InputStream inputStream)
        throws IOException {
    final int responseCode = httpClientResponse.getStatusLine().getStatusCode();
    switch (responseCode) {
    case 200:/* w w  w .j a  v a 2s  .  c om*/
    case 201:
        final InputStreamReader reader = new InputStreamReader(inputStream, Charsets.UTF_8);

        try {
            return CharStreams.toString(reader);
        } finally {
            Closeables.closeQuietly(reader);
        }

    case 204:
        return "";

    default:
        throw new IOException("Could not load file from " + httpClientRequest.getRequestLine().getUri() + " ("
                + httpClientResponse.getStatusLine().getStatusCode() + ")");
    }
}

From source file:org.apache.mahout.classifier.sequencelearning.hmm.hadoop.MapWritableCache.java

/**
 * Wraps a (Key, MapWritable) into a sequence file and stores it to the specified directory.
 *
 * @param key           key of the SequenceFile
 * @param map           MapWritable to be stored
 * @param output        directory where the sequence file should be stored
 * @param conf          configuration// w ww .j  a va2s.  c o m
 * @param overwritePath if true, overwrite the file present in the output directory
 * @param deleteOnExit  if true. deletes the map on exiting
 * @throws IOException
 */
public static void save(Writable key, MapWritable map, Path output, Configuration conf, boolean overwritePath,
        boolean deleteOnExit) throws IOException {

    FileSystem fs = FileSystem.get(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,
            MapWritable.class);
    try {
        writer.append(key, new MapWritable(map));
    } finally {
        Closeables.closeQuietly(writer);
    }

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

From source file:org.sonar.plugins.pmd.SonarWayProfile.java

@Override
public RulesProfile createProfile(ValidationMessages validation) {
    Reader config = null;/* w w w. j a  v a  2  s .co  m*/
    try {
        config = new InputStreamReader(
                this.getClass().getResourceAsStream("/org/sonar/plugins/pmd/profile-sonar-way.xml"));

        RulesProfile profile = importer.importProfile(config, validation);
        profile.setLanguage(Java.KEY);
        profile.setName(RulesProfile.SONAR_WAY_NAME);

        return profile;
    } finally {
        Closeables.closeQuietly(config);
    }
}