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:com.fullcontact.sstable.index.SSTableIndexIndex.java

/**
 * Create and write an index index based on the input Cassandra Index.db file. Read the Index.db and generate chunks
 * (splits) based on the configured chunk size.
 *
 * @param fileSystem Hadoop file system.
 * @param sstablePath SSTable Index.db.//ww w  .j  a va2 s . c o m
 * @throws IOException
 */
public static void writeIndex(final FileSystem fileSystem, final Path sstablePath) throws IOException {

    final Configuration configuration = fileSystem.getConf();

    final long splitSize = configuration.getLong(HadoopSSTableConstants.HADOOP_SSTABLE_SPLIT_MB,
            HadoopSSTableConstants.DEFAULT_SPLIT_MB) * 1024 * 1024;

    final Closer closer = Closer.create();

    final Path outputPath = sstablePath.suffix(SSTABLE_INDEX_SUFFIX);
    final Path inProgressOutputPath = sstablePath.suffix(SSTABLE_INDEX_IN_PROGRESS_SUFFIX);

    boolean success = false;
    try {
        final FSDataOutputStream os = closer.register(fileSystem.create(inProgressOutputPath));

        final TLongArrayList splitOffsets = new TLongArrayList();
        long currentStart = 0;
        long currentEnd = 0;
        final IndexOffsetScanner index = new IndexOffsetScanner(sstablePath, fileSystem);

        while (index.hasNext()) {
            // NOTE: This does not give an exact size of this split in bytes but a rough estimate.
            // This should be good enough since it's only used for sorting splits by size in hadoop land.
            while (currentEnd - currentStart < splitSize && index.hasNext()) {
                currentEnd = index.next();
                splitOffsets.add(currentEnd);
            }

            // Record the split
            final long[] offsets = splitOffsets.toArray();
            os.writeLong(offsets[0]); // Start
            os.writeLong(offsets[offsets.length - 1]); // End

            // Clear the offsets
            splitOffsets.clear();

            if (index.hasNext()) {
                currentStart = index.next();
                currentEnd = currentStart;
                splitOffsets.add(currentStart);
            }
        }

        success = true;
    } finally {
        closer.close();

        if (!success) {
            fileSystem.delete(inProgressOutputPath, false);
        } else {
            fileSystem.rename(inProgressOutputPath, outputPath);
        }
    }
}

From source file:zipkin.storage.cassandra3.DefaultSessionFactory.java

/**
 * Creates a session and ensures schema if configured. Closes the cluster and session if any
 * exception occurred./*  w w w  . j av a  2  s . co m*/
 */
@Override
public Session create(Cassandra3Storage cassandra) {
    Closer closer = Closer.create();
    try {
        Cluster cluster = closer.register(buildCluster(cassandra));
        cluster.register(new QueryLogger.Builder().build());
        Session session;
        if (cassandra.ensureSchema) {
            session = closer.register(cluster.connect());
            Schema.ensureExists(cassandra.keyspace, session);
            session.execute("USE " + cassandra.keyspace);
        } else {
            session = cluster.connect(cassandra.keyspace);
        }

        initializeUDTs(session);

        return session;
    } catch (RuntimeException e) {
        try {
            closer.close();
        } catch (IOException ignored) {
        }
        throw e;
    }
}

From source file:com.android.tools.lint.Reporter.java

/** Returns a URL to a local copy of the given resource, or null. There is
 * no filename conflict resolution. */
protected String addLocalResources(URL url) throws IOException {
    // Attempt to make local copy
    File resourceDir = computeResourceDir();
    if (resourceDir != null) {
        String base = url.getFile();
        base = base.substring(base.lastIndexOf('/') + 1);
        mNameToFile.put(base, new File(url.toExternalForm()));

        File target = new File(resourceDir, base);
        Closer closer = Closer.create();
        try {/*from   w  w  w .  j  a v  a2  s .  c  o  m*/
            FileOutputStream output = closer.register(new FileOutputStream(target));
            InputStream input = closer.register(url.openStream());
            ByteStreams.copy(input, output);
        } catch (Throwable e) {
            closer.rethrow(e);
        } finally {
            closer.close();
        }
        return resourceDir.getName() + '/' + encodeUrl(base);
    }
    return null;
}

From source file:gobblin.metastore.FsStateStore.java

/**
 * See {@link StateStore#put(String, String, T)}.
 *
 * <p>//from  w  ww . j  av a2  s.c o  m
 *   This implementation does not support putting the state object into an existing store as
 *   append is to be supported by the Hadoop SequenceFile (HADOOP-7139).
 * </p>
 */
@Override
public void put(String storeName, String tableName, T state) throws IOException {
    String tmpTableName = this.useTmpFileForPut ? TMP_FILE_PREFIX + tableName : tableName;
    Path tmpTablePath = new Path(new Path(this.storeRootDir, storeName), tmpTableName);

    if (!this.fs.exists(tmpTablePath) && !create(storeName, tmpTableName)) {
        throw new IOException("Failed to create a state file for table " + tmpTableName);
    }

    Closer closer = Closer.create();
    try {
        @SuppressWarnings("deprecation")
        SequenceFile.Writer writer = closer.register(SequenceFile.createWriter(this.fs, this.conf, tmpTablePath,
                Text.class, this.stateClass, SequenceFile.CompressionType.BLOCK, new DefaultCodec()));
        writer.append(new Text(Strings.nullToEmpty(state.getId())), state);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }

    if (this.useTmpFileForPut) {
        Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName);
        HadoopUtils.renamePath(this.fs, tmpTablePath, tablePath);
    }
}

From source file:com.sk89q.worldguard.six2five.RegionsProcessor.java

public boolean downgrade(File file) throws IOException {
    Yaml yaml = new Yaml();
    Map<Object, Object> data;

    Closer closer = Closer.create();
    try {/*from ww  w.ja  va 2s. c o  m*/
        InputStream is = closer.register(new FileInputStream(file));
        BufferedInputStream bis = closer.register(new BufferedInputStream(is));
        data = (Map<Object, Object>) yaml.load(bis);

        log.info("Converting UUIDs to names...");
        downgrade(data);
    } catch (FileNotFoundException e) {
        log.log(Level.WARNING, "The file '" + file.getAbsolutePath() + "' does not exist");
        return false;
    } catch (IOException e) {
        log.log(Level.WARNING, "Failed to open file for reading", e);
        return false;
    } finally {
        closer.close();
    }

    closer = Closer.create();
    try {
        File backupFile = new File(file.getParentFile(), Files.getNameWithoutExtension(file.getName()) + "-"
                + System.currentTimeMillis() + "." + Files.getFileExtension(file.getName()) + ".backup");

        if (!file.renameTo(backupFile)) {
            throw new IOException("Failed to rename old file to " + backupFile.getAbsolutePath());
        }

        log.info("Moved regions file to the backup file at " + backupFile.getAbsolutePath());

        FileWriter fw = closer.register(new FileWriter(file));
        yaml.dump(data, fw);
    } catch (IOException e) {
        log.log(Level.WARNING, "Failed to open file for writing", e);
        return false;
    } finally {
        closer.close();
    }

    log.info("UUID -> name conversion is complete");

    return true;
}

From source file:se.sics.caracaldb.store.SizeScan.java

@Override
public StorageResponse execute(Persistence store) throws IOException {
    long size = 0;
    long keys = 0;

    Closer closer = Closer.create();
    try {/*from w w w .  j a  v  a2  s . com*/
        byte[] begin = range.begin.getArray();
        for (StoreIterator it = closer.register(store.iterator(begin)); it.hasNext(); it.next()) {
            byte[] key = it.peekKey();
            if (range.contains(key)) {
                byte[] value = it.peekRaw();
                keys++;
                size += key.length;
                size += value.length;
            } else {
                //special case (a,b) and key is a
                if (Key.compare(begin, key) != 0) {
                    break; // reached end of range
                }
            }
        }

    } catch (Throwable e) {
        closer.rethrow(e);
    } finally {
        closer.close();
    }
    return new SizeResp(this, new Diff(size, keys, true));
}

From source file:com.taobao.android.builder.tools.jarmerge.JarMergerWithOverride.java

private void addFolder(@NonNull File folder, @NonNull String path) throws IOException, ZipAbortException {
    logger.verbose("addFolder(%1$s, %2$s)", folder, path);
    File[] files = folder.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isFile()) {
                String entryPath = path + file.getName();
                if (filter == null || filter.checkEntry(entryPath)) {
                    logger.verbose("addFolder(%1$s, %2$s): entry %3$s", folder, path, entryPath);
                    duplicates.put(path + file.getName(), folder.getAbsolutePath());
                    if (duplicates.get(path + file.getName()).size() > 1) {
                        logger.info("[Duplicated]" + path + file.getName() + ":" + folder.getAbsolutePath()
                                + ":" + duplicates.get(path + file.getName()));
                        continue;
                    }//from  www.  j  a va  2  s  . c o m

                    // new entry
                    jarOutputStream.putNextEntry(new JarEntry(entryPath));

                    // put the file content
                    Closer localCloser = Closer.create();
                    try {
                        FileInputStream fis = localCloser.register(new FileInputStream(file));
                        int count;
                        while ((count = fis.read(buffer)) != -1) {
                            jarOutputStream.write(buffer, 0, count);
                        }
                    } finally {
                        localCloser.close();
                    }

                    // close the entry
                    jarOutputStream.closeEntry();
                }
            } else if (file.isDirectory()) {
                addFolder(file, path + file.getName() + "/");
            }
        }
    }
}

From source file:ru.runa.af.web.system.TaskHandlerClassesInformation.java

private static void init() {
    String deploymentDirPath = IoCommons.getDeploymentDirPath();
    String earFilePath = deploymentDirPath + "/" + SystemProperties.getEARFileName();
    Closer closer = Closer.create();
    try {//from  ww  w  .j a  v a 2s .  c o  m
        ZipInputStream earInputStream = closer.register(new ZipInputStream(new FileInputStream(earFilePath)));
        ZipEntry entry;
        while ((entry = earInputStream.getNextEntry()) != null) {
            if (entry.getName().endsWith(".jar")) {
                searchInJar(entry.getName(), new JarInputStream(earInputStream));
            }
        }
        if (IoCommons.getAppServer() == AppServer.JBOSS4) {
            File deploymentDirectory = new File(deploymentDirPath);
            log.debug("Searching in deployment directory: " + deploymentDirectory);
            for (File file : IoCommons.getJarFiles(deploymentDirectory)) {
                JarInputStream jarInputStream = closer.register(new JarInputStream(new FileInputStream(file)));
                searchInJar(file.getName(), jarInputStream);
            }
        }
        File extensionDirectory = new File(IoCommons.getExtensionDirPath());
        if (extensionDirectory.exists() && extensionDirectory.isDirectory()) {
            log.debug("Searching in extension directory: " + extensionDirectory);
            for (File file : IoCommons.getJarFiles(extensionDirectory)) {
                JarInputStream jarInputStream = closer.register(new JarInputStream(new FileInputStream(file)));
                searchInJar(file.getName(), jarInputStream);
            }
        } else {
            log.debug("No extension directory found: " + extensionDirectory);
        }
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
            log.warn(e);
        }
    }
}

From source file:com.android.builder.internal.packaging.DexIncrementalRenameManager.java

/**
 * Writes incremental state./*from  ww w . ja  v a  2  s.c  om*/
 *
 * @throws IOException failed to write state
 */
private void writeState() throws IOException {
    File stateFile = new File(mIncrementalDir, STATE_FILE);

    Properties props = new Properties();
    int currIdx = 0;
    for (BiMap.Entry<RelativeFile, String> entry : mNameMap.entrySet()) {
        props.put(BASE_KEY_PREFIX + currIdx, entry.getKey().getBase().getPath());
        props.put(FILE_KEY_PREFIX + currIdx, entry.getKey().getFile().getPath());
        props.put(RENAMED_KEY_PREFIX + currIdx, entry.getValue());
        currIdx++;
    }

    Closer closer = Closer.create();
    try {
        props.store(closer.register(new FileWriter(stateFile)), null);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:alluxio.master.RecomputeCommand.java

@Override
public void run() {
    try {/*from  w ww  .  ja va2  s. c  o  m*/
        LOG.info("Exec {} output to {}", mCommand, mFilePath);
        Process p = java.lang.Runtime.getRuntime().exec(mCommand);
        String line;
        Closer closer = Closer.create();
        try {
            BufferedReader bri = closer.register(new BufferedReader(new InputStreamReader(p.getInputStream())));
            BufferedReader bre = closer.register(new BufferedReader(new InputStreamReader(p.getErrorStream())));
            File file = new File(mFilePath);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = closer.register(new BufferedWriter(fw));
            while ((line = bri.readLine()) != null) {
                bw.write(line + "\n");
            }
            while ((line = bre.readLine()) != null) {
                bw.write(line + "\n");
            }
            bw.flush();
        } finally {
            closer.close();
        }

        p.waitFor();
        LOG.info("Exec {} output to {} done.", mCommand, mFilePath);
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
    }
}