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:org.apache.mahout.utils.vectors.arff.Driver.java

protected static void writeFile(String outDir, File file, long maxDocs, ARFFModel arffModel, File dictOut,
        String delimiter, boolean jsonDictonary) throws IOException {
    log.info("Converting File: {}", file);
    ARFFModel model = new MapBackedARFFModel(arffModel.getWords(), arffModel.getWordCount() + 1,
            arffModel.getNominalMap());/*  w ww .  j  ava  2  s  .  c  o  m*/
    Iterable<Vector> iteratable = new ARFFVectorIterable(file, model);
    String outFile = outDir + '/' + file.getName() + ".mvc";

    VectorWriter vectorWriter = getSeqFileWriter(outFile);
    try {
        long numDocs = vectorWriter.write(iteratable, maxDocs);
        writeLabelBindings(dictOut, model, delimiter, jsonDictonary);
        log.info("Wrote: {} vectors", numDocs);
    } finally {
        Closeables.close(vectorWriter, false);
    }
}

From source file:com.opentable.db.postgres.embedded.EmbeddedPostgres.java

private Thread newCloserThread() {
    final Thread closeThread = new Thread(new Runnable() {
        @Override//from  w w  w . ja va  2 s. c  o  m
        public void run() {
            try {
                Closeables.close(EmbeddedPostgres.this, true);
            } catch (IOException ex) {
                LOG.error("Unexpected IOException from Closeables.close", ex);
            }
        }
    });
    closeThread.setName("postgres-" + instanceId + "-closer");
    return closeThread;
}

From source file:org.jclouds.examples.rackspace.DeleteAll.java

private void deleteCloudDNS() throws IOException, TimeoutException {
    System.out.format("Delete DNS Domains%n");

    CloudDNSApi cloudDNSApi = ContextBuilder
            .newBuilder(System.getProperty("provider.cdns", "rackspace-clouddns-us"))
            .credentials(username, apiKey).buildApi(CloudDNSApi.class);

    try {/*w w w.j  a v a 2  s. com*/
        Set<Domain> allDomains = cloudDNSApi.getDomainApi().list().concat().toSet();
        Iterable<Domain> topLevelDomains = Iterables.filter(allDomains, IS_DOMAIN);
        Iterable<Integer> topLevelDomainIds = Iterables.transform(topLevelDomains, GET_DOMAIN_ID);

        if (!allDomains.isEmpty()) {
            awaitComplete(cloudDNSApi, cloudDNSApi.getDomainApi().delete(topLevelDomainIds, true));
            System.out.format("  Deleted %s domains%n", Iterables.size(topLevelDomainIds));
        }
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

    Closeables.close(cloudDNSApi, true);
}

From source file:org.apache.jackrabbit.oak.plugins.blob.MarkSweepGarbageCollector.java

/**
 * Mark and sweep. Main entry method for GC.
 *
 * @param markOnly whether to mark only//from  w  w  w.j a va  2s.co m
 * @throws Exception the exception
 */
protected void markAndSweep(boolean markOnly) throws Exception {
    boolean threw = true;
    GarbageCollectorFileState fs = new GarbageCollectorFileState(root);
    try {
        Stopwatch sw = Stopwatch.createStarted();
        LOG.info("Starting Blob garbage collection with markOnly [{}]", markOnly);

        long markStart = System.currentTimeMillis();
        mark(fs);
        if (!markOnly) {
            long deleteCount = sweep(fs, markStart);
            threw = false;

            long maxTime = getLastMaxModifiedTime(markStart) > 0 ? getLastMaxModifiedTime(markStart)
                    : markStart;
            LOG.info(
                    "Blob garbage collection completed in {}. Number of blobs deleted [{}] with max modification time of [{}]",
                    sw.toString(), deleteCount, timestampToString(maxTime));
        }
    } catch (Exception e) {
        LOG.error("Blob garbage collection error", e);
        throw e;
    } finally {
        if (!LOG.isTraceEnabled()) {
            Closeables.close(fs, threw);
        }
    }
}

From source file:DisplayClustering.java

protected static void writeSampleData(Path output) throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(output.toUri(), conf);
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, output, Text.class, VectorWritable.class);
    try {//w w w.  j  a  va2 s. com
        int i = 0;
        for (VectorWritable vw : SAMPLE_DATA) {
            writer.append(new Text("sample_" + i++), vw);
        }
    } finally {
        Closeables.close(writer, false);
    }
}

From source file:com.wandoulabs.jodis.RoundRobinJedisPool.java

@Override
public void close() {
    try {/*from  w w  w.  j  a  v a2 s .  c  om*/
        Closeables.close(watcher, true);
    } catch (IOException e) {
        LOG.fatal("IOException should not have been thrown", e);
    }
    if (closeCurator) {
        curatorClient.close();
    }
    List<PooledObject> pools = this.pools;
    this.pools = ImmutableList.of();
    for (PooledObject pool : pools) {
        pool.pool.close();
    }
}

From source file:com.android.dvlib.DeviceSchema.java

/**
 * Helper method that returns a validator for a specific version of the XSD.
 *
 * @param version Between 1 and {@link #NS_LATEST_VERSION}, included.
 * @return A {@link Schema} validator or null.
 *///from w  w  w  .java  2s.  c  o m
@Nullable
public static Schema getSchema(int version) throws SAXException {
    InputStream xsdStream = null;
    try {
        xsdStream = getXsdStream(version);
        if (xsdStream == null) {
            return null;
        }
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsdStream));
        return schema;
    } finally {
        try {
            if (xsdStream != null)
                Closeables.close(xsdStream, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }
}

From source file:org.apache.mahout.cf.taste.example.email.MailToPrefsDriver.java

private static List<Path> createDictionaryChunks(Path inputPath, Path dictionaryPathBase, String name,
        Configuration baseConf, int chunkSizeInMegabytes, int[] maxTermDimension) throws IOException {
    List<Path> chunkPaths = Lists.newArrayList();

    Configuration conf = new Configuration(baseConf);

    FileSystem fs = FileSystem.get(inputPath.toUri(), conf);

    long chunkSizeLimit = chunkSizeInMegabytes * 1024L * 1024L;
    int chunkIndex = 0;
    Path chunkPath = new Path(dictionaryPathBase, name + chunkIndex);
    chunkPaths.add(chunkPath);/*from w  ww . ja  v  a  2  s.c o  m*/

    SequenceFile.Writer dictWriter = new SequenceFile.Writer(fs, conf, chunkPath, Text.class,
            IntWritable.class);

    try {
        long currentChunkSize = 0;
        Path filesPattern = new Path(inputPath, OUTPUT_FILES_PATTERN);
        int i = 1; //start at 1, since a miss in the OpenObjectIntHashMap returns a 0
        for (Pair<Writable, Writable> record : new SequenceFileDirIterable<Writable, Writable>(filesPattern,
                PathType.GLOB, null, null, true, conf)) {
            if (currentChunkSize > chunkSizeLimit) {
                Closeables.close(dictWriter, false);
                chunkIndex++;

                chunkPath = new Path(dictionaryPathBase, name + chunkIndex);
                chunkPaths.add(chunkPath);

                dictWriter = new SequenceFile.Writer(fs, conf, chunkPath, Text.class, IntWritable.class);
                currentChunkSize = 0;
            }

            Writable key = record.getFirst();
            int fieldSize = DICTIONARY_BYTE_OVERHEAD + key.toString().length() * 2 + Integer.SIZE / 8;
            currentChunkSize += fieldSize;
            dictWriter.append(key, new IntWritable(i++));
        }
        maxTermDimension[0] = i;
    } finally {
        Closeables.close(dictWriter, false);
    }

    return chunkPaths;
}

From source file:dodola.anole.lib.FileUtils.java

/**
 * Reads a portion of a file to memory.// w  ww  .  j a v a 2  s. c o m
 *
 * @param file   the file to read data from
 * @param start  the offset in the file to start reading
 * @param length the number of bytes to read
 * @return the bytes read
 * @throws Exception failed to read the file
 */

public static byte[] readSegment(File file, long start, int length) throws Exception {
    Preconditions.checkArgument(start >= 0, "start < 0");
    Preconditions.checkArgument(length >= 0, "length < 0");

    byte data[];
    boolean threw = true;
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    try {
        raf.seek(start);

        data = new byte[length];
        int tot = 0;
        while (tot < length) {
            int r = raf.read(data, tot, length - tot);
            if (r < 0) {
                throw new EOFException();
            }

            tot += r;
        }

        threw = false;
    } finally {
        Closeables.close(raf, threw);
    }

    return data;
}

From source file:com.google.caliper.runner.worker.Worker.java

/**
 * Write a line of data to the worker process over the socket.
 *
 * <p>N.B. Writing data via {@link #sendMessage(Serializable)} is only valid once the underlying
 * socket has been opened. This should be fine assuming that socket writes are only in response to
 * socket reads (which is currently the case), so there is no way that a write could happen prior
 * to the socket being opened./*from   w ww. java 2s  . c  o  m*/
 */
public void sendMessage(Serializable message) throws IOException {
    checkState(isRunning(), "Cannot read items from a %s StreamService", state());
    checkState(socketWriter != null, "Attempted to write to the socket before it was opened.");
    try {
        socketWriter.write(message);
    } catch (IOException e) {
        Closeables.close(socketWriter, true);
        notifyFailed(e);
        throw e;
    }
}