Example usage for org.apache.commons.io IOUtils toString

List of usage examples for org.apache.commons.io IOUtils toString

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toString.

Prototype

public static String toString(byte[] input, String encoding) throws IOException 

Source Link

Document

Get the contents of a byte[] as a String using the specified character encoding.

Usage

From source file:com.linkedin.flashback.factory.RecordedHttpBodyFactory.java

/**
 * Given content type, charset, construct concrete {@link com.linkedin.flashback.serializable.RecordedByteHttpBody}
 * @param contentType http body content type
 * @param contentEncoding http body content encoding
 * @param inputStream input stream from http request/response
 * @param charset charset//from w w  w. j av  a 2s  .  co  m
 * @return concrete {@link com.linkedin.flashback.serializable.RecordedHttpBody}
 *
 * */
public static RecordedHttpBody create(String contentType, String contentEncoding, final InputStream inputStream,
        final String charset) throws IOException {

    if (HttpUtilities.isCompressedContentEncoding(contentEncoding)) {
        return new RecordedEncodedHttpBody(IOUtils.toByteArray(inputStream), contentEncoding, charset,
                contentType);
    } else if (HttpUtilities.isTextContentType(contentType)) {
        return new RecordedStringHttpBody(IOUtils.toString(inputStream, charset));
    } else {
        return new RecordedByteHttpBody(IOUtils.toByteArray(inputStream));
    }
}

From source file:com.sikulix.scripting.SXClient.java

private static void doStopServer() throws UnirestException, IOException {
    String url = urlBase + "/stop";
    GetRequest request = Unirest.get(url);
    try {//from  ww w.  j a v a  2  s . c o m
        HttpResponse<?> response = request.asString();
        InputStream respIS = response.getRawBody();
        String respText = IOUtils.toString(respIS, "UTF-8");
        log.trace("%s", respText);
    } catch (Exception ex) {
        log.error("%s", ex.getMessage());
    }
}

From source file:ec.edu.ucuenca.dcc.sld.ConfigInfo.java

private ConfigInfo() {

    InputStream resourceAsStream = this.getClass().getResourceAsStream("/config.json");
    String theString;//from  w  w  w .ja  v  a 2 s .c o  m
    try {
        theString = IOUtils.toString(resourceAsStream, Charset.defaultCharset().toString());
        config = JSON.parse(theString).getAsObject().get("Config").getAsObject();
    } catch (IOException ex) {
        Logger.getLogger(ConfigInfo.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:co.turnus.ui.util.HtmlUtil.java

public static String fileToString(String htmlResourceName) {
    try {/* w w w .j ava 2 s  .c om*/
        InputStream is = new HtmlUtil().getClass().getClassLoader().getResource("/html/" + htmlResourceName)
                .openStream();
        return IOUtils.toString(is, "UTF-8");
    } catch (Exception e) {
        TurnusLogger.warning("The html resource %s cannot be loaded", htmlResourceName);
    }

    return "";
}

From source file:ivorius.ivtoolkit.rendering.IvShaderInstanceMC.java

@Deprecated
/**/* w w w  . j  a  v a  2 s. c o  m*/
 * Use IvShaderLoaderMC instead.
 */
public static void trySettingUpShader(IvShaderInstance shaderInstance, ResourceLocation vertexShader,
        ResourceLocation fragmentShader, String utils) {
    String vShader = null;
    String fShader = null;

    IResource vShaderRes = null;
    IResource fShaderRes = null;

    try {
        vShaderRes = Minecraft.getMinecraft().getResourceManager().getResource(vertexShader);
        fShaderRes = Minecraft.getMinecraft().getResourceManager().getResource(fragmentShader);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (vShaderRes != null && fShaderRes != null) {
        try {
            vShader = IOUtils.toString(vShaderRes.getInputStream(), Charsets.UTF_8);
            fShader = IOUtils.toString(fShaderRes.getInputStream(), Charsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (vShader != null && fShader != null) {
            if (utils != null) {
                vShader = addUtils(vShader, utils);
                fShader = addUtils(fShader, utils);
            }

            shaderInstance.trySettingUpShader(vShader, fShader);
        }
    }
}

From source file:abfab3d.opencl.ProgramLoader.java

public static CLProgram load(CLContext context, String[] filename, boolean unroll) throws IOException {
    StringBuilder bldr = new StringBuilder();
    int len = filename.length;

    for (int i = 0; i < len; i++) {
        InputStream is = getStreamFor(filename[i]);
        String st = IOUtils.toString(is, "UTF-8");
        if (unroll) {
            st = unrollIncludes(st);//from   w ww  .j  a  v  a2 s  .c o  m
        }
        bldr.append("// Include: ");
        bldr.append(filename[i]);
        bldr.append("\n");
        bldr.append(st);
        bldr.append("\n");
        bldr.append("// End Include: ");
        bldr.append(filename[i]);
        bldr.append("\n");

    }
    return context.createProgram(bldr.toString());
}

From source file:com.ibm.watson.app.qaclassifier.tools.FileUtils.java

public static String loadFromFilesystemOrClasspath(String path, Charset charset) throws IOException {
    String contents = null;/*from ww w. j a v a 2  s.  c  om*/

    File file = new File(path);
    if (file.exists()) {
        contents = org.apache.commons.io.FileUtils.readFileToString(file, charset);
        if (contents != null) {
            System.out.println(MessageKey.AQWQAC10100I_reading_from_file_1.getMessage(file.getAbsolutePath())
                    .getFormattedMessage());
        }
    }

    if (contents == null) {
        // Try the classpath
        try (InputStream stream = FileUtils.class.getResourceAsStream(path)) {
            contents = IOUtils.toString(stream, charset);
            if (contents != null) {
                System.out.println(MessageKey.AQWQAC10101I_reading_from_classpath_1.getMessage(path)
                        .getFormattedMessage());
            }
        }
    }

    if (contents == null) {
        throw new IllegalArgumentException(MessageKey.AQWQAC14101E_could_not_find_path_on_classpath_1
                .getMessage(path).getFormattedMessage());
    }

    return contents;
}

From source file:com.yoncabt.ebr.util.ResultSetDeserializer.java

public ResultSetDeserializer(InputStream is) throws IOException {
    jo = new JSONObject(IOUtils.toString(is, "utf-8"));
    readTypes();
}

From source file:apidemo.APIDemo.java

public static String sendPost(String url, byte[] data)
        throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException {
    //        logger.info("url: " + url);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new ByteArrayEntity(data));

    try (CloseableHttpResponse res = httpclient.execute(httpPost)) {
        HttpEntity entity = res.getEntity();

        InputStream inputStream = entity.getContent();
        String sResponse = IOUtils.toString(inputStream, "UTF-8");

        return sResponse;
    }/*  w ww  . j ava 2s . co  m*/
}

From source file:gobblin.data.management.ConversionHiveTestUtils.java

public static String readQueryFromFile(String directory, String filename) throws IOException {
    InputStream is = ConversionHiveTestUtils.class.getClassLoader()
            .getResourceAsStream(StringUtils.removeEnd(directory, Path.SEPARATOR) + Path.SEPARATOR + filename);

    return IOUtils.toString(is, "UTF-8");
}