Example usage for com.google.common.io Closeables closeQuietly

List of usage examples for com.google.common.io Closeables closeQuietly

Introduction

In this page you can find the example usage for com.google.common.io Closeables closeQuietly.

Prototype

public static void closeQuietly(@Nullable Reader reader) 

Source Link

Document

Closes the given Reader , logging any IOException that's thrown rather than propagating it.

Usage

From source file:net.sourceforge.docfetcher.model.parse.OpenOfficeParser.java

@Override
protected final ParseResult parse(@NotNull File file, @NotNull ParseContext context) throws ParseException {
    ZipFile zipFile = null;//from  w ww  .ja  va2  s  .c  o m
    try {
        // Get zip entries
        zipFile = new ZipFile(file);
        ZipEntry manifZipEntry = zipFile.getEntry("META-INF/manifest.xml"); //$NON-NLS-1$
        ZipEntry metaZipEntry = zipFile.getEntry("meta.xml"); //$NON-NLS-1$
        ZipEntry contentZipEntry = zipFile.getEntry("content.xml"); //$NON-NLS-1$
        if (manifZipEntry == null || metaZipEntry == null || contentZipEntry == null)
            throw new ParseException(Msg.file_corrupted.get());

        // Find out if file is password protected
        InputStream manifInputStream = zipFile.getInputStream(manifZipEntry);
        Source manifSource = new Source(manifInputStream);
        Closeables.closeQuietly(manifInputStream);
        manifSource.setLogger(null);
        StartTag encryptTag = manifSource.getNextStartTag(0, "manifest:encryption-data"); //$NON-NLS-1$
        if (encryptTag != null)
            throw new ParseException(Msg.doc_pw_protected.get());

        // Get tags from meta.xml file
        InputStream metaInputStream = zipFile.getInputStream(metaZipEntry);
        Source metaSource = new Source(metaInputStream);
        Closeables.closeQuietly(metaInputStream);
        metaSource.setLogger(null);
        String title = getElementContent(metaSource, "dc:title"); //$NON-NLS-1$
        String author = getElementContent(metaSource, "dc:creator"); //$NON-NLS-1$
        String description = getElementContent(metaSource, "dc:description"); //$NON-NLS-1$
        String subject = getElementContent(metaSource, "dc:subject"); //$NON-NLS-1$
        String keyword = getElementContent(metaSource, "meta:keyword"); //$NON-NLS-1$

        // Collect content.xml entries
        List<ZipEntry> contentEntries = new ArrayList<ZipEntry>();
        contentEntries.add(contentZipEntry);
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            ZipEntry entry = zipEntries.nextElement();
            if (entry.getName().endsWith("/content.xml")) //$NON-NLS-1$
                contentEntries.add(entry);
        }

        // Get contents from the content.xml entries
        StringBuilder sb = new StringBuilder();
        for (ZipEntry entry : contentEntries) {
            InputStream contentInputStream = zipFile.getInputStream(entry);
            Source contentSource = new Source(contentInputStream);
            Closeables.closeQuietly(contentInputStream);
            contentSource.setLogger(null);
            Element contentElement = contentSource.getNextElement(0, "office:body"); //$NON-NLS-1$
            if (contentElement == null) // this content.xml file doesn't seem to contain text
                continue;
            String content = contentElement.getContent().getTextExtractor().toString();
            sb.append(content).append(" "); //$NON-NLS-1$
        }

        // Create and return parse result
        ParseResult parseResult = new ParseResult(sb);
        parseResult.setTitle(title);
        parseResult.addAuthor(author);
        parseResult.addMiscMetadata(description);
        parseResult.addMiscMetadata(subject);
        parseResult.addMiscMetadata(keyword);
        return parseResult;
    } catch (IOException e) {
        throw new ParseException(e);
    } finally {
        closeZipFile(zipFile);
    }
}

From source file:org.apache.mahout.classifier.rbm.model.SimpleRBM.java

/**
 * Materialize.//from w w  w.ja v a  2  s. c  o  m
 *
 * @param output path to serialize to
 * @param conf the hadoop config
 * @return the rbm
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static SimpleRBM materialize(Path output, Configuration conf) throws IOException {
    FileSystem fs = output.getFileSystem(conf);
    Matrix weightMatrix;
    String visLayerType = "";
    String hidLayerType = "";
    FSDataInputStream in = fs.open(output);
    try {

        char chr;
        while ((chr = in.readChar()) != ' ')
            ;
        while ((chr = in.readChar()) != ' ')
            visLayerType += chr;
        while ((chr = in.readChar()) != ' ')
            hidLayerType += chr;

        weightMatrix = MatrixWritable.readMatrix(in);
    } finally {
        Closeables.closeQuietly(in);
    }
    Layer vl = ClassUtils.instantiateAs(visLayerType, Layer.class, new Class[] { int.class },
            new Object[] { weightMatrix.rowSize() });
    Layer hl = ClassUtils.instantiateAs(hidLayerType, Layer.class, new Class[] { int.class },
            new Object[] { weightMatrix.columnSize() });

    SimpleRBM rbm = new SimpleRBM(vl, hl);
    rbm.setWeightMatrix(weightMatrix);
    return rbm;
}

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

@Override
protected void doTrain(File targetFile, DataModel inmemoryData, int numProcessors) throws IOException {
    BufferedWriter writer = null;

    ExecutorService executorService = Executors.newFixedThreadPool(numProcessors + 1);

    try {/* ww w. jav a2 s.com*/

        ItemSimilarity similarity = (ItemSimilarity) Class.forName(conf.getSimilarityClass())
                .getConstructor(DataModel.class).newInstance(inmemoryData);

        ItemBasedRecommender trainer = new GenericItemBasedRecommender(inmemoryData, similarity);

        writer = new BufferedWriter(new FileWriter(targetFile));

        int batchSize = 100;
        int numItems = inmemoryData.getNumItems();

        List<long[]> itemIDBatches = queueItemIDsInBatches(inmemoryData.getItemIDs(), numItems, batchSize);

        log.info("Queued {} items in {} batches", numItems, itemIDBatches.size());

        BlockingQueue<long[]> itemsIDsToProcess = new LinkedBlockingQueue<long[]>(itemIDBatches);
        BlockingQueue<String> output = new LinkedBlockingQueue<String>();

        AtomicInteger numActiveWorkers = new AtomicInteger(numProcessors);
        for (int n = 0; n < numProcessors; n++) {
            executorService.execute(new SimilarItemsWorker(n, itemsIDsToProcess, output, trainer,
                    conf.getSimilarItemsPerItem(), numActiveWorkers));
        }
        executorService.execute(new OutputWriter(output, writer, numActiveWorkers));

    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        executorService.shutdown();
        try {
            executorService.awaitTermination(6, TimeUnit.HOURS);
        } catch (InterruptedException e) {

        }
        Closeables.closeQuietly(writer);
    }
}

From source file:com.skp.experiment.common.MahoutTestCase.java

protected static void writeLines(File file, String... lines) throws IOException {
    Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
    try {/*  w ww .  j av  a2 s.  c o m*/
        for (String line : lines) {
            writer.write(line);
            writer.write('\n');
        }
    } finally {
        Closeables.closeQuietly(writer);
    }
}

From source file:com.sonarsource.lits.Dump.java

private static void endRule(PrintStream out) {
    endComponent(out);
    out.print("}\n");
    Closeables.closeQuietly(out);
}

From source file:de.blizzy.documentr.access.UserStore.java

@PostConstruct
public void init() throws IOException, GitAPIException {
    String passwordHash = passwordEncoder.encodePassword("admin", "admin"); //$NON-NLS-1$ //$NON-NLS-2$
    User adminUser = new User("admin", passwordHash, "admin@example.com", false); //$NON-NLS-1$ //$NON-NLS-2$

    ILockedRepository repo = null;//from  w w w . j a  v  a2 s.  c  o m
    boolean created = false;
    try {
        repo = globalRepositoryManager.createProjectCentralRepository(REPOSITORY_NAME, false, adminUser);
        created = true;
    } catch (IllegalStateException e) {
        // okay
    } finally {
        Closeables.closeQuietly(repo);
    }

    if (created) {
        createInitialAdmin(adminUser);
        createInitialRoles(adminUser);
    }
}

From source file:org.apache.mahout.classifier.rbm.model.SimpleRBM.java

@Override
public void serialize(Path output, Configuration conf) throws IOException {
    FileSystem fs = output.getFileSystem(conf);
    String rbmnr = conf.get("rbmnr");
    FSDataOutputStream out = null;/*  ww  w . java  2  s.  c o m*/
    if (rbmnr != null)
        out = fs.create(new Path(output, conf.get("rbmnr")), true);
    else
        out = fs.create(output, true);

    try {
        out.writeChars(this.getClass().getName() + " ");
        out.writeChars(visibleLayer.getClass().getName() + " ");
        out.writeChars(hiddenLayer.getClass().getName() + " ");

        MatrixWritable.writeMatrix(out, weightMatrix);
    } finally {
        Closeables.closeQuietly(out);
    }
}

From source file:com.metamx.druid.curator.announcement.Announcer.java

@LifecycleStop
public void stop() {
    synchronized (toAnnounce) {
        if (!started) {
            return;
        }// ww w  . j  a v  a2s  .  c  om

        started = false;

        for (Map.Entry<String, PathChildrenCache> entry : listeners.entrySet()) {
            Closeables.closeQuietly(entry.getValue());
        }

        for (Map.Entry<String, ConcurrentMap<String, byte[]>> entry : announcements.entrySet()) {
            String basePath = entry.getKey();

            for (String announcementPath : entry.getValue().keySet()) {
                unannounce(ZKPaths.makePath(basePath, announcementPath));
            }
        }

        for (String parent : parentsIBuilt) {
            try {
                curator.delete().forPath(parent);
            } catch (Exception e) {
                log.info(e, "Unable to delete parent[%s], boooo.", parent);
            }
        }
    }
}

From source file:de.blizzy.documentr.subscription.SubscriptionStore.java

public void subscribe(String projectName, String branchName, String path, User user) throws IOException {
    ILockedRepository repo = null;//from   ww  w  .  j av  a 2 s .  com
    try {
        repo = getOrCreateRepository(user);
        String loginName = user.getLoginName();
        String json = BlobUtils.getHeadContent(repo.r(), loginName + SUBSCRIPTIONS_SUFFIX);
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        Set<Page> pages = Sets.newHashSet();
        if (StringUtils.isNotBlank(json)) {
            List<Page> pagesList = gson.fromJson(json, new TypeToken<List<Page>>() {
            }.getType());
            pages = Sets.newHashSet(pagesList);
        }

        Page page = new Page(projectName, branchName, path);
        if (pages.add(page)) {
            json = gson.toJson(pages);
            File workingDir = RepositoryUtil.getWorkingDir(repo.r());
            File file = new File(workingDir, loginName + SUBSCRIPTIONS_SUFFIX);
            FileUtils.writeStringToFile(file, json, Charsets.UTF_8);

            Git git = Git.wrap(repo.r());
            git.add().addFilepattern(loginName + SUBSCRIPTIONS_SUFFIX).call();
            PersonIdent ident = new PersonIdent(loginName, user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage(loginName + SUBSCRIPTIONS_SUFFIX)
                    .call();
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:com.metamx.druid.index.v1.MMappedIndexAdapter.java

@Override
public Iterable<Rowboat> getRows() {
    return new Iterable<Rowboat>() {
        @Override/*from   w ww. java2 s.  co  m*/
        public Iterator<Rowboat> iterator() {
            return new Iterator<Rowboat>() {
                final IndexedLongs timestamps = index.getReadOnlyTimestamps();
                final MetricHolder[] metrics;
                final IndexedFloats[] floatMetrics;
                final Map<String, Indexed<? extends IndexedInts>> dimensions;

                final int numMetrics = index.getAvailableMetrics().size();

                int currRow = 0;
                boolean done = false;

                {
                    dimensions = Maps.newLinkedHashMap();
                    for (String dim : index.getAvailableDimensions()) {
                        dimensions.put(dim, index.getDimColumn(dim));
                    }

                    final Indexed<String> availableMetrics = index.getAvailableMetrics();
                    metrics = new MetricHolder[availableMetrics.size()];
                    floatMetrics = new IndexedFloats[availableMetrics.size()];
                    for (int i = 0; i < metrics.length; ++i) {
                        metrics[i] = index.getMetricHolder(availableMetrics.get(i));
                        if (metrics[i].getType() == MetricHolder.MetricType.FLOAT) {
                            floatMetrics[i] = metrics[i].getFloatType();
                        }
                    }
                }

                @Override
                public boolean hasNext() {
                    final boolean hasNext = currRow < numRows;
                    if (!hasNext && !done) {
                        Closeables.closeQuietly(timestamps);
                        for (IndexedFloats floatMetric : floatMetrics) {
                            Closeables.closeQuietly(floatMetric);
                        }
                        done = true;
                    }
                    return hasNext;
                }

                @Override
                public Rowboat next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }

                    int[][] dims = new int[dimensions.size()][];
                    int dimIndex = 0;
                    for (String dim : dimensions.keySet()) {
                        IndexedInts dimVals = dimensions.get(dim).get(currRow);

                        int[] theVals = new int[dimVals.size()];
                        for (int j = 0; j < theVals.length; ++j) {
                            theVals[j] = dimVals.get(j);
                        }

                        dims[dimIndex++] = theVals;
                    }

                    Object[] metricArray = new Object[numMetrics];
                    for (int i = 0; i < metricArray.length; ++i) {
                        switch (metrics[i].getType()) {
                        case FLOAT:
                            metricArray[i] = floatMetrics[i].get(currRow);
                            break;
                        case COMPLEX:
                            metricArray[i] = metrics[i].getComplexType().get(currRow);
                        }
                    }

                    Map<String, String> descriptions = Maps.newHashMap();
                    for (String spatialDim : index.getSpatialIndexes().keySet()) {
                        descriptions.put(spatialDim, "spatial");
                    }
                    final Rowboat retVal = new Rowboat(timestamps.get(currRow), dims, metricArray, currRow,
                            descriptions);

                    ++currRow;

                    return retVal;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}