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:org.apache.mahout.classifier.sgd.NewsgroupHelper.java

Vector encodeFeatureVector(File file, int actual, int leakType, Multiset<String> overallCounts)
        throws IOException {
    long date = (long) (1000 * (DATE_REFERENCE + actual * MONTH + 1 * WEEK * rand.nextDouble()));
    Multiset<String> words = ConcurrentHashMultiset.create();

    BufferedReader reader = Files.newReader(file, Charsets.UTF_8);
    try {/*from w w w  .jav a  2 s  .c o  m*/
        String line = reader.readLine();
        Reader dateString = new StringReader(DATE_FORMATS[leakType % 3].format(new Date(date)));
        countWords(analyzer, words, dateString, overallCounts);
        while (line != null && !line.isEmpty()) {
            boolean countHeader = (line.startsWith("From:") || line.startsWith("Subject:")
                    || line.startsWith("Keywords:") || line.startsWith("Summary:")) && leakType < 6;
            do {
                Reader in = new StringReader(line);
                if (countHeader) {
                    countWords(analyzer, words, in, overallCounts);
                }
                line = reader.readLine();
            } while (line != null && line.startsWith(" "));
        }
        if (leakType < 3) {
            countWords(analyzer, words, reader, overallCounts);
        }
    } finally {
        Closeables.closeQuietly(reader);
    }

    Vector v = new RandomAccessSparseVector(FEATURES);
    bias.addToVector("", 1, v);
    for (String word : words.elementSet()) {
        encoder.addToVector(word, Math.log1p(words.count(word)), v);
    }

    return v;
}

From source file:com.zimbra.cs.pop3.Pop3Mailbox.java

/**
 * initialize the Pop3Mailbox, without keeping a reference to either the Mailbox object or
 * any of the Message objects in the inbox.
 *//*from   w ww  .j  a v a  2 s.  com*/
Pop3Mailbox(Mailbox mbox, Account acct, String query) throws ServiceException {
    id = mbox.getId();
    numDeleted = 0;
    opContext = new OperationContext(acct);
    deleteOption = acct.getPrefPop3DeleteOption();

    if (Strings.isNullOrEmpty(query)) {
        Set<Integer> folderIds = acct.isPrefPop3IncludeSpam()
                ? ImmutableSet.of(Mailbox.ID_FOLDER_INBOX, Mailbox.ID_FOLDER_SPAM)
                : Collections.singleton(Mailbox.ID_FOLDER_INBOX);
        String dateConstraint = acct.getAttr(Provisioning.A_zimbraPrefPop3DownloadSince);
        Date popSince = dateConstraint == null ? null : LdapDateUtil.parseGeneralizedTime(dateConstraint);
        messages = mbox.openPop3Folder(opContext, folderIds, popSince);
        for (Pop3Message p3m : messages) {
            totalSize += p3m.getSize();
        }
    } else {
        ZimbraQueryResults results = null;
        messages = new ArrayList<Pop3Message>(500);
        try {
            results = mbox.index.search(opContext, query, POP3_TYPES, SortBy.DATE_DESC, 500);

            while (results.hasNext()) {
                ZimbraHit hit = results.getNext();
                if (hit instanceof MessageHit) {
                    MessageHit mh = (MessageHit) hit;
                    Message msg = mh.getMessage();
                    if (!msg.isTagged(Flag.FlagInfo.POPPED)) {
                        totalSize += msg.getSize();
                        messages.add(new Pop3Message(msg));
                    }
                }
            }
        } finally {
            Closeables.closeQuietly(results);
        }
    }
}

From source file:co.cask.cdap.common.lang.jar.BundleJarUtil.java

/**
 * Returns an {@link InputSupplier} for a given entry. This avoids unjar the whole file to just get one entry.
 * However, to get many entries, unjar would be more efficient. Also, the jar file is scanned every time the
 * {@link InputSupplier#getInput()} is invoked.
 *
 * @param jarLocation Location of the jar file.
 * @param entryName Name of the entry to fetch
 * @return An {@link InputSupplier}.// w w  w.  jav a  2  s . c  o m
 */
public static InputSupplier<InputStream> getEntry(final Location jarLocation, final String entryName)
        throws IOException {
    Preconditions.checkArgument(jarLocation != null);
    Preconditions.checkArgument(entryName != null);
    final URI uri = jarLocation.toURI();

    // Small optimization if the location is local
    if ("file".equals(uri.getScheme())) {
        return new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                final JarFile jarFile = new JarFile(new File(uri));
                ZipEntry entry = jarFile.getEntry(entryName);
                if (entry == null) {
                    throw new IOException("Entry not found for " + entryName);
                }
                return new FilterInputStream(jarFile.getInputStream(entry)) {
                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            jarFile.close();
                        }
                    }
                };
            }
        };
    }

    // Otherwise, use JarInputStream
    return new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            JarInputStream is = new JarInputStream(jarLocation.getInputStream());
            JarEntry entry = is.getNextJarEntry();
            while (entry != null) {
                if (entryName.equals(entry.getName())) {
                    return is;
                }
                entry = is.getNextJarEntry();
            }
            Closeables.closeQuietly(is);
            throw new IOException("Entry not found for " + entryName);
        }
    };
}

From source file:com.netflix.exhibitor.core.processes.ProcessMonitor.java

private Runnable makeEofProc(final Process process, final String completionMessage, final Mode mode) {
    return new Runnable() {
        @Override/*  w w w . j  a  v  a  2  s . c om*/
        public void run() {
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                if (mode == Mode.DESTROY_ON_INTERRUPT) {
                    Closeables.closeQuietly(process.getErrorStream());
                    Closeables.closeQuietly(process.getInputStream());
                    Closeables.closeQuietly(process.getOutputStream());

                    process.destroy();
                }
            }

            if (completionMessage != null) {
                exhibitor.getLog().add(ActivityLog.Type.INFO, completionMessage);
            }
        }
    };
}

From source file:com.netflix.exhibitor.servlet.ExhibitorServletContextListener.java

private Map<String, String> makeArgsBuilder() {
    Map<String, String> argsBuilder = Maps.newHashMap();

    try {/*from w  w  w. j  ava2 s.c  o  m*/
        URL resource = Resources.getResource(EXHIBITOR_PROPERTIES);
        InputStream stream = resource.openStream();
        try {
            Properties properties = new Properties(System.getProperties());
            properties.load(stream);
            applyProperties(argsBuilder, properties);
        } finally {
            Closeables.closeQuietly(stream);
        }
    } catch (IllegalArgumentException e) {
        log.warn("Could not find " + EXHIBITOR_PROPERTIES);
    } catch (IOException e) {
        log.error("Could not load " + EXHIBITOR_PROPERTIES, e);
    }

    applyProperties(argsBuilder, System.getProperties());

    return argsBuilder;
}

From source file:org.cogroo.analyzer.ComponentFactory.java

public Analyzer createSentenceDetector() {
    long start = System.nanoTime();
    SentenceDetectorME sentenceDetector = null;
    InputStream modelIn = null;/*  w  w w.ja v a2 s  .c  om*/
    Analyzer analyzer = null;
    if (modelPathMap.containsKey(Analyzers.SENTENCE_DETECTOR)) {
        try {
            modelIn = ComponentFactory.class.getResourceAsStream(modelPathMap.get(Analyzers.SENTENCE_DETECTOR));
            SentenceModel model = new SentenceModel(modelIn);
            sentenceDetector = new SentenceDetectorME(model);
        } catch (IOException e) {
            LOGGER.fatal("Couldn't load sentence model!", e);
        } finally {
            Closeables.closeQuietly(modelIn);
        }

        if (sentenceDetector == null)
            throw new InitializationException("Couldn't load SentenceDetectorME class");

        analyzer = new SentenceDetector(sentenceDetector);
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Initialized SentenceDetector in " + ((System.nanoTime() - start) / 1000000) + "ms]");
    }
    return analyzer;
}

From source file:org.sonarsource.ldap.it.ApacheDS.java

/**
 * Stream will be closed automatically./*from w  ww. ja  va 2 s . c  o m*/
 */
public void importLdif(InputStream is) throws Exception {
    Preconditions.checkState(directoryService.isStarted(), "Directory service not started");
    try {
        LdifReader entries = new LdifReader(is);
        CoreSession rootDSE = directoryService.getAdminSession();
        // see LdifFileLoader
        for (LdifEntry ldifEntry : entries) {
            LOG.info(ldifEntry.toString());
            if (ChangeType.Add == ldifEntry.getChangeType()) {
                rootDSE.add(new DefaultServerEntry(rootDSE.getDirectoryService().getRegistries(),
                        ldifEntry.getEntry()));
            } else if (ChangeType.Modify == ldifEntry.getChangeType()) {
                rootDSE.modify(ldifEntry.getDn(), ldifEntry.getModificationItems());
            } else if (ChangeType.Delete == ldifEntry.getChangeType()) {
                rootDSE.delete(ldifEntry.getDn());
            }
        }
    } finally {
        Closeables.closeQuietly(is);
    }
}

From source file:com.b2international.snowowl.snomed.reasoner.server.net4j.SnomedOntologyExportIndication.java

@Override
protected void responding(final ExtendedDataOutputStream out, final OMMonitor monitor) throws Exception {
    final SnomedOntologyService ontologyService = ApplicationContext.getInstance()
            .getService(SnomedOntologyService.class);
    OWLOntology ontology = null;/* ww w .ja v  a  2 s  .com*/
    Async async = null;

    monitor.begin();

    try {
        async = monitor.forkAsync();

        final File ontologyTempFile = TMPUtil.createTempFile();
        ontology = ontologyService.createOntology(branchPath, true);
        ontologyService.saveOntology(ontology, exportType, ontologyTempFile);

        final long length = ontologyTempFile.length();
        out.writeLong(length);

        InputStream inputStream = null;

        try {
            inputStream = IOUtil.openInputStream(ontologyTempFile);
            inputStream = new BufferedInputStream(inputStream);
            IOUtil.copyBinary(inputStream, out, length);
        } finally {
            Closeables.closeQuietly(inputStream);
        }

    } finally {
        if (null != async) {
            async.stop();
        }

        if (null != ontology) {
            ontologyService.removeOntology(ontology);
        }

        monitor.done();
    }
}

From source file:com.github.praxissoftware.maven.plugins.GenerateFeaturesMojo.java

@SuppressWarnings("unchecked")
@Override/*from   ww  w.j  a  v  a2  s. c  o  m*/
public void execute() throws MojoExecutionException {
    Writer out = null;
    try {

        // Get the template text from the jar's resources.
        final InputSupplier<InputStreamReader> supplier = CharStreams
                .newReaderSupplier(new InputSupplier<InputStream>() {
                    @Override
                    public InputStream getInput() throws IOException {
                        return getClass().getClassLoader().getResourceAsStream("features.mustache.xml");
                    }
                }, Charsets.UTF_8);
        final String template = CharStreams.toString(supplier);

        // Create the mustache factory from the loaded template.
        final Mustache mustache = new MustacheBuilder().parse(template, "features.mustache.xml");

        // Establish output stream.
        final File featureFile = setUpFile(outputFile);
        out = new FileWriter(featureFile);

        // Build context.
        final Map<String, Object> context = convert(project.getArtifact());

        final List<Map<String, Object>> dependencies = Lists.newArrayList();
        for (final Artifact dependency : Ordering.natural().onResultOf(new SortByCoordinates())
                .sortedCopy(Iterables.filter((Collection<Artifact>) project.getDependencyArtifacts(),
                        new ArtifactsWeWant()))) {
            dependencies.add(convert(dependency));
        }
        context.put("dependencies", dependencies);

        getLog().info("Writing feature to " + outputFile.getAbsolutePath());

        // Render template.
        mustache.execute(out, context);
    } catch (final Exception e) {
        Throwables.propagateIfInstanceOf(e, MojoExecutionException.class);
        Throwables.propagateIfPossible(e);
        throw new MojoExecutionException("Unable to generate features.xml.", e);
    } finally {
        Closeables.closeQuietly(out);
    }
}

From source file:org.apache.giraph.partition.IdenticalWorkerPartitioner.java

public void initialization(Configuration job) {
    Path path = new Path(partitionMapPath); //used to open file by filesystem
    vid2pid = new short[graphVertexNumber];
    //read the partition file to build the vid2pid map.
    FileSystem fs = null;/*from   www  . j av a2s.c  om*/
    FSDataInputStream fileIn = null;
    BufferedReader reader = null;
    try {
        fs = path.getFileSystem(job);
        fileIn = fs.open(path);
        reader = new BufferedReader(new InputStreamReader(fileIn, Charsets.UTF_8));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] tokens = SEPARATOR.split(line);
            vid2pid[Integer.valueOf(tokens[0])] = Short.valueOf(tokens[1]);
        }
        //     if(LOG.isDebugEnabled()) {
        //        for(Integer key:vid2pid.keySet()) {
        //           LOG.debug("HashTable: key= " + key +" , value= " + vid2pid.get(key));
        //        }
        //     }
    } catch (IOException e) {
    } finally {
        Closeables.closeQuietly(fileIn);
        Closeables.closeQuietly(reader);
    }
}