Example usage for com.google.common.io Closer close

List of usage examples for com.google.common.io Closer close

Introduction

In this page you can find the example usage for com.google.common.io Closer close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes all Closeable instances that have been added to this Closer .

Usage

From source file:org.gradle.caching.internal.LocalDirectoryBuildCacheService.java

@Override
public void store(final BuildCacheKey key, final BuildCacheEntryWriter result) throws BuildCacheException {
    persistentCache.useCache(new Runnable() {
        @Override// ww w.  ja va  2 s  .  c  om
        public void run() {
            File file = getFile(key.getHashCode());
            try {
                Closer closer = Closer.create();
                OutputStream output = closer.register(new FileOutputStream(file));
                try {
                    result.writeTo(output);
                } finally {
                    closer.close();
                }
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }
    });
}

From source file:ch.ledcom.tomcat.valves.SessionSerializableCheckerValve.java

/**
 * Check if an object is serializable, emit a warning log if it is not.
 *
 * @param attribute// w w w .  j av a  2  s  . c o m
 *            the attribute to check
 * @throws IOException
 */
private void checkSerializable(final Object attribute) throws IOException {
    if (!Serializable.class.isAssignableFrom(attribute.getClass())) {
        log.warn(format("Session attribute [%s] of class [%s] is not " + "serializable.", attribute,
                attribute.getClass()));
    }
    final Closer closer = Closer.create();
    try {
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(ByteStreams.nullOutputStream()));
        out.writeObject(attribute);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:li.klass.fhem.util.ApplicationProperties.java

void load(URL url) throws Exception {
    Closer closer = Closer.create();
    InputStream stream = url.openStream();
    closer.register(stream);//from   ww w  . j a va 2s.  c  o m
    try {
        properties.load(stream);
    } catch (IOException e) {
        Log.e(TAG, "error while loading url", e);
    } finally {
        closer.close();
    }
}

From source file:biz.paluch.maven.configurator.AbstractConfigureMojo.java

/**
 * @return Project properties and propertySources.
 * @throws IOException/*from www .j  a v  a  2  s  .  co m*/
 */
private Properties getProperties() throws IOException {

    Properties properties = new Properties();
    properties.putAll(System.getProperties());
    properties.putAll(project.getProperties());

    if (propertySources != null) {

        Closer closer = Closer.create();
        try {
            for (String propertySource : propertySources) {
                loadPropertySource(properties, closer, propertySource);
            }
        } finally {
            closer.close();
        }
    }
    return properties;
}

From source file:net.derquinse.bocas.jersey.client.BocasClient.java

private MemoryByteSource load(InputStream is) throws IOException {
    Closer closer = Closer.create();
    try {/*from  w  ww  .  j a  va 2  s  .  c om*/
        return loader.load(closer.register(is));
    } finally {
        closer.close();
    }
}

From source file:fr.techcode.downloadix.validation.FileValidator.java

/**
 * Validate if the file isn't corrupt//ww w.jav  a  2 s  . co m
 *
 * @param file File to check
 * @return Valid or Corrupt
 */
@Override
public boolean isValid(File file) {
    // Check if the file exist
    if (!file.exists())
        return false;

    // Get bytes from a file
    byte[] datas = null;
    Closer closer = Closer.create();
    try {
        try {
            FileInputStream stream = closer.register(new FileInputStream(file));
            datas = ByteStreams.toByteArray(stream);
        } catch (Throwable throwable) {
            throw closer.rethrow(throwable);
        } finally {
            closer.close();
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return datas != null && algorithm.hashBytes(datas).toString().equalsIgnoreCase(hash);
}

From source file:com.jive.myco.seyren.core.service.notification.IrcCatNotificationService.java

private void sendMessage(String ircCatHost, int ircCatPort, String message, String channel) throws IOException {
    Socket socket = new Socket(ircCatHost, ircCatPort);
    Closer closer = Closer.create();
    try {//from w  w  w . ja  v a 2  s  . c  om
        Writer out = closer.register(new OutputStreamWriter(socket.getOutputStream()));
        out.write(format("%s %s\n", channel, message));
        out.flush();
    } catch (IOException e) {
        socket.close();
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.gradle.caching.internal.LocalDirectoryBuildCache.java

@Override
public boolean load(final BuildCacheKey key, final BuildCacheEntryReader reader) throws BuildCacheException {
    return persistentCache.useCache("load build cache entry", new Factory<Boolean>() {
        @Override/*from w w  w  .  j a  va2 s. c om*/
        public Boolean create() {
            File file = getFile(key.getHashCode());
            if (file.isFile()) {
                try {
                    Closer closer = Closer.create();
                    FileInputStream stream = closer.register(new FileInputStream(file));
                    try {
                        reader.readFrom(stream);
                        return true;
                    } finally {
                        closer.close();
                    }
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return false;
        }
    });
}

From source file:org.gradle.caching.internal.controller.service.StoreTarget.java

@Override
public void writeTo(OutputStream output) throws IOException {
    Closer closer = Closer.create();
    closer.register(output);/*from   w w w  . jav  a 2 s .  c o  m*/
    try {
        if (stored) {
            throw new IllegalStateException("Build cache entry has already been stored");
        }
        stored = true;
        Files.asByteSource(file).copyTo(output);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.gradle.caching.internal.LocalDirectoryBuildCacheService.java

@Override
public boolean load(final BuildCacheKey key, final BuildCacheEntryReader reader) throws BuildCacheException {
    return persistentCache.useCache(new Factory<Boolean>() {
        @Override/*from   ww w  . j  av a 2 s.  co  m*/
        public Boolean create() {
            File file = getFile(key.getHashCode());
            if (file.isFile()) {
                try {
                    Closer closer = Closer.create();
                    FileInputStream stream = closer.register(new FileInputStream(file));
                    try {
                        reader.readFrom(stream);
                        return true;
                    } finally {
                        closer.close();
                    }
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return false;
        }
    });
}