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:com.aerofs.baseline.http.ChunkedDownloadResource.java

@GET
public ChunkedOutput<byte[]> getChunked() throws NoSuchAlgorithmException {
    String digested = Resources.getHexDigest(ChunkedDownloadResource.RESOURCE_BYTES);
    LOGGER.info("chunked digest:{}", digested);

    final ChunkedOutput<byte[]> output = new ChunkedOutput<>(byte[].class);

    new Thread(new Runnable() {
        @Override/* w  w  w  .j  a va  2  s  .c om*/
        public void run() {
            try {
                for (int i = 0; i < RESOURCE_BYTES.length; i++) {
                    output.write(RESOURCE_BYTES[i]);
                    LOGGER.info("write chunk {}", i);
                    Thread.sleep(1000);
                }
            } catch (IOException e) {
                LOGGER.warn("fail chunked output write", e);
            } catch (InterruptedException e) {
                LOGGER.warn("interrupted during chunked output write");
            } finally {
                try {
                    Closeables.close(output, true);
                } catch (IOException e) {
                    LOGGER.warn("fail close chunked output");
                }
            }
        }
    }).start();

    return output;
}

From source file:org.cleansvg.CleanTask.java

private Document input() throws IOException {
    final Reader reader = readerSupplier.getInput();
    boolean thrown = true;
    try {/* w  w w  . ja  v  a 2  s  .c  o m*/
        final Document document = getDocument(reader);
        thrown = false;
        return document;
    } finally {
        Closeables.close(reader, thrown);
    }
}

From source file:com.turn.ttorrent.protocol.torrent.Torrent.java

@Nonnull
private static Map<String, BEValue> load(@Nonnull File file) throws IOException {
    InputStream in = new FileInputStream(file);
    try {// w  w  w  .j  av  a2 s  . c om
        return new StreamBDecoder(in).bdecodeMap().getMap();
    } finally {
        Closeables.close(in, true);
    }
}

From source file:org.kitesdk.data.spi.Schemas.java

public static Schema fromAvro(FileSystem fs, Path location) throws IOException {
    InputStream in = null;//from  w ww. j a  va 2  s . c  o m
    boolean threw = true;

    try {
        in = fs.open(location);
        Schema schema = fromAvro(in);
        threw = false;
        return schema;
    } finally {
        Closeables.close(in, threw);
    }
}

From source file:net.yxy.athena.service.server.ComputeService.java

public void close() throws IOException {
    Closeables.close(novaApi, true);
}

From source file:org.kitesdk.data.spi.filesystem.AvroAppender.java

@Override
public void close() throws IOException {
    Closeables.close(dataFileWriter, false);
}

From source file:de.tuberlin.dima.cuttlefish.preprocessing.vectorization.FeatureDictionary.java

public void writeToFile(File file) throws IOException {
    SortedMap<Integer, String> features = new TreeMap<Integer, String>();

    for (Map.Entry<String, Map<String, Integer>> fieldEntry : textFeatureOffsets.entrySet()) {
        String field = fieldEntry.getKey();
        for (Map.Entry<String, Integer> featureWithOffset : fieldEntry.getValue().entrySet()) {
            features.put(featureWithOffset.getValue(), field + "=" + featureWithOffset.getKey());
        }/*  ww w.ja  va  2s  .com*/
    }

    BufferedWriter writer = null;

    try {
        writer = new BufferedWriter(new FileWriter(file));

        for (Map.Entry<Integer, String> feature : features.entrySet()) {
            writer.write(String.valueOf(feature.getKey()));
            writer.write(" ");
            writer.write(feature.getValue());
            writer.newLine();
        }

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

From source file:hihex.cs.PidEntry.java

/**
 * Close the file it is currently writing to.
 *///from   ww  w.j a v  a2s  .co m
public PidEntry close() {
    if (writer.isPresent()) {
        final Writer writer2 = writer.get();
        try {
            writer2.flush();
        } catch (final IOException e) {
            CsLog.e("Failed to flush file, some data may not be written. " + e);
        } finally {
            try {
                Closeables.close(writer2, true);
            } catch (IOException e) {
                // Ignore.
            }
        }
    }
    return new PidEntry(pid, processName, mThreadNames);
}

From source file:com.netflix.priam.utils.JMXNodeTool.java

/**
 * This method will test if you can connect and query something before handing over the connection,
 * This is required for our retry logic.
 *//*  w  w  w .  j  a v a2s  .  c  o  m*/
private static boolean testConnection() {
    // connecting first time hence return false.
    if (tool == null) {
        return false;
    }

    try {
        tool.isInitialized();
    } catch (Throwable ex) {
        try {
            Closeables.close(tool, true);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        return false;
    }
    return true;
}

From source file:org.apache.mahout.common.iterator.sequencefile.SequenceFileValueIterator.java

@Override
public void close() throws IOException {
    value = null;
    Closeables.close(reader, true);
    endOfData();
}