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:de.haber.xmind2latex.cli.PropertyLoader.java

/**
 * @param fileName file name of the property file
 * @param propNames names of the properties to read out
 *
 * @return reads out properties 'propName' from the given property file 'fileName' and stored
 * them in the result list in the same order. If a property is not available or the property
 * file cannot be loaded, the corresponding {@link Optional} is absent.
 *///  w w w.  j a va 2s.  co  m
public static List<Optional<String>> getProperties(String fileName, String... propNames) {
    checkNotNull(fileName);
    checkNotNull(propNames);

    List<Optional<String>> result = new ArrayList<Optional<String>>(propNames.length);

    Properties prop = new Properties();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream stream = loader.getResourceAsStream(fileName);
    try {
        prop.load(stream);
        for (String p : propNames) {
            String versionNumber = prop.getProperty(p);
            result.add(Optional.fromNullable(versionNumber));
        }
    } catch (Exception e) {
        for (int i = 0; i < propNames.length; i++) {
            result.add(Optional.<String>absent());
        }
    } finally {
        try {
            Closeables.close(stream, true);
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
    return ImmutableList.copyOf(result);
}

From source file:org.jooby.internal.FastByteArrayOutputStream.java

@Override
public void close() throws IOException {
    headers.accept("Content-Length", count + "");
    OutputStream out = null;//from w  w  w  . j  ava 2  s. c o  m
    try {
        out = stream.get();
        out.write(buf, 0, count);
    } finally {
        Closeables.close(out, true);
    }
}

From source file:org.geoserver.catalog.util.CloseableIteratorAdapter.java

/**
 * Closes the wrapped iterator if its an instance of {@code CloseableIterator}, does nothing
 * otherwise; override if needed./*from  ww w .  ja  v  a 2s .  com*/
 * 
 * @see java.io.Closeable#close()
 */
@Override
public void close() {
    try {
        Closeables.close(whatToClose, false);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        whatToClose = null;
    }
}

From source file:sg.atom.utils._beta.functional.BaseSequence.java

@Override
public <OutType> OutType accumulate(OutType initValue, final Accumulator<OutType, T> fn) {
    Yielder<OutType> yielder = null;
    try {/*from  ww  w .ja  va 2  s .co m*/
        yielder = toYielder(initValue, YieldingAccumulators.fromAccumulator(fn));
        return yielder.get();
    } finally {
        try {
            Closeables.close(yielder, true);
        } catch (IOException ioe) {

        }
    }
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.utils.Zips.java

public static void unzip(File zipFile, File destFolder) throws IOException {
    ZipInputStream zis = null;/*from w  w w  . j  a  va2 s .co  m*/
    try {
        zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(destFolder, entry.getName());
                Files.createParentDirs(file);
                Files.asByteSink(file, FileWriteMode.APPEND).writeFrom(zis);
            }
        }
    } finally {
        Closeables.close(zis, true);
    }
}

From source file:ch.ledcom.maven.sitespeed.httpserver.HttpTestServer.java

public HttpTestServer(final int port, final Map<String, String> pages) throws IOException {
    server = HttpServer.create(new InetSocketAddress(port), 0);

    for (final Entry<String, String> page : pages.entrySet()) {
        server.createContext(page.getKey(), new HttpHandler() {
            @Override/*from  w  w  w .  java2 s  .com*/
            public void handle(HttpExchange t) throws IOException {
                t.sendResponseHeaders(200, page.getValue().length());
                OutputStream out = null;
                boolean threw = true;
                try {
                    out = t.getResponseBody();
                    out.write(page.getValue().getBytes());
                    threw = false;
                } finally {
                    Closeables.close(out, threw);
                }
            }
        });
    }

    server.setExecutor((Executor) null);
}

From source file:org.anarres.qemu.examples.AbstractQEmuExample.java

@Nonnull
private File download(@Nonnull URI source) throws IOException {
    File dir = new File("build/images/downloaded");
    HashCode hash = Hashing.md5().hashString(source.toString(), Charsets.UTF_8);
    File file = new File(dir, hash.toString());
    if (!file.exists()) {
        InputStream in = source.toURL().openStream();
        try {//from  ww w.j a  v  a  2s.  c  o  m
            Files.asByteSink(file).writeFrom(in);
        } finally {
            Closeables.close(in, false);
        }
    }
    return file;
}

From source file:ezbake.security.EzSecurityClientIT.java

@After
public void closeClient() throws IOException {
    Closeables.close(ezbakeSecurityClient, true);
}

From source file:eu.interedition.text.xml.XMLParser.java

public Text parse(Text source, final XMLParserConfiguration configuration)
        throws IOException, XMLStreamException {
    Preconditions.checkArgument(source.getType() == Text.Type.XML);
    final Text target = textRepository.create(Text.Type.TXT);
    final XMLParserState state = new XMLParserState(source, target, configuration);
    try {/*from w  w w.  ja  v a  2  s  .  co  m*/
        textRepository.read(source, new XMLParserTextConsumer(state));
        Reader textReader = null;
        try {
            return textRepository.write(state.getTarget(), textReader = state.readText());
        } finally {
            Closeables.close(textReader, false);
        }
    } catch (Throwable t) {
        Throwables.propagateIfInstanceOf(t, IOException.class);
        Throwables.propagateIfInstanceOf(Throwables.getRootCause(t), XMLStreamException.class);
        throw Throwables.propagate(t);
    }
}

From source file:com.github.autermann.sockets.ssl.KeyStoreOptions.java

public KeyStore read() throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    KeyStore ks = KeyStore.getInstance(getType());
    FileInputStream in = null;/*w  w w  .j  a v a  2  s. c om*/
    try {
        in = new FileInputStream(getPath());
        ks.load(in, getPass().toCharArray());
        return ks;
    } finally {
        Closeables.close(in, true);
    }
}