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.kairosdb.testing.JsonResponse.java

public JsonResponse(HttpResponse response) throws IOException {
    Header[] allHeaders = response.getAllHeaders();
    for (Header header : allHeaders) {
        headers.put(header.getName(), header.getValue());
    }/*from w  w w .  ja  va  2  s.c  om*/

    statusString = response.getStatusLine().getReasonPhrase();
    statusCode = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();

    if (entity != null)
        json = CharStreams.toString(new InputStreamReader(entity.getContent(), Charsets.UTF_8));
}

From source file:util.HttpUtils.java

/**
 * Makes a HTTP Post.//from  ww w .  j a va2  s.  c o  m
 * @param params
 * @return Response string
 */
public static String postRequest(String url, String params) {
    try {
        URL urlObj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(getDefaultProxy());
        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
        writer.write(params);
        writer.close();

        return CharStreams.toString(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
    } catch (Exception e) {
        return "ERROR: " + Throwables.getStackTraceAsString(e);
    }
}

From source file:reflex.DefaultReflexIOHandler.java

public static String getStreamAsString(InputStream is, String encoding) throws FileNotFoundException {
    String ret = null;//from w ww  .  j  a v a  2  s.c  om
    InputStreamReader isr = null;
    try {
        isr = new InputStreamReader(is, encoding);
        ret = CharStreams.toString(isr);
    } catch (IOException e) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error with Reflex stream",
                e);
    } finally {
        try {
            if (isr != null) {
                isr.close();
            }
            is.close();
        } catch (IOException e) {
            throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error closing stream",
                    e);
        }
    }
    return ret;
}

From source file:com.bennavetta.appsite.processor.scripting.ScriptingProcessor.java

@Override
public void process(String path, MediaType type, InputStream in, OutputStream out, Request request,
        Response response) throws Exception {
    Charset encoding = (type.charset().isPresent() ? type.charset().get() : Charsets.UTF_8);
    String scriptText = null;/* w  w w  .j av a2  s.c o  m*/
    try (Reader reader = new InputStreamReader(in, encoding)) {
        scriptText = CharStreams.toString(reader);
    }
    Script script = factory.getScript(scriptText);
    script.run(request, response, out);
}

From source file:br.com.objectos.blog.PostTemplateLoaderGuice.java

@Override
public PostTemplate loadFromFile(File template) throws BlogException {
    try {/*www  . j  av  a  2s . c om*/

        BufferedReader reader = Files.newReader(template, Charsets.UTF_8);
        String _template = CharStreams.toString(reader);
        return load(_template);

    } catch (FileNotFoundException e) {
        throw Throwables.propagate(e);

    } catch (IOException e) {
        throw new PostTemplateException(e);

    }
}

From source file:org.jf.smalidea.errorReporting.GithubFeedbackTask.java

private static String getToken() {
    InputStream stream = GithubFeedbackTask.class.getClassLoader().getResourceAsStream("token");
    if (stream == null) {
        return null;
    }//from  w  ww.ja  v  a 2  s.co m
    try {
        return CharStreams.toString(new InputStreamReader(stream, "UTF-8"));
    } catch (IOException ex) {
        return null;
    }
}

From source file:org.eclipse.che.mail.template.ST.STTemplateProcessorImpl.java

private String resolve(String template) throws TemplateNotFoundException {
    try (Reader reader = new InputStreamReader(IoUtil.getResource(template))) {
        return CharStreams.toString(reader);
    } catch (IOException e) {
        throw new TemplateNotFoundException(e.getMessage(), e);
    }/*  w  w w .java2s  .  c om*/
}

From source file:edu.umb.cs.source.std.Utils.java

public static Output compile(String src, String outer) {
    // remove the package (to avoid having to create dirs)
    if (src.startsWith("package")) {
        int n = 0;
        for (; n < src.length(); ++n) {
            if (src.charAt(n) == ';') {
                src = src.substring(n + 1);
                break;
            }/*  www . j  a va 2s.co m*/
        }
    }
    try {
        int retcode = 0;
        String fn = outer + ".java";

        // write the source to file
        FileWriter fstream = new FileWriter(tempDir.getAbsolutePath() + "/" + fn);
        BufferedWriter outFile = new BufferedWriter(fstream);
        outFile.write(src);
        //Close the output stream
        outFile.close();

        // compile the source
        Process p = Runtime.getRuntime().exec("javac " + fn, null, tempDir);
        try {
            p.waitFor();
            retcode = p.exitValue();
        } catch (InterruptedException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            return new Output(ex.getMessage(), true);
        }

        if (retcode != 0) {
            // cannot compile!
            return new Output(CharStreams.toString(new InputStreamReader(p.getErrorStream())), true);
        }

        // execute the source
        p = Runtime.getRuntime().exec("java " + outer, null, tempDir);
        try {
            p.waitFor();
            retcode = p.exitValue();
        } catch (InterruptedException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            return new Output(ex.getMessage(), true);
        }

        if (retcode != 0) {
            // something wrong!
            return new Output(CharStreams.toString(new InputStreamReader(p.getErrorStream())), true);
        }

        // otherwise, return the output
        String ret = CharStreams.toString(new InputStreamReader(p.getInputStream()));

        // TODO: clean up the temp files ?
        return new Output(ret, false);
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        return new Output(ex.getMessage(), true);
    }
}

From source file:io.airlift.command.OutputProcessor.java

public void start() {
    outputFuture = submit(executor, new Callable<String>() {
        @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
        @Override//from  w w  w  .  j  ava  2 s  .co  m
        public String call() throws IOException {
            return CharStreams.toString(new InputStreamReader(inputStream, UTF_8));
        }
    });
}

From source file:com.vsct.supervision.notification.exception.SeyrenResponseErrorHandler.java

@Override
public void handleError(ClientHttpResponse response) throws IOException {
    String seyrenResponseBody;/*from w  w w .  j a  v  a 2  s . com*/
    LOGGER.debug("Response : {} {}", response.getStatusCode(), response.getStatusText());
    if (response.getBody() != null) {
        seyrenResponseBody = CharStreams.toString(new InputStreamReader(response.getBody(), "UTF-8"));
    } else {
        seyrenResponseBody = "Response whithout body";
    }
    CerebroException exception = new CerebroException(ErrorCode.SEYREN_ERROR, seyrenResponseBody);
    throw exception;
}