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

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

Introduction

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

Prototype


public <C extends Closeable> C register(@Nullable C closeable) 

Source Link

Document

Registers the given closeable to be closed when this Closer is #close closed .

Usage

From source file:com.skcraft.launcher.install.ZipExtract.java

@Override
public void run() {
    Closer closer = Closer.create();

    try {//from  w w w .  ja  v  a  2  s.  com
        InputStream is = closer.register(source.openBufferedStream());
        ZipInputStream zis = closer.register(new ZipInputStream(is));
        ZipEntry entry;

        destination.getParentFile().mkdirs();

        while ((entry = zis.getNextEntry()) != null) {
            if (matches(entry)) {
                File file = new File(getDestination(), entry.getName());
                writeEntry(zis, file);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.grouplens.lenskit.cli.TrainModel.java

@Override
public void execute() throws IOException, RecommenderBuildException {
    LenskitConfiguration dataConfig = input.getConfiguration();
    LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder();
    for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
        builder.addConfiguration(config);
    }/*w ww .  j  a  v a  2 s.co  m*/
    builder.addConfiguration(dataConfig, ModelDisposition.EXCLUDED);

    Stopwatch timer = Stopwatch.createStarted();
    LenskitRecommenderEngine engine = builder.build();
    timer.stop();
    logger.info("built model in {}", timer);
    File output = getOutputFile();
    logger.info("writing model to {}", output);
    Closer closer = Closer.create();
    try {
        OutputStream stream = closer.register(new FileOutputStream(output));
        if (LKFileUtils.isCompressed(output)) {
            stream = closer.register(new GZIPOutputStream(stream));
        }
        engine.write(stream);
    } catch (Throwable th) {
        throw closer.rethrow(th);
    } finally {
        closer.close();
    }
}

From source file:com.googlecode.jmxtrans.model.output.Slf4JOutputWriter.java

private void logValue(Server server, Query query, List<String> typeNames, Result result,
        Map.Entry<String, Object> values) throws IOException {
    Object value = values.getValue();

    if (value != null && isNumeric(value)) {
        Closer closer = Closer.create();
        try {/*from  www. j av a2 s.  c o  m*/
            closer.register(MDC.putCloseable("server", computeAlias(server)));
            closer.register(MDC.putCloseable("metric",
                    KeyUtils.getKeyString(server, query, result, values, typeNames, null)));
            closer.register(MDC.putCloseable("value", value.toString()));
            if (result.getKeyAlias() != null) {
                closer.register(MDC.putCloseable("resultAlias", result.getKeyAlias()));
            }
            closer.register(MDC.putCloseable("attributeName", result.getAttributeName()));
            closer.register(MDC.putCloseable("key", values.getKey()));
            closer.register(MDC.putCloseable("epoch", valueOf(result.getEpoch())));

            logger.info("");
        } catch (Throwable t) {
            throw closer.rethrow(t);
        } finally {
            closer.close();
        }
    }
}

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 a  v  a 2 s .  co 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:net.derquinse.common.io.BytesTransformer.java

/** Transforms a stream of bytes. */
public void transform(InputStream input, ByteSink output) throws IOException {
    checkInput(input);/*from   www.jav  a2  s. c  o m*/
    checkOutput(output);
    final Closer closer = Closer.create();
    try {
        transform(input, closer.register(output.openStream()));
    } finally {
        closer.close();
    }
}

From source file:org.apache.jackrabbit.oak.upgrade.cli.blob.FileDataStoreFactory.java

@Override
public BlobStore create(Closer closer) {
    OakFileDataStore delegate = new OakFileDataStore();
    delegate.setPath(directory);/*from ww  w .  j  av  a 2  s  . c o m*/
    delegate.init(null);
    closer.register(asCloseable(delegate));
    return new DataStoreBlobStore(delegate);
}

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

/**
 * Validate if the file isn't corrupt/*from  www  . j av a  2s  .com*/
 *
 * @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:eu.interedition.text.index.IndexServiceBase.java

@Override
protected void startUp() throws Exception {
    if (LOG.isLoggable(Level.INFO)) {
        LOG.log(Level.INFO, "Starting {0}", getClass().getName());
    }// ww w  . j  a  va  2s  .  c o m
    final Closer closer = Closer.create();
    try {
        closer.register(writer()).commit();
    } finally {
        closer.close();
    }
}

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

private long measureSerializedSize(final Object attribute) throws IOException {
    final Closer closer = Closer.create();
    try {/*w  w w . j  a va  2  s  .  co  m*/
        final CountingOutputStream countingStream = closer
                .register(new CountingOutputStream(ByteStreams.nullOutputStream()));
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(countingStream));
        out.writeObject(attribute);
        return countingStream.getCount();
    } finally {
        closer.close();
    }
}

From source file:com.googlecode.jmxtrans.model.output.support.HttpOutputWriter.java

private void consumeInputStreams(HttpURLConnection httpURLConnection) throws IOException {
    Closer closer = Closer.create();
    try {//from w w  w. j  a  va2 s . co  m
        InputStream in = closer.register(httpURLConnection.getInputStream());
        InputStream err = closer.register(httpURLConnection.getErrorStream());
        copy(in, nullOutputStream());
        if (err != null)
            copy(err, nullOutputStream());
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}