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:org.jclouds.karaf.itests.live.JcloudsLiveTestSupport.java

/**
 * @return the IP address of the client on which this code is running.
 * @throws java.io.IOException/*w ww.j  av a  2  s  . com*/
 */
protected String getOriginatingIp() throws IOException {
    URL url = new URL("http://checkip.amazonaws.com/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    InputStream in = connection.getInputStream();
    try {
        return CharStreams.toString(new InputStreamReader(in)).trim() + "/32";
    } finally {
        in.close();
    }
}

From source file:zipkin.storage.cassandra3.Schema.java

static void applyCqlFile(String keyspace, Session session, String resource) {
    try (Reader reader = new InputStreamReader(Schema.class.getResourceAsStream(resource))) {
        for (String cmd : CharStreams.toString(reader).split(";")) {
            cmd = cmd.trim().replace(" " + DEFAULT_KEYSPACE, " " + keyspace);
            if (!cmd.isEmpty()) {
                session.execute(cmd);/*  ww w.  j  a v  a  2s. co  m*/
            }
        }
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:mx.bigdata.jcalais.rest.CalaisRestClient.java

public CalaisResponse analyze(Readable readable, CalaisConfig config) throws IOException {
    return analyze(CharStreams.toString(readable), config);
}

From source file:zotmc.collect.forge.McCollectInit.java

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
    try {/*from  ww w  . j  av  a 2s  .c  o m*/
        File dir = new File(event.getModConfigurationDirectory(), MODID);

        if (dir.exists() && !dir.isDirectory())
            dir.delete();
        dir.mkdirs();

        Joiner slash = Joiner.on('/');

        for (File f : dir.listFiles())
            try {
                String fn = f.getName();

                if (fn.toLowerCase(ENGLISH).endsWith(".js"))
                    scripts.put(fn, Files.toString(f, Charsets.UTF_8));

                else if (fn.toLowerCase(ENGLISH).endsWith(".zip")) {
                    Closer closer = Closer.create();

                    try {
                        ZipFile zf = new ZipFile(f);
                        closer.register(zf);

                        for (ZipEntry ze : Enumerable.forZipFile(zf))
                            if (ze.getName().toLowerCase(ENGLISH).endsWith(".js")) {
                                String zen = slash.join(fn, ze.getName());

                                try {
                                    scripts.put(zen, CharStreams.toString(
                                            new InputStreamReader(zf.getInputStream(ze), Charsets.UTF_8)));

                                } catch (Exception e) {
                                    FMLLog.log(ERROR, e, "[%s] An error occurred while reading a file: %s",
                                            NAME, zen);
                                }
                            }

                    } catch (Exception e) {
                        throw e;
                    } finally {
                        closer.close();
                    }
                }

            } catch (Exception e) {
                FMLLog.log(ERROR, e, "[%s] An error occurred while reading a file: %s", NAME, f.getName());
            }

    } catch (Exception e) {
        FMLLog.log(ERROR, e, "[%s] An error occurred while trying to access the setting files!", NAME);
    }

}

From source file:com.google.wave.splash.auth.oauth.OAuthRequestFactory.java

@Override
public ListenableFuture<String> makeSignedRequest(String body) {
    SessionContext session = sessionProvider.get();
    InputStream bodyStream;/*from   w ww. j  av  a  2 s. c  o m*/
    try {
        bodyStream = new ByteArrayInputStream(body.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }

    String result = null;
    try {
        InputStream output;
        if (session.isAuthenticated()) {
            output = getSignedResult(session, bodyStream);
        } else {
            output = getUnsignedResult(body, bodyStream);
        }
        result = CharStreams.toString(new InputStreamReader(output, HttpMessage.DEFAULT_CHARSET));
    } catch (URISyntaxException e) {
        LOG.warning(e.toString());
    } catch (OAuthException e) {
        LOG.warning(e.toString());
    } catch (IOException e) {
        LOG.severe(e.toString());
    }
    return Futures.immediateFuture(result);
}

From source file:com.github.nethad.clustermeister.provisioning.utils.GanymedSSHClient.java

private String readOutputStream(Session session) throws IOException {
    StreamGobbler streamGobbler = new StreamGobbler(session.getStdout());
    BufferedReader outputStream = new BufferedReader(new InputStreamReader(streamGobbler));
    return CharStreams.toString(outputStream);
}

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

public List<AccessReason> getReasonsFromFile(String filename) {
    InputStream fis = null;/* w  ww . jav a 2  s .  c om*/
    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<AccessReason>>() {
        });

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

From source file:com.google.idea.blaze.android.plugin.PluginCompatibilityEnforcer.java

@Nullable
private String readProductBuildTxt() {
    try (InputStream inputStream = getClass().getClassLoader()
            .getResourceAsStream("META-INF/product-build.txt")) {
        if (inputStream == null) {
            return null;
        }//from w w  w .java  2  s  .  c  o  m
        return CharStreams.toString(new InputStreamReader(inputStream)).trim();
    } catch (IOException e) {
        LOG.error("Could not read META-INF/product-build.txt", e);
        return null;
    }
}

From source file:eu.project.ttc.models.TermOccurrence.java

/**
 * //w w w .j ava 2 s  .co m
 * @param contextSize
 *          The number of characters before and after the occurrence.
 * @return
 * @throws IOException 
 */
public String getTextualContext(int contextSize) throws IOException {
    FileReader r = new FileReader(sourceDocument.getUrl().replaceFirst("file:", ""));
    String text = CharStreams.toString(r);
    r.close();
    int begin = Math.max(this.begin - contextSize, 0);
    int end = Math.min(this.end + contextSize, text.length());
    return TermUtils.collapseText(text.substring(begin, end));
}

From source file:com.indeed.imhotep.web.QueryMetadata.java

private static String streamToString(InputStream inputStream) {
    try {//from w ww. ja v a  2  s.  c om
        final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
        final String stringValue = CharStreams.toString(inputStreamReader);
        inputStreamReader.close();
        return stringValue;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}