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.kitesdk.data.spi.Schemas.java

public static Schema fromJSON(String name, FileSystem fs, Path location) throws IOException {
    InputStream in = null;/* w ww  .  ja v  a2  s  .  c  om*/
    boolean threw = true;

    try {
        in = fs.open(location);
        Schema schema = fromJSON(name, in);
        threw = false;
        return schema;
    } finally {
        Closeables.close(in, threw);
    }
}

From source file:org.codelibs.elasticsearch.taste.similarity.precompute.MultithreadedBatchItemSimilarities.java

@Override
public int computeItemSimilarities(final int degreeOfParallelism, final int maxDurationInHours,
        final SimilarItemsWriter writer) throws IOException {

    final ExecutorService executorService = Executors.newFixedThreadPool(degreeOfParallelism + 1);

    Output output = null;//from   w  ww  .  ja va2s  .c om
    try {
        writer.open();

        final DataModel dataModel = getRecommender().getDataModel();

        final BlockingQueue<long[]> itemsIDsInBatches = queueItemIDsInBatches(dataModel, batchSize);
        final BlockingQueue<List<SimilarItems>> results = new LinkedBlockingQueue<List<SimilarItems>>();

        final AtomicInteger numActiveWorkers = new AtomicInteger(degreeOfParallelism);
        for (int n = 0; n < degreeOfParallelism; n++) {
            executorService.execute(new SimilarItemsWorker(n, itemsIDsInBatches, results, numActiveWorkers));
        }

        output = new Output(results, writer, numActiveWorkers);
        executorService.execute(output);

    } catch (final Exception e) {
        throw new IOException(e);
    } finally {
        executorService.shutdown();
        try {
            final boolean succeeded = executorService.awaitTermination(maxDurationInHours, TimeUnit.HOURS);
            if (!succeeded) {
                throw new RuntimeException(
                        "Unable to complete the computation in " + maxDurationInHours + " hours!");
            }
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
        Closeables.close(writer, false);
    }

    return output.getNumSimilaritiesProcessed();
}

From source file:org.apache.mahout.utils.vectors.lucene.SeqFilePrint.java

@Override
public int run(String[] strings) throws Exception {
    Configuration conf = getConf();
    FileSystem fs = FileSystem.get(conf);
    Path inputPath = new Path(this.inputSeqFile);
    BufferedWriter br = null;//from w  ww .  ja  va 2  s. c  o m

    File textOutFile = new File(this.outFile);
    Writer writer = Files.newWriter(textOutFile, Charsets.UTF_8);

    SequenceFile.Reader reader = null;
    try {
        reader = new SequenceFile.Reader(fs, inputPath, conf);
        Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
        Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf);
        while (reader.next(key, value)) {
            writer.write(key.toString());
            writer.write(value.toString());
            writer.write('\n');
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeStream(reader);
        Closeables.close(writer, false);

    }

    return 0;
}

From source file:org.apache.mahout.utils.ConcatenateVectorsJob.java

private Class<? extends Writable> getKeyClass(Path path, FileSystem fs) throws IOException {
    // this works for both part* and a directory/ with part*.
    Path pathPattern = new Path(path, "part*");
    FileStatus[] paths = fs.globStatus(pathPattern);
    Preconditions.checkArgument(paths.length > 0, path.getName() + " is a file, should be a directory");

    Path file = paths[0].getPath();
    SequenceFile.Reader reader = null;
    try {//w w  w . j a  v  a  2s  .co  m
        reader = new SequenceFile.Reader(fs, file, fs.getConf());
        return reader.getKeyClass().asSubclass(Writable.class);
    } finally {
        Closeables.close(reader, true);
    }
}

From source file:com.metamx.common.io.smoosh.FileSmoosher.java

public SmooshedWriter addWithSmooshedWriter(final String name, final long size) throws IOException {
    if (size > maxChunkSize) {
        throw new IAE("Asked to add buffers[%,d] larger than configured max[%,d]", size, maxChunkSize);
    }//www .j av  a2s .  c om
    if (currOut == null) {
        currOut = getNewCurrOut();
    }
    if (currOut.bytesLeft() < size) {
        Closeables.close(currOut, false);
        currOut = getNewCurrOut();
    }

    final int startOffset = currOut.getCurrOffset();

    return new SmooshedWriter() {
        private boolean open = true;
        private long bytesWritten = 0;

        @Override
        public int write(InputStream in) throws IOException {
            return verifySize(currOut.write(in));
        }

        @Override
        public int write(ByteBuffer in) throws IOException {
            return verifySize(currOut.write(in));
        }

        private int verifySize(int bytesWrittenInChunk) throws IOException {
            bytesWritten += bytesWrittenInChunk;

            if (bytesWritten != currOut.getCurrOffset() - startOffset) {
                throw new ISE("WTF? Perhaps there is some concurrent modification going on?");
            }
            if (bytesWritten > size) {
                throw new ISE("Wrote[%,d] bytes for something of size[%,d].  Liar!!!", bytesWritten, size);
            }

            return bytesWrittenInChunk;
        }

        @Override
        public boolean isOpen() {
            return open;
        }

        @Override
        public void close() throws IOException {
            open = false;
            internalFiles.put(name, new Metadata(currOut.getFileNum(), startOffset, currOut.getCurrOffset()));

            if (bytesWritten != currOut.getCurrOffset() - startOffset) {
                throw new ISE("WTF? Perhaps there is some concurrent modification going on?");
            }
            if (bytesWritten != size) {
                throw new IOException(String.format(
                        "Expected [%,d] bytes, only saw [%,d], potential corruption?", size, bytesWritten));
            }
        }
    };
}

From source file:com.spotify.cassandra.extra.CassandraRule.java

@Override
protected void after() {
    super.after();
    try {//from  w  ww  .  ja  v a2  s . co m
        cleanup();
        Closeables.close(session, true);
    } catch (DriverException ex) {
        logger.log(Level.WARNING, "can't drop keyspace after test", ex);
    } catch (IOException ex) {
        logger.log(Level.WARNING, "can't close session", ex);
    } finally {
        try {
            Closeables.close(cluster, true);
        } catch (IOException ex) {
            logger.log(Level.WARNING, "can't close cluster", ex);
        }
    }
}

From source file:org.grycap.gpf4med.cloud.Gpf4MedServiceController.java

@Override
public void close() throws IOException {
    Closeables.close(closer, true);
}

From source file:org.plista.kornakapi.core.training.LDATopicFactorizer.java

/**
 * gets topic posterior from lda output/* w  w w .j a v  a  2  s  .  co  m*/
 * @throws IOException
 */
private void getAllTopicPosterior() throws IOException {
    itemFeatures = new HashMap<String, Vector>();
    Reader reader = new SequenceFile.Reader(fs, new Path(this.conf.getLDADocTopicsPath()), lconf);
    IntWritable key = new IntWritable();
    VectorWritable newVal = new VectorWritable();
    while (reader.next(key, newVal)) {
        itemFeatures.put(getIndexItem(key.get()), newVal.get());
    }
    Closeables.close(reader, false);
}

From source file:org.apache.mahout.cf.taste.hadoop.als.FactorizationEvaluator.java

@Override
public int run(String[] args) throws Exception {

    addInputOption();/*from   w ww .j  av a 2  s .c om*/
    addOption("userFeatures", null, "path to the user feature matrix", true);
    addOption("itemFeatures", null, "path to the item feature matrix", true);
    addOption("usesLongIDs", null, "input contains long IDs that need to be translated");
    addOutputOption();

    Map<String, List<String>> parsedArgs = parseArguments(args);
    if (parsedArgs == null) {
        return -1;
    }

    Path errors = getTempPath("errors");

    Job predictRatings = prepareJob(getInputPath(), errors, TextInputFormat.class, PredictRatingsMapper.class,
            DoubleWritable.class, NullWritable.class, SequenceFileOutputFormat.class);

    Configuration conf = predictRatings.getConfiguration();
    conf.set(USER_FEATURES_PATH, getOption("userFeatures"));
    conf.set(ITEM_FEATURES_PATH, getOption("itemFeatures"));

    boolean usesLongIDs = Boolean.parseBoolean(getOption("usesLongIDs"));
    if (usesLongIDs) {
        conf.set(ParallelALSFactorizationJob.USES_LONG_IDS, String.valueOf(true));
    }

    boolean succeeded = predictRatings.waitForCompletion(true);
    if (!succeeded) {
        return -1;
    }

    BufferedWriter writer = null;
    try {
        FileSystem fs = FileSystem.get(getOutputPath().toUri(), getConf());
        FSDataOutputStream outputStream = fs.create(getOutputPath("rmse.txt"));
        double rmse = computeRmse(errors);
        writer = new BufferedWriter(new OutputStreamWriter(outputStream, Charsets.UTF_8));
        writer.write(String.valueOf(rmse));
    } finally {
        Closeables.close(writer, false);
    }

    return 0;
}

From source file:hudson.plugins.timestamper.io.TimestampsReader.java

/**
 * Read up to {@code count} time-stamps and add them to the given list.
 * //from w w  w .jav a  2 s  .  c  o  m
 * @param count
 *          the number of time-stamps to read
 * @param timestamps
 *          the list that will contain the time-stamps (optional)
 * @throws IOException
 */
private void read(int count, Optional<List<Timestamp>> timestamps) throws IOException {
    if (count < 1 || !timestampsFile.isFile()) {
        return;
    }
    InputStream inputStream = new FileInputStream(timestampsFile);
    boolean threw = true;
    try {
        ByteStreams.skipFully(inputStream, filePointer);
        inputStream = new BufferedInputStream(inputStream);
        int i = 0;
        while (i < count && filePointer < timestampsFile.length()) {
            Timestamp timestamp = readNext(inputStream);
            if (timestamps.isPresent()) {
                timestamps.get().add(timestamp);
            }
            i++;
        }
        threw = false;
    } finally {
        Closeables.close(inputStream, threw);
    }
}