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.eincs.decanter.utils.io.JsonResponseHandler.java

@Override
public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    String result = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));
    try {/*from www  .  ja v  a 2 s.  co  m*/
        return new JSONObject(result);
    } catch (JSONException e) {
        return null;
    }
}

From source file:io.apiman.cli.util.DeclarativeUtil.java

/**
 * Load the Declaration from the given Path, using the mapper provided.
 *
 * @param path       the Path to the declaration
 * @param mapper     the Mapper to use/*w ww .  j  a v  a2s.c  om*/
 * @param properties property placeholders to resolve
 * @return the Declaration
 */
public static Declaration loadDeclaration(Path path, ObjectMapper mapper, Map<String, String> properties) {
    try (InputStream is = Files.newInputStream(path)) {
        String fileContents = CharStreams.toString(new InputStreamReader(is));
        LOGGER.trace("Declaration file raw: {}", fileContents);

        Declaration declaration = loadDeclaration(mapper, fileContents, properties);

        // check for the presence of shared properties in the declaration
        final Map<String, String> sharedProperties = ofNullable(declaration.getShared())
                .map(SharedItems::getProperties).orElse(Collections.emptyMap());

        if (sharedProperties.size() > 0) {
            LOGGER.trace("Resolving {} shared placeholders", sharedProperties.size());
            final Map<String, String> mutableProperties = Maps.newHashMap(properties);
            mutableProperties.putAll(sharedProperties);

            // this is not very efficient, as it requires parsing the declaration twice
            declaration = loadDeclaration(mapper, fileContents, mutableProperties);
        }

        return declaration;

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

From source file:com.chalup.markdownviewer.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {//from  w  w  w.  j  a  v  a 2 s  . com
        String markdown = CharStreams.toString(new InputStreamReader(
                getContentResolver().openInputStream(getIntent().getData()), Charsets.UTF_8));
        String html = new AndDown().markdownToHtml(markdown);
        ((WebView) findViewById(R.id.webView)).loadDataWithBaseURL(getIntent().getDataString(), html,
                "text/html", null, null);
    } catch (Exception e) {
        Log.e(TAG, "File read error: " + e);
    }

    getSupportActionBar().setTitle(getIntent().getData().getLastPathSegment());
}

From source file:com.test.http.utils.ResponseUtils.java

/**
 * Loads content from local resources directories
 *
 * @param path resources directory path//from   www. j a va 2 s .  co m
 * @return Returns content of file
 */
public static String loadFromResource(String path) {
    InputStream is = ResponseUtils.class.getResourceAsStream(path);

    try {
        return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);

        return toJson(new MessageResponse(false, "invalid content"));
    }
}

From source file:com.hellblazer.tron.documentation.UrlJavaFileObject.java

UrlJavaFileObject(URL url) throws URISyntaxException {
    super(url.toURI(), Kind.SOURCE);

    try (InputStream in = url.openStream()) {
        Reader javaSourceReader = new InputStreamReader(in);
        this.fileObjectContent = CharStreams.toString(javaSourceReader);
    } catch (IOException e) {
        throw new IllegalStateException("IOException during reading JavaFileObject content...", e);
    }/*  w  w  w  . j  av  a2 s  .c  o  m*/
}

From source file:org.excalibur.core.util.Strings2.java

public static String toStringAndClose(InputStream input, Charset cs) throws IOException {
    checkNotNull(input, "input");
    checkNotNull(cs, "charset");

    InputStreamReader isr = new InputStreamReader(input, cs);

    try {//  ww  w.ja  v a2s.  com
        return CharStreams.toString(isr);
    } finally {
        closeQuietly(isr, input);
    }
}

From source file:org.n52.shetland.util.HTTP.java

public static String getAsString(URI uri) throws IOException {
    try (CloseableHttpResponse response = CLIENT.execute(new HttpGet(uri))) {
        HttpEntity entity = response.getEntity();
        String encoding = Optional.ofNullable(entity.getContentEncoding()).map(Header::getValue)
                .orElse(StandardCharsets.UTF_8.name());
        Charset charset = Charset.forName(encoding);
        try (InputStream is = entity.getContent(); Reader reader = new InputStreamReader(is, charset)) {
            return CharStreams.toString(reader);
        }//from   w ww . j ava2  s  . c  om
    }
}

From source file:com.jythonui.server.BUtil.java

public static String readFromFileInput(InputStream is) {
    try {//from www . j  a v a 2  s .  com
        return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
    } catch (IOException e) {
        errorLog(SHolder.getM().getMess(IErrorCode.ERRORCODE55, ILogMess.FILEIOEXCEPTION), e);
        return null;
    }
}

From source file:com.google.cloud.runtimes.tomcat.test.integration.CustomServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String configuration = CharStreams
            .toString(new InputStreamReader(getClass().getResourceAsStream("/custom-test-specification.json")));

    resp.setContentType("application/json");
    resp.getWriter().print(configuration);
}

From source file:net.shibboleth.idp.attribute.resolver.impl.DatabaseTestingSupport.java

protected static String ReadSqlFromFile(@Nullable String initializingSQLFile) {

    final String file = StringSupport.trimOrNull(initializingSQLFile);

    if (null == file) {
        return null;
    }/* www. jav  a  2s. co  m*/

    final InputStream is = DatabaseTestingSupport.class.getResourceAsStream(file);

    if (null == is) {
        log.warn("Could not locate SQL file called {} ", file);
        return null;
    }
    String sql;
    try {
        sql = StringSupport.trimOrNull(CharStreams.toString(new InputStreamReader(is)));
    } catch (IOException e) {
        log.warn("Could not read SQL file called {}.", file);
        return null;
    }

    if (null == sql) {
        log.warn("SQL file called {} was empty.", file);
        return null;
    }

    return sql;
}