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:eu.interedition.text.repository.JdbcStore.java

public <R> R add(long id, TextWriter<R> writer) {

    try {/* w w  w. j a  v a 2s.  com*/
        if (insertText == null) {
            insertText = connection
                    .prepareStatement("insert into interedition_text (text_content, id) values (?, ?)");
        }
        final Clob textClob = connection.createClob();
        R result = null;
        Writer textWriter = null;
        try {
            result = writer.write(textWriter = textClob.setCharacterStream(1));
        } finally {
            Closeables.close(textWriter, false);
        }
        insertText.setClob(1, textClob);
        insertText.setLong(2, id);
        insertText.executeUpdate();

        textClob.free();

        txLog.textsAdded(id);
        return result;
    } catch (SQLException e) {
        throw Throwables.propagate(e);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:eu.interedition.web.index.IndexController.java

@Override
public void destroy() throws Exception {
    Closeables.close(this.indexReader, false);
    Closeables.close(this.indexWriter, false);
    Closeables.close(this.directory, false);
}

From source file:io.druid.query.aggregation.AggregationTestHelper.java

public void createIndex(InputStream inputDataStream, String parserJson, String aggregators, File outDir,
        long minTimestamp, QueryGranularity gran, int maxRowCount) throws Exception {
    try {/*w ww  . j  ava 2  s .c  om*/
        StringInputRowParser parser = mapper.readValue(parserJson, StringInputRowParser.class);

        LineIterator iter = IOUtils.lineIterator(inputDataStream, "UTF-8");
        List<AggregatorFactory> aggregatorSpecs = mapper.readValue(aggregators,
                new TypeReference<List<AggregatorFactory>>() {
                });

        createIndex(iter, parser, aggregatorSpecs.toArray(new AggregatorFactory[0]), outDir, minTimestamp, gran,
                true, maxRowCount);
    } finally {
        Closeables.close(inputDataStream, true);
    }
}

From source file:org.nll.hbase.ui.util.HbaseUtil.java

public static void getResultScann(String tableName) throws Exception {
    Scan scan = new Scan();
    ResultScanner rs = null;//  www.  j  ava  2 s  .c o  m
    HTableInterface table = getTable(defaultConnection, tableName);
    try {
        rs = table.getScanner(scan);
        for (Result r : rs) {
            for (KeyValue kv : r.list()) {
                System.out.println("row:" + Bytes.toString(kv.getRow()));
                System.out.println("family:" + Bytes.toString(kv.getFamily()));
                System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
                System.out.println("value:" + Bytes.toString(kv.getValue()));
                System.out.println("timestamp:" + kv.getTimestamp());
                System.out.println("-------------------------------------------");
            }
        }
    } finally {
        Closeables.close(rs, true);
        Closeables.close(table, true);
    }
}

From source file:org.apache.mahout.classifier.df.data.Utils.java

private static void writeDataToFile(String[] sData, Path path) throws IOException {
    BufferedWriter output = null;
    try {//  www  .  ja v  a2 s  . c o  m
        output = Files.newWriter(new File(path.toString()), Charsets.UTF_8);
        for (String line : sData) {
            output.write(line);
            output.write('\n');
        }
    } finally {
        Closeables.close(output, false);
    }

}

From source file:com.replaymod.replaystudio.replay.ZipReplayFile.java

@Override
public void remove(String entry) throws IOException {
    saveInputFile();//from   w ww  . j a v  a 2s  .  co  m
    Closeables.close(outputStreams.remove(entry), true);
    File file = changedEntries.remove(entry);
    if (file != null && file.exists()) {
        delete(file);
    }
    removedEntries.add(entry);
    File removedFile = new File(removedFiles, entry);
    createParentDirs(removedFile);
    touch(removedFile);
}

From source file:com.ibm.opensirf.jaxrs.JCloudsApi.java

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

From source file:ezbake.security.impl.cache.EzbakeDistributedCache.java

/**
 * @param key - key of the associated value in the (key,value) pair
 * @return - the value associated with specified key.
 *///  ww  w .j ava 2  s  .co m
@Override
public Value get(Key key, Class<?> clazz) {
    Value token = null;

    SecretKey symKey = fetchSymmetricKey();

    Lease l = null;
    ByteArrayInputStream bis = null;
    ObjectInput in = null;
    try {
        l = mutex.acquire();
        byte[] cipherValue = jedis.get(key.getBytes());
        mutex.returnLease(l);

        if (cipherValue != null) {
            token = (Value) clazz.newInstance();
            byte[] value = aesCrypto.decrypt(symKey, cipherValue);

            bis = new ByteArrayInputStream(value);
            in = new ObjectInputStream(bis);
            Object o = in.readObject();
            try {
                token = (Value) o;
            } catch (ClassCastException e) {
                log.error("Retrieved something for: {}, but couldn't deserialize it properly", key);
            }
        }
    } catch (Exception e) {
        log.error("Caught exception fetching: {}", key, e);
    } finally {
        try {
            Closeables.close(bis, true);
        } catch (IOException e) {
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    return token;
}

From source file:org.jclouds.examples.rackspace.cloudservers.CloudServersPublish.java

/**
 * Always close your service when you're done with it.
 *///from  www.  ja  v a2 s  . c  om
public void close() throws IOException {
    Closeables.close(computeService.getContext(), true);
}

From source file:com.android.build.gradle.internal.SdkHandler.java

private void findLocation(@NonNull Project project) {
    if (sTestSdkFolder != null) {
        sdkFolder = sTestSdkFolder;/*  w  ww  .j  ava2 s.  c o m*/
        return;
    }

    File rootDir = project.getRootDir();
    File localProperties = new File(rootDir, FN_LOCAL_PROPERTIES);
    Properties properties = new Properties();

    if (localProperties.isFile()) {
        InputStreamReader reader = null;
        try {
            //noinspection IOResourceOpenedButNotSafelyClosed
            FileInputStream fis = new FileInputStream(localProperties);
            reader = new InputStreamReader(fis, Charsets.UTF_8);
            properties.load(reader);
        } catch (FileNotFoundException ignored) {
            // ignore since we check up front and we don't want to fail on it anyway
            // in case there's an env var.
        } catch (IOException e) {
            throw new RuntimeException(String.format("Unable to read %1$s.", localProperties.getAbsolutePath()),
                    e);
        } finally {
            try {
                Closeables.close(reader, true /* swallowIOException */);
            } catch (IOException e) {
                // ignore.
            }
        }
    }

    findSdkLocation(properties, rootDir);
    findNdkLocation(properties);
}