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.n52.iceland.util.JSONUtils.java

public static String print(JsonNode node) {
    final StringWriter writer = new StringWriter();
    try {/*from w w  w . j av  a  2s . co m*/
        print(writer, node);
        writer.flush();
    } catch (IOException e) {
        // cannot happen
    } finally {
        try {
            Closeables.close(writer, true);
        } catch (IOException ioe) {
            LOGGER.error("Error while colsing closeable!", ioe);
        }
    }
    return writer.toString();
}

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

/**
 * Loads a Vector from the specified path. Returns null if no vector exists.
 *//*ww  w.  j ava  2  s.c om*/
public static Vector load(Configuration conf, Path input) throws IOException {
    log.info("Loading vector from: {}", input);
    SequenceFileValueIterator<VectorWritable> iterator = new SequenceFileValueIterator<VectorWritable>(input,
            true, conf);
    try {
        return iterator.next().get();
    } finally {
        Closeables.close(iterator, true);
    }
}

From source file:org.lenskit.util.table.writer.CSVWriter.java

/**
 * Open a CSV writer to write to a file.
 *
 * @param file        The file to write to.
 * @param layout      The layout of the table.
 * @param compression What compression, if any, to use.
 * @return A CSV writer outputting to {@code file}.
 * @throws IOException if there is an error opening the file or writing the column header.
 *///from  w  ww  .  j a  v  a  2s  .  c o m
public static CSVWriter open(File file, TableLayout layout, CompressionMode compression) throws IOException {
    Files.createParentDirs(file);
    Writer writer = LKFileUtils.openOutput(file, Charset.defaultCharset(), compression);
    try {
        return new CSVWriter(writer, layout);
    } catch (Exception ex) {
        Closeables.close(writer, true);
        Throwables.propagateIfInstanceOf(ex, IOException.class);
        throw Throwables.propagate(ex);
    }
}

From source file:net.sf.lucis.core.impl.DefaultLucisSearcher.java

public void close() {
    try {// w w  w  . j  a v a2 s.  com
        Closeables.close(searcher, true);
        Closeables.close(reader, true);
    } catch (IOException e) {
        // TODO
    }
}

From source file:org.jclouds.examples.rackspace.cdn.DeleteService.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 ww .  j ava  2 s .  com*/
public void close() throws IOException {
    Closeables.close(cdnApi, true);
}

From source file:org.cleansvg.CleanTask.java

private void output(final Document document) throws IOException, TranscoderException {
    final Writer writer = writerSupplier.getOutput();
    boolean thrown = true;
    try {//  w  ww  .j  a v  a 2  s. c  om
        transcode(document, writer);
        thrown = false;
    } finally {
        Closeables.close(writer, thrown);
    }
}

From source file:org.codice.solr.factory.impl.Closer.java

/**
 * Closes all registered {@link Closeable}s since the creation or since the last call to {@link
 * #returning} whichever came first.//from   w w  w .  j a  va2s.c o  m
 */
@Override
public void close() {
    while (!closeables.isEmpty()) {
        try {
            Closeables.close(closeables.removeFirst(), true);
        } catch (IOException e) { // exceptions are suppressed above
        }
    }
}

From source file:org.jclouds.examples.rackspace.clouddatabases.CreateUser.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.
 *//*from  w  w  w.  j a va 2 s.  co  m*/
public void close() throws IOException {
    Closeables.close(troveApi, true);
}

From source file:com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.java

@Override
protected void processFiltered(File file, List<String> lines) {
    // Cannot use lines as the line separators have been removed!
    try {//  w w  w.  j a v a 2s. c o  m
        final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        boolean threw = true;
        try {
            if (!endsWithNewline(randomAccessFile)) {
                log(0, MSG_KEY_NO_NEWLINE_EOF, file.getPath());
            }
            threw = false;
        } finally {
            Closeables.close(randomAccessFile, threw);
        }
    } catch (final IOException ignored) {
        log(0, MSG_KEY_UNABLE_OPEN, file.getPath());
    }
}

From source file:org.apache.mahout.common.distance.WeightedDistanceMeasure.java

@Override
public void configure(Configuration jobConf) {
    if (parameters == null) {
        ParameteredGeneralizations.configureParameters(this, jobConf);
    }/* w w w . ja  va2s. c o  m*/
    try {
        if (weightsFile.get() != null) {
            FileSystem fs = FileSystem.get(weightsFile.get().toUri(), jobConf);
            VectorWritable weights = ClassUtils
                    .instantiateAs((Class<? extends VectorWritable>) vectorClass.get(), VectorWritable.class);
            if (!fs.exists(weightsFile.get())) {
                throw new FileNotFoundException(weightsFile.get().toString());
            }
            DataInputStream in = fs.open(weightsFile.get());
            try {
                weights.readFields(in);
            } finally {
                Closeables.close(in, true);
            }
            this.weights = weights.get();
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}