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.spark.shuffle.sort.BypassMergeSortShuffleWriter.java

/**
 * Concatenate all of the per-partition files into a single combined file.
 *
 * @return array of lengths, in bytes, of each partition of the file (used by map output tracker).
 *///from   w w w. ja v a 2 s. c  om
private long[] writePartitionedFile(File outputFile) throws IOException {
    // Track location of the partition starts in the output file
    final long[] lengths = new long[numPartitions];
    if (partitionWriters == null) {
        // We were passed an empty iterator
        return lengths;
    }

    final FileOutputStream out = new FileOutputStream(outputFile, true);
    final long writeStartTime = System.nanoTime();
    boolean threwException = true;
    try {
        for (int i = 0; i < numPartitions; i++) {
            final FileInputStream in = new FileInputStream(partitionWriters[i].fileSegment().file());
            boolean copyThrewException = true;
            try {
                lengths[i] = Utils.copyStream(in, out, false, transferToEnabled);
                copyThrewException = false;
            } finally {
                Closeables.close(in, copyThrewException);
            }
            if (!partitionWriters[i].fileSegment().file().delete()) {
                logger.error("Unable to delete file for partition {}", i);
            }
        }
        threwException = false;
    } finally {
        Closeables.close(out, threwException);
        writeMetrics.incShuffleWriteTime(System.nanoTime() - writeStartTime);
    }
    partitionWriters = null;
    return lengths;
}

From source file:org.apache.mahout.clustering.iterator.ClusterIterator.java

/**
 * Return if all of the Clusters in the parts in the filePath have converged or not
 * //from  ww w.  ja v a2  s .c  o m
 * @param filePath
 *          the file path to the single file containing the clusters
 * @return true if all Clusters are converged
 * @throws IOException
 *           if there was an IO error
 */
private static boolean isConverged(Path filePath, Configuration conf, FileSystem fs) throws IOException {
    for (FileStatus part : fs.listStatus(filePath, PathFilters.partFilter())) {
        SequenceFileValueIterator<ClusterWritable> iterator = new SequenceFileValueIterator<ClusterWritable>(
                part.getPath(), true, conf);
        while (iterator.hasNext()) {
            ClusterWritable value = iterator.next();
            if (!value.getValue().isConverged()) {
                Closeables.close(iterator, true);
                return false;
            }
        }
    }
    return true;
}

From source file:org.gpfvic.mahout.cf.taste.impl.model.file.FileDataModel.java

/**
 * @param delimiterRegex If your data file don't use '\t' or ',' as delimiters, you can specify 
 * user own using regex pattern./*www .j a v  a  2  s  .c  o m*/
 * @throws IOException
 */
public FileDataModel(File dataFile, boolean transpose, long minReloadIntervalMS, String delimiterRegex)
        throws IOException {

    this.dataFile = Preconditions.checkNotNull(dataFile.getAbsoluteFile());
    if (!dataFile.exists() || dataFile.isDirectory()) {
        throw new FileNotFoundException(dataFile.toString());
    }
    Preconditions.checkArgument(dataFile.length() > 0L, "dataFile is empty");
    Preconditions.checkArgument(minReloadIntervalMS >= 0L, "minReloadIntervalMs must be non-negative");

    log.info("Creating FileDataModel for file {}", dataFile);

    this.lastModified = dataFile.lastModified();
    this.lastUpdateFileModified = readLastUpdateFileModified();

    FileLineIterator iterator = new FileLineIterator(dataFile, false);
    String firstLine = iterator.peek();
    while (firstLine.isEmpty() || firstLine.charAt(0) == COMMENT_CHAR) {
        iterator.next();
        firstLine = iterator.peek();
    }
    Closeables.close(iterator, true);

    char delimiter;
    if (delimiterRegex == null) {
        delimiter = determineDelimiter(firstLine);
        delimiterPattern = Splitter.on(delimiter);
    } else {
        delimiter = '\0';
        delimiterPattern = Splitter.onPattern(delimiterRegex);
        if (!delimiterPattern.split(firstLine).iterator().hasNext()) {
            throw new IllegalArgumentException("Did not find a delimiter(pattern) in first line");
        }
    }
    List<String> firstLineSplit = new ArrayList<>();
    for (String token : delimiterPattern.split(firstLine)) {
        firstLineSplit.add(token);
    }
    // If preference value exists and isn't empty then the file is specifying pref values
    hasPrefValues = firstLineSplit.size() >= 3 && !firstLineSplit.get(2).isEmpty();

    this.reloadLock = new ReentrantLock();
    this.transpose = transpose;
    this.minReloadIntervalMS = minReloadIntervalMS;

    reload();
}

From source file:de.faustedition.reasoning.InscriptionPrecedenceResource.java

@Get("svg|html")
public Representation svg() throws IOException, ExecutionException, InterruptedException {
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final Process tred = new ProcessBuilder(environment.getRequiredProperty("graphviz.tred.path")).start();
    final Process dot = new ProcessBuilder(environment.getRequiredProperty("graphviz.dot.path"), "-Tsvg")
            .start();/*from w  w  w  .ja v  a2s . c  om*/

    executorService.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStream dataStream = null;
            OutputStream tredStream = null;
            try {
                ByteStreams.copy(
                        dataStream = new ByteArrayInputStream(asDot().getBytes(Charset.forName("UTF-8"))),
                        tredStream = tred.getOutputStream());
            } finally {
                Closeables.close(dataStream, false);
                Closeables.close(tredStream, false);
            }
            return null;
        }
    });

    executorService.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStream tredStream = null;
            OutputStream dotStream = null;
            try {
                ByteStreams.copy(tredStream = tred.getInputStream(), dotStream = dot.getOutputStream());
            } finally {
                Closeables.close(tredStream, false);
                Closeables.close(dotStream, false);
            }
            return null;
        }
    });

    final Future<FileBackedOutputStream> dotFuture = executorService
            .submit(new Callable<FileBackedOutputStream>() {
                @Override
                public FileBackedOutputStream call() throws Exception {
                    final FileBackedOutputStream buf = new FileBackedOutputStream(102400);
                    InputStream dotStream = null;
                    try {
                        ByteStreams.copy(dotStream = dot.getInputStream(), buf);
                    } finally {
                        Closeables.close(dotStream, false);
                        Closeables.close(buf, false);
                    }
                    return buf;
                }
            });

    Preconditions.checkState(tred.waitFor() == 0);
    Preconditions.checkState(dot.waitFor() == 0);

    final FileBackedOutputStream resultBuf = dotFuture.get();

    return new OutputRepresentation(MediaType.IMAGE_SVG) {
        @Override
        public void write(OutputStream outputStream) throws IOException {
            ByteStreams.copy(resultBuf.getSupplier(), outputStream);
            resultBuf.reset();
        }
    };
}

From source file:org.apache.mahout.cf.taste.impl.model.file.FileDataModel.java

/**
 * @param delimiterRegex If your data file don't use '\t' or ',' as delimiters, you can specify 
 * user own using regex pattern./*from   w w w.  ja  v  a  2 s .c om*/
 * @throws IOException
 */
public FileDataModel(File dataFile, boolean transpose, long minReloadIntervalMS, String delimiterRegex)
        throws IOException {

    this.dataFile = Preconditions.checkNotNull(dataFile.getAbsoluteFile());
    if (!dataFile.exists() || dataFile.isDirectory()) {
        throw new FileNotFoundException(dataFile.toString());
    }
    Preconditions.checkArgument(dataFile.length() > 0L, "dataFile is empty");
    Preconditions.checkArgument(minReloadIntervalMS >= 0L, "minReloadIntervalMs must be non-negative");

    log.info("Creating FileDataModel for file {}", dataFile);

    this.lastModified = dataFile.lastModified();
    this.lastUpdateFileModified = readLastUpdateFileModified();

    FileLineIterator iterator = new FileLineIterator(dataFile, false);
    String firstLine = iterator.peek();
    while (firstLine.isEmpty() || firstLine.charAt(0) == COMMENT_CHAR) {
        iterator.next();
        firstLine = iterator.peek();
    }
    Closeables.close(iterator, true);

    if (delimiterRegex == null) {
        delimiter = determineDelimiter(firstLine);
        delimiterPattern = Splitter.on(delimiter);
    } else {
        delimiter = '\0';
        delimiterPattern = Splitter.onPattern(delimiterRegex);
        if (!delimiterPattern.split(firstLine).iterator().hasNext()) {
            throw new IllegalArgumentException("Did not find a delimiter(pattern) in first line");
        }
    }
    List<String> firstLineSplit = Lists.newArrayList();
    for (String token : delimiterPattern.split(firstLine)) {
        firstLineSplit.add(token);
    }
    // If preference value exists and isn't empty then the file is specifying pref values
    hasPrefValues = firstLineSplit.size() >= 3 && !firstLineSplit.get(2).isEmpty();

    this.reloadLock = new ReentrantLock();
    this.transpose = transpose;
    this.minReloadIntervalMS = minReloadIntervalMS;

    reload();
}

From source file:fr.obeo.releng.targetplatform.pde.Converter.java

private Diagnostic serialize(URI targetPlatformLocation, String xml) {
    BasicDiagnostic ret = new BasicDiagnostic();
    URI targetDefinitionLocation = targetPlatformLocation.trimFileExtension().appendFileExtension("target");

    if (hasContentDifferencesOtherThanSequenceNumber(targetDefinitionLocation, xml)) {
        OutputStream outputStream = null;
        try {// w w w . j a  va  2s. c om
            outputStream = new BufferedOutputStream(
                    new FileOutputStream(new File(targetDefinitionLocation.toFileString())));
            outputStream.write(xml.getBytes());
        } catch (Exception e) {
            ret.merge(BasicDiagnostic.toDiagnostic(e));
        } finally {
            try {
                Closeables.close(outputStream, true);
            } catch (IOException e) {
                // swallowed
            }
        }
    } else {
        ret.merge(
                new BasicDiagnostic(Diagnostic.INFO, TargetPlatformBundleActivator.PLUGIN_ID, -1,
                        "The target definition '" + targetDefinitionLocation
                                + "' did not changed since previous generation, we did not overwrote it.",
                        null));
    }

    return ret;
}

From source file:eu.interedition.web.text.TextController.java

@RequestMapping(method = RequestMethod.POST, consumes = "multipart/form-data", produces = "text/html")
public RedirectView postTextForm(@RequestParam("file") MultipartFile file, //
        @RequestParam("fileType") Text.Type textType, //
        @RequestParam(value = "fileEncoding", required = false, defaultValue = "UTF-8") String charset)
        throws IOException, XMLStreamException, SAXException {
    Preconditions.checkArgument(!file.isEmpty(), "Empty file");

    InputStream fileStream = null;
    XMLStreamReader xmlReader = null;
    try {//from w ww. j  a  v  a2  s  .  c om
        switch (textType) {
        case TXT:
            return redirectTo(Text.create(sessionFactory.getCurrentSession(), null,
                    new InputStreamReader(fileStream = file.getInputStream(), Charset.forName(charset))));
        case XML:
            return redirectTo(Text.create(sessionFactory.getCurrentSession(), null,
                    xmlReader = xmlInputFactory.createXMLStreamReader(fileStream = file.getInputStream())));
        }
    } finally {
        XML.closeQuietly(xmlReader);
        Closeables.close(fileStream, false);
    }
    throw new IllegalArgumentException(textType.toString());
}

From source file:org.apache.mahout.vectorizer.tfidf.TFIDFConverter.java

/**
 * Read the document frequency List which is built at the end of the DF Count Job. This will use constant
 * memory and will run at the speed of your disk read
 *///w  w  w  .j a va  2 s .  com
private static Pair<Long[], List<Path>> createDictionaryChunks(Path featureCountPath, Path dictionaryPathBase,
        Configuration baseConf, int chunkSizeInMegabytes) throws IOException {
    List<Path> chunkPaths = Lists.newArrayList();
    Configuration conf = new Configuration(baseConf);

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

    long chunkSizeLimit = chunkSizeInMegabytes * 1024L * 1024L;
    int chunkIndex = 0;
    Path chunkPath = new Path(dictionaryPathBase, FREQUENCY_FILE + chunkIndex);
    chunkPaths.add(chunkPath);
    SequenceFile.Writer freqWriter = new SequenceFile.Writer(fs, conf, chunkPath, IntWritable.class,
            LongWritable.class);

    try {
        long currentChunkSize = 0;
        long featureCount = 0;
        long vectorCount = Long.MAX_VALUE;
        Path filesPattern = new Path(featureCountPath, OUTPUT_FILES_PATTERN);
        for (Pair<IntWritable, LongWritable> record : new SequenceFileDirIterable<IntWritable, LongWritable>(
                filesPattern, PathType.GLOB, null, null, true, conf)) {

            if (currentChunkSize > chunkSizeLimit) {
                Closeables.close(freqWriter, false);
                chunkIndex++;

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

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

            int fieldSize = SEQUENCEFILE_BYTE_OVERHEAD + Integer.SIZE / 8 + Long.SIZE / 8;
            currentChunkSize += fieldSize;
            IntWritable key = record.getFirst();
            LongWritable value = record.getSecond();
            if (key.get() >= 0) {
                freqWriter.append(key, value);
            } else if (key.get() == -1) {
                vectorCount = value.get();
            }
            featureCount = Math.max(key.get(), featureCount);

        }
        featureCount++;
        Long[] counts = { featureCount, vectorCount };
        return new Pair<Long[], List<Path>>(counts, chunkPaths);
    } finally {
        Closeables.close(freqWriter, false);
    }
}

From source file:com.android.assetstudiolib.GraphicGenerator.java

/**
 * Returns one of the built in stencil images, or null
 *
 * @param relativePath stencil path such as "launcher-stencil/square/web/back.png"
 * @return the image, or null//from w w  w . ja v  a2  s  . co  m
 * @throws IOException if an unexpected I/O error occurs
 */
public static BufferedImage getStencilImage(String relativePath) throws IOException {
    InputStream is = GraphicGenerator.class.getResourceAsStream(relativePath);
    if (is == null) {
        return null;
    }
    try {
        return ImageIO.read(is);
    } finally {
        Closeables.close(is, true /* swallowIOException */);
    }
}

From source file:org.apache.mahout.math.hadoop.decomposer.EigenVerificationJob.java

private void saveCleanEigens(Configuration conf,
        Collection<Map.Entry<MatrixSlice, EigenStatus>> prunedEigenMeta) throws IOException {
    Path path = new Path(outPath, CLEAN_EIGENVECTORS);
    FileSystem fs = FileSystem.get(path.toUri(), conf);
    SequenceFile.Writer seqWriter = new SequenceFile.Writer(fs, conf, path, IntWritable.class,
            VectorWritable.class);
    try {//from  w ww .ja v  a2  s.c  o m
        IntWritable iw = new IntWritable();
        int numEigensWritten = 0;
        int index = 0;
        for (Map.Entry<MatrixSlice, EigenStatus> pruneSlice : prunedEigenMeta) {
            MatrixSlice s = pruneSlice.getKey();
            EigenStatus meta = pruneSlice.getValue();
            EigenVector ev = new EigenVector(s.vector(), meta.getEigenValue(), Math.abs(1 - meta.getCosAngle()),
                    s.index());
            // log.info("appending {} to {}", ev, path);
            Writable vw = new VectorWritable(ev);
            iw.set(index++);
            seqWriter.append(iw, vw);

            // increment the number of eigenvectors written and see if we've
            // reached our specified limit, or if we wish to write all eigenvectors
            // (latter is built-in, since numEigensWritten will always be > 0
            numEigensWritten++;
            if (numEigensWritten == maxEigensToKeep) {
                log.info("{} of the {} total eigens have been written", maxEigensToKeep,
                        prunedEigenMeta.size());
                break;
            }
        }
    } finally {
        Closeables.close(seqWriter, false);
    }
    cleanedEigensPath = path;
}