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.xemantic.tadedon.guava.jar.Manifests.java

public static Manifest getManifest(URL url) {
    try {/*from w  w  w  .  jav  a 2  s  .  c o m*/
        Manifest manifest = new Manifest();
        boolean threw = true;
        InputStream in = url.openStream();
        try {
            manifest.read(in);
            threw = false;
        } finally {
            Closeables.close(in, threw);
        }
        return manifest;
    } catch (IOException e) {
        throw new RuntimeException("Cannot read manifest file from given URL", e);
    }
}

From source file:org.apache.mahout.cf.taste.similarity.precompute.example.GroupLensDataModel.java

private static File convertGLFile(File originalFile) throws IOException {
    // Now translate the file; remove commas, then convert "::" delimiter to comma
    File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "ratings.txt");
    if (resultFile.exists()) {
        resultFile.delete();//from ww w . j  a  v a  2 s . c  o  m
    }
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8);
        for (String line : new FileLineIterable(originalFile, false)) {
            int lastDelimiterStart = line.lastIndexOf(COLON_DELIMTER);
            if (lastDelimiterStart < 0) {
                throw new IOException("Unexpected input format on line: " + line);
            }
            String subLine = line.substring(0, lastDelimiterStart);
            String convertedLine = COLON_DELIMITER_PATTERN.matcher(subLine).replaceAll(",");
            writer.write(convertedLine);
            writer.write('\n');
        }
    } catch (IOException ioe) {
        resultFile.delete();
        throw ioe;
    } finally {
        Closeables.close(writer, false);
    }
    return resultFile;
}

From source file:org.apache.mahout.utils.vectors.io.DelimitedTermInfoWriter.java

@Override
public void write(TermInfo ti) throws IOException {

    Iterator<TermEntry> entIter = ti.getAllEntries();
    try {/*from www. ja v a  2s . c o m*/
        writer.write(String.valueOf(ti.totalTerms(field)));
        writer.write('\n');
        writer.write("#term" + delimiter + "doc freq" + delimiter + "idx");
        writer.write('\n');
        while (entIter.hasNext()) {
            TermEntry entry = entIter.next();
            writer.write(entry.getTerm());
            writer.write(delimiter);
            writer.write(String.valueOf(entry.getDocFreq()));
            writer.write(delimiter);
            writer.write(String.valueOf(entry.getTermIdx()));
            writer.write('\n');
        }
    } finally {
        Closeables.close(writer, false);
    }
}

From source file:com.baidu.sqlengine.memory.unsafe.utils.sort.UnsafeSorterSpillReader.java

public UnsafeSorterSpillReader(SerializerManager serializerManager, File file, ConnectionId blockId)
        throws IOException {
    assert (file.length() > 0);
    final BufferedInputStream bs = new BufferedInputStream(new FileInputStream(file));
    try {//from w ww  . j  a v  a2 s .  co m
        this.in = serializerManager.wrapForCompression(blockId, bs);
        this.din = new DataInputStream(this.in);
        numRecords = numRecordsRemaining = din.readInt();
    } catch (IOException e) {
        Closeables.close(bs, /* swallowIOException = */ true);
        throw e;
    }
}

From source file:jp.tomorrowkey.irkit4j.http.HttpClient.java

@VisibleForTesting
void write(OutputStream outputStream, byte[] content) throws IOException {
    try {//  w  w  w .ja  v  a  2s.  c om
        outputStream.write(content);
        outputStream.flush();
    } finally {
        Closeables.close(outputStream, true);
    }
}

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

private void serialize(final Object object, final ByteSink byteSink) throws IOException {
    final OutputStream output = byteSink.openBufferedStream();
    boolean threw = true;
    try {/*from   www  .j av  a2  s  . c o m*/
        serialize(object, output);
        threw = false;
    } finally {
        Closeables.close(output, threw);
    }
}

From source file:com.android.tools.idea.gradle.invoker.GradleOutputForwarder.java

void close() {
    try {//from   w ww  . j av a 2 s. com
        Closeables.close(myOutput, true /* swallowIOException */);
        Closeables.close(myStdErr, true /* swallowIOException */);
    } catch (IOException e) {
        // Cannot happen
    }
}

From source file:io.fabric8.etcd.core.EtcdClientImpl.java

@Override
public void stop() {
    try {/*ww  w.j  a  va  2s .c o m*/
        Closeables.close(client, true);
    } catch (IOException e) {
        //ignore
    }
}

From source file:org.excalibur.discovery.service.zoo.ServiceDiscoveryResource.java

@Override
public void close() throws IOException {
    if (started.compareAndSet(true, false)) {
        Closeables.close(this.discovery, true);
    }//from   w  w w  .  j  a  v  a  2  s  .co m
}

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

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