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.github.fommil.netlib.generator.F2jJavadocExtractor.java

private String getRawJavadoc(Method method) throws IOException {
    String filename = method.getDeclaringClass().getCanonicalName().replace(".", "/") + ".html";

    @Cleanup/*from   w  w w  . j av  a  2s  .  c o m*/
    ZipFile zip = new ZipFile(jar);
    Enumeration<? extends ZipEntry> en = zip.entries();
    while (en.hasMoreElements()) {
        ZipEntry entry = en.nextElement();
        if (entry.getName().endsWith(filename)) {
            @Cleanup
            InputStreamReader stream = new InputStreamReader(zip.getInputStream(entry), "UTF-8");
            return CharStreams.toString(stream);
        }
    }
    return "";
}

From source file:org.apache.curator.ensemble.exhibitor.DefaultExhibitorRestClient.java

@Override
public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
    URI uri = new URI(useSsl ? "https" : "http", null, hostname, port, uriPath, null, null);
    HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
    connection.addRequestProperty("Accept", mimeType);
    Reader in = new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
    try {/*from  www .  jav a2s.c  o m*/
        return CharStreams.toString(in);
    } finally {
        CloseableUtils.closeQuietly(in);
    }
}

From source file:de.dentrassi.rpm.builder.Script.java

public String makeScriptContent() throws IOException {
    if (this.file != null && !Strings.isNullOrEmpty(this.script)) {
        throw new IllegalStateException("Script must not have 'file' and 'script' set at the same time.");
    }/*from www  .j a v  a  2  s .  c  o  m*/

    if (this.file != null) {
        try (Reader reader = new InputStreamReader(new FileInputStream(this.file), StandardCharsets.UTF_8)) {
            return CharStreams.toString(reader);
        }
    }

    return this.script;
}

From source file:io.macgyver.core.resource.provider.filesystem.FileSystemResource.java

@Override
public String getContentAsString() throws IOException {
    String text;/*from   w  ww. java 2  s.  co m*/
    try (InputStreamReader reader = new InputStreamReader(openInputStream(), Charsets.UTF_8)) {
        text = CharStreams.toString(reader);
    }
    return text;
}

From source file:com.brighttag.agathon.cassandra.AgathonConnector.java

private static String readFrom(URLConnection connection) throws IOException {
    return CharStreams.toString(new InputStreamReader(connection.getInputStream(), "UTF-8"));
}

From source file:com.google.devtools.build.lib.webstatusserver.StaticResourceHandler.java

private StaticResourceHandler(String path, String contentType, boolean absolutePath) {
    try {/*www.  j av  a  2 s. c o  m*/
        if (absolutePath) {
            InputStream resourceStream = loadFromAbsolutePath(WebStatusServerModule.class, path);
            response = CharStreams.toString(new InputStreamReader(resourceStream));

        } else {
            response = ResourceFileLoader.loadResource(WebStatusServerModule.class, path);
        }
        httpCode = 200;
    } catch (IOException e) {
        throw new IllegalArgumentException("resource " + path + " not found");
    }
    this.contentType = ImmutableList.of(contentType);
}

From source file:org.trinity.shellplugin.widget.impl.view.qt.StyledViewsPlugin.java

private void loadStylelSheet() throws IOException {
    final InputStream in = getClass().getClassLoader().getResourceAsStream(STYLESHEET_NAME);
    final String content = CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8));
    QApplication.instance().setStyleSheet(content);
}

From source file:seaclouds.matchmaker.Matchmaker.java

public Map<String, Object> match(String reqName, String reqValue, Map reqDescription)
        throws FileNotFoundException {
    // TODO Auto-generated method stub

    System.out.println("-----\nMatchmaking start");

    //TODO: ask the discoverer to find services with reqName and reqValue

    InputStream cloudInput;//  w w w . j  a  va  2s.  c om
    cloudInput = getClass().getResourceAsStream("/computeServices.yaml");

    String nuroCaseYaml = null;
    try {
        nuroCaseYaml = CharStreams.toString(new InputStreamReader(cloudInput, Charsets.UTF_8));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Closeables.closeQuietly(cloudInput);
    TOSCAYamlParser cloudModel = new TOSCAYamlParser(nuroCaseYaml);

    Map<String, Object> cloudOfferedServiceList = cloudModel.getNodeTemplates();
    Map<String, Object> suitableServiceList = new LinkedHashMap<>();
    log.info("\n" + cloudOfferedServiceList.size() + " service(s) available");

    for (Entry<String, Object> service : cloudOfferedServiceList.entrySet()) {

        String serviceName = (String) service.getKey();
        Map<String, Object> serviceDescription = (Map) service.getValue();

        //1. check if the service offers a capability equals to reqName and check if the service is of type included in reqValue
        //note: a service can offer more than a single capability, thus they are stored into a map
        Map<String, Object> capabilitiesList = (Map) serviceDescription.get("capabilities");
        String offServiceType = (String) serviceDescription.get("type");

        if (capabilitiesList.containsKey(reqName) && offServiceType.equals(reqValue)) {
            if (matchProperties(reqDescription, serviceDescription)) {
                log.info("Service suitable");
                suitableServiceList.put(serviceName, serviceDescription);
                //put serviceDescription somewhere or return its unique identifier
            } else {
                log.info("Service not suitable");
            }
        } else {
            log.info("Service not suitable");
        }
    }

    //TODO: return a map with matching services for each module/requirement

    log.info("-----\nMatchmaking end");
    return suitableServiceList;
}

From source file:google.registry.request.RequestModule.java

@Provides
@Payload//  w  w  w .  j av a  2 s.c o m
static String providePayloadAsString(HttpServletRequest req) {
    try {
        return CharStreams.toString(req.getReader());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.auraframework.modules.impl.ModulesCompilerNode.java

public ModulesCompilerData compile(File file) throws Exception {
    // executes: node .../compiler-cli.js .../compiler.js input.js output.js
    String filePath = file.getAbsolutePath();
    File output = ModulesCompilerUtil.createTempFile("out");
    List<String> command = new ArrayList<>();
    command.add(PATH_TO_NODE);/*  w w w. j  a va  2 s  .  c  o  m*/
    command.add(ModulesCompilerUtil.COMPILER_CLI_JS_PATH);
    command.add(ModulesCompilerUtil.COMPILER_JS_PATH);
    command.add(filePath);
    command.add(output.getAbsolutePath());

    Process process = new ProcessBuilder(command).redirectErrorStream(true).start();

    // can exec in current thread as stderr is redirected to stdout
    String stdout = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charsets.UTF_8));

    int exitCode = process.waitFor();
    if (exitCode != 0) {
        throw new RuntimeException(
                "ModulesCompilerNode failed for: " + filePath + "\n    exit code: " + exitCode + '\n' + stdout);
    }

    String result = new String(Files.readAllBytes(output.toPath()), Charsets.UTF_8);
    output.delete();
    // TODO: compiler metadata
    return new ModulesCompilerData(result, null, null);
}