Example usage for com.google.common.io CharStreams toString

List of usage examples for com.google.common.io CharStreams toString

Introduction

In this page you can find the example usage for com.google.common.io CharStreams toString.

Prototype

public static String toString(Readable r) throws IOException 

Source Link

Document

Reads all characters from a Readable object into a String .

Usage

From source file:com.mckinsey.jenkins.GithubPipelineCreator.github.GithubWebhook.java

protected String getRequestPayload(StaplerRequest req) throws IOException {
    return CharStreams.toString(req.getReader());
}

From source file:com.google.api.codegen.MixedPathTestDataLocator.java

@Override
public String fetchTestData(URL url) {
    if ("file".equals(url.getProtocol())) {
        try (Reader reader = new InputStreamReader(url.openStream())) {
            return CharStreams.toString(reader);
        } catch (IOException e) {
            // Ignore, fallback to parent's implementation.
        }/*from  www. ja  v  a 2 s.  c  o m*/
    }

    return super.fetchTestData(url);
}

From source file:org.obm.push.mail.transformer.HtmlToText.java

@Override
public InputStream transform(InputStream input, Charset charset) throws IOException {
    Preconditions.checkNotNull(input);/*from  w w  w .  jav a  2  s  .  c  o m*/
    Preconditions.checkNotNull(charset);
    String htmlString = CharStreams.toString(new InputStreamReader(input, charset));
    String ppPlainText = Jsoup.clean(htmlString, "", Whitelist.none().addTags("br", "p"),
            new Document.OutputSettings().prettyPrint(true));
    String plainText = Jsoup.clean(ppPlainText, "", Whitelist.none(),
            new Document.OutputSettings().prettyPrint(false));
    String withEntities = Parser.unescapeEntities(plainText, false);
    return new ByteArrayInputStream(withEntities.getBytes(charset));
}

From source file:org.killbill.billing.plugin.meter.MeterTestSuiteWithEmbeddedDB.java

@BeforeSuite(groups = { "slow", "mysql" })
public void startMysqlBeforeTestSuite()
        throws IOException, ClassNotFoundException, SQLException, URISyntaxException {
    helper.initialize();/*from   w w w.j  a  v  a 2s.  c o m*/
    helper.start();

    final InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            return Resources.getResource("org/killbill/billing/plugin/meter/ddl.sql").openStream();
        }
    };
    final String ddl = CharStreams.toString(CharStreams.newReaderSupplier(inputSupplier, Charsets.UTF_8));
    helper.executeScript(ddl);
    helper.refreshTableNames();
}

From source file:natlab.toolkits.filehandling.GenericFile.java

public String getContents() throws IOException {
    return CharStreams.toString(getReader());
}

From source file:eu.operando.pq.PrivacyQuestionsService.java

/**
 * Load the questions from the given resource file in JAR/WAR and
 * turn then into JSON string./*from  w  w w  . ja  v a  2 s . c om*/
 * @return The list of questions held in the file.
 */
private List<Questionobject> getQuestionsFromFile(String filename) {
    InputStream fis = null;
    try {
        fis = this.getClass().getClassLoader().getResourceAsStream(filename);
        String content = CharStreams.toString(new InputStreamReader(fis, Charsets.UTF_8));
        Closeables.closeQuietly(fis);

        ObjectMapper mapper = new ObjectMapper();

        //JSON from String to Object
        return mapper.readValue(content, new TypeReference<List<Questionobject>>() {
        });

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:annis.provider.PlainTextMatchGroupProvider.java

@Override
public MatchGroup readFrom(Class<MatchGroup> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {

    String val = CharStreams.toString(new InputStreamReader(entityStream, Charsets.UTF_8));
    MatchGroup result = MatchGroup.parseString(val);
    return result;

}

From source file:de.dentrassi.pm.database.Tasks.java

private UpgradeTask loadTask(final String name) throws Exception {
    final URL entry = this.bundle.getEntry(name);

    try (final Reader r = new InputStreamReader(entry.openStream(), StandardCharsets.UTF_8)) {
        final String sql = CharStreams.toString(r);
        final String[] sqlToks = sql.split(";");

        final List<String> sqls = new LinkedList<>();

        for (final String sqlTok : sqlToks) {
            final String s = sqlTok.trim();
            if (!s.isEmpty()) {
                sqls.add(s);/*from   ww w  .  ja  v  a2  s. co  m*/
            }
        }

        if (sqls.isEmpty()) {
            return null;
        }

        return new StatementTask(sqls);
    }
}

From source file:org.eclipse.xtext.web.servlet.HttpServiceContext.java

private String initializeParameters() {
    try {/* w  w w .  j a v  a2  s  . c  om*/
        String _xblockexpression = null;
        {
            String _contentType = this.request.getContentType();
            String[] _split = null;
            if (_contentType != null) {
                _split = _contentType.split(";(\\s*)");
            }
            final String[] contentType = _split;
            if (((contentType != null) && Objects.equal(contentType[0], "application/x-www-form-urlencoded"))) {
                String _xifexpression = null;
                if ((((contentType != null) && (contentType.length >= 2))
                        && contentType[1].startsWith("charset="))) {
                    String _get = contentType[1];
                    int _length = "charset=".length();
                    _xifexpression = _get.substring(_length);
                } else {
                    Charset _defaultCharset = Charset.defaultCharset();
                    _xifexpression = _defaultCharset.toString();
                }
                String charset = _xifexpression;
                BufferedReader _reader = this.request.getReader();
                String _string = CharStreams.toString(_reader);
                final String[] encodedParams = _string.split("&");
                for (final String param : encodedParams) {
                    {
                        final int nameEnd = param.indexOf("=");
                        if ((nameEnd > 0)) {
                            final String key = param.substring(0, nameEnd);
                            String _substring = param.substring((nameEnd + 1));
                            final String value = URLDecoder.decode(_substring, charset);
                            this.parameters.put(key, value);
                        }
                    }
                }
            }
            final Enumeration<String> paramNames = this.request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                {
                    final String name = paramNames.nextElement();
                    String _parameter = this.request.getParameter(name);
                    this.parameters.put(name, _parameter);
                }
            }
            String _xifexpression_1 = null;
            boolean _containsKey = this.parameters.containsKey(IServiceContext.SERVICE_TYPE);
            boolean _not = (!_containsKey);
            if (_not) {
                String _pathInfo = this.request.getPathInfo();
                String _substring = null;
                if (_pathInfo != null) {
                    _substring = _pathInfo.substring(1);
                }
                _xifexpression_1 = this.parameters.put(IServiceContext.SERVICE_TYPE, _substring);
            }
            _xblockexpression = _xifexpression_1;
        }
        return _xblockexpression;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}

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

@SuppressWarnings("unchecked")
@Override//from w  w  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);
    }
}