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.vaticanfetcher.model.parse.ChmParser.java

/**
 * Converts all <tt>ChmEntry</tt>s under <tt>entry</tt> to strings and puts them into the given <tt>StringBuilder</tt>.
 * //from  ww w  . j a v a  2  s . c  o  m
 * @param renderText
 *            Whether the textual contents of the <tt>ChmEntry</tt>s
 *            should be extracted in a readable format (true) or as raw strings (false).
 */
private void append(@NotNull StringBuilder sb, @NotNull ChmEntry entry, boolean renderText) throws IOException {
    if (entry.hasAttribute(ChmEntry.Attribute.DIRECTORY)) {
        for (ChmEntry child : entry.entries(ChmEntry.Attribute.ALL))
            append(sb, child, renderText);
    } else {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(entry.getInputStream(), "utf8" // Just guessing... //$NON-NLS-1$
            ));
            StringBuilder entryBuffer = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                entryBuffer.append(line).append("\n\n"); //$NON-NLS-1$

            /*
             * The current version of chm4j doesn't allow differentiating between binary files (such as images) 
             * and HTML files. Therefore we scan the text for HTML tags to select the HTML files.
             */
            if (isHTML(entryBuffer)) {
                Source source = new Source(entryBuffer);
                source.setLogger(null);
                if (renderText)
                    sb.append(source.getRenderer().setIncludeHyperlinkURLs(false).toString());
                else
                    sb.append(source.getTextExtractor().toString());
            }
        } catch (RuntimeException e) {
            // The HTML lib can do this to us; do nothing
        } finally {
            Closeables.closeQuietly(reader);
        }
    }
}

From source file:com.intelligentsia.dowsers.entity.store.memory.InMemoryEntityStore.java

@Override
public <T> void store(final T entity)
        throws NullPointerException, ConcurrencyException, IllegalArgumentException {
    Writer writer = null;/*from   w w  w  . j  a  v  a 2s. c  o  m*/
    try {
        writer = new StringWriter();
        entityMapper.writeValue(writer, entity);
        entities.put(References.identify(entity), writer.toString());
    } finally {
        if (writer != null) {
            Closeables.closeQuietly(writer);
        }
    }
}

From source file:ch.piratenpartei.pivote.serialize.SerializationContext.java

protected synchronized void loadMappings() throws ClassNotFoundException {
    try {//from   w w  w  . ja  va 2 s  .c o  m
        Properties properties = null;
        Enumeration<URL> resources = classLoader
                .getResources("META-INF/ch/piratenpartei/pivote/serialize/class-mappings.properties");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            log.debug("Loading mappings from {}", url);
            InputStream input = null;
            try {
                input = new BufferedInputStream(url.openStream());
                if (properties == null) {
                    properties = new Properties();
                } else {
                    properties.clear();
                }
                properties.load(input);
                for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                    String javaName = (String) entry.getKey();
                    String protocolName = (String) entry.getValue();
                    if (mappings.containsKey(javaName) || mappings.containsValue(protocolName)) {
                        log.error("Skipping duplicate mapping: {} => {}", javaName, protocolName);
                    } else {
                        Class<?> clazz = Class.forName(javaName, false, classLoader);
                        mappings.put(protocolName, piVoteSerializable(clazz));
                        log.trace("Mapping: protocol:{} <=> java:{}", protocolName, javaName);
                    }
                }
            } catch (IOException e) {
                log.error("Error loading mappings from {}", url, e);
            } finally {
                Closeables.closeQuietly(input);
            }
        }
    } catch (IOException e) {
        log.error("Cannot load class mappings", e);
    }
}

From source file:org.sonar.plugins.dotnet.tests.XmlParserHelper.java

public void close() {
    Closeables.closeQuietly(reader);

    if (stream != null) {
        try {/*w  w w.  jav a 2s  .  c om*/
            stream.close();
        } catch (XMLStreamException e) {
            throw Throwables.propagate(e);
        }
    }
}

From source file:org.sonar.plugins.jacococd.JaCoCoCDOverallSensor.java

private void mergeReports(File reportOverall, File... reports) {
    SessionInfoStore infoStore = new SessionInfoStore();
    ExecutionDataStore dataStore = new ExecutionDataStore();

    loadSourceFiles(infoStore, dataStore, reports);

    BufferedOutputStream outputStream = null;
    try {/* w  w  w .  j av a  2 s  .com*/
        outputStream = new BufferedOutputStream(new FileOutputStream(reportOverall));
        ExecutionDataWriter dataWriter = new ExecutionDataWriter(outputStream);

        infoStore.accept(dataWriter);
        dataStore.accept(dataWriter);
    } catch (IOException e) {
        throw new SonarException(
                String.format("Unable to write overall coverage report %s", reportOverall.getAbsolutePath()),
                e);
    } finally {
        Closeables.closeQuietly(outputStream);
    }
}

From source file:org.apache.drill.exec.rpc.bit.BitConnection.java

public void shutdownIfClient() {
    if (bus.isClient())
        Closeables.closeQuietly(bus);
}

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

private static File convertGLFile(File originalFile) throws IOException {
    // Now translate the file; remove commas, then convert "::" delimiter to comma
    File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "ratings.txt");
    if (resultFile.exists()) {
        resultFile.delete();//from   w ww  .  j  av a2s . c  o m
    }
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8));
        for (String line : new FileLineIterable(originalFile, false)) {
            int lastDelimiterStart = line.lastIndexOf(COLON_DELIMTER);
            if (lastDelimiterStart < 0) {
                throw new IOException("Unexpected input format on line: " + line);
            }
            String subLine = line.substring(0, lastDelimiterStart);
            String convertedLine = COLON_DELIMITER_PATTERN.matcher(subLine).replaceAll(",");
            writer.println(convertedLine);
        }
    } catch (IOException ioe) {
        resultFile.delete();
        throw ioe;
    } finally {
        Closeables.closeQuietly(writer);
    }
    return resultFile;
}

From source file:co.cask.tigon.internal.app.FlowSpecificationAdapter.java

public void toJson(FlowSpecification appSpec, OutputSupplier<? extends Writer> outputSupplier)
        throws IOException {
    Writer writer = outputSupplier.getOutput();
    try {//  ww w  .  j av a2s . com
        toJson(appSpec, writer);
    } finally {
        Closeables.closeQuietly(writer);
    }
}

From source file:com.netflix.exhibitor.core.index.LogSearch.java

@Override
public void close() {
    Closeables.closeQuietly(searcher);
    Closeables.closeQuietly(reader);
    Closeables.closeQuietly(directory);
}

From source file:net.sourceforge.vaticanfetcher.model.parse.MSOffice2007Parser.java

@Override
protected final String renderText(File file, String filename) throws ParseException {
    OPCPackage pkg = null;//  w  w  w.  j  a v  a  2  s.  co  m
    try {
        pkg = OPCPackage.open(file.getPath(), PackageAccess.READ);
        return extractText(pkg);
    } catch (Exception e) {
        throw new ParseException(e);
    } finally {
        Closeables.closeQuietly(pkg);
    }
}