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) throws IOException 

Source Link

Document

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

Usage

From source file:com.hortonworks.registries.schemaregistry.avro.AvroSchemaProviderTest.java

@Test
public void testAvroSchemas() throws Exception {
    AvroSchemaProvider avroSchemaProvider = new AvroSchemaProvider();
    try (InputStream schemaStream = AvroSchemaProviderTest.class.getResourceAsStream("/avro/trucks.avsc");) {
        List<SchemaFieldInfo> schemaFieldInfos = avroSchemaProvider
                .generateFields(IOUtils.toString(schemaStream));
        LOG.info("generated schema fields [{}]", schemaFieldInfos);
        Assert.assertEquals(11, schemaFieldInfos.size());
    }/*from   ww  w. j av  a2s.  c  o m*/
}

From source file:com.betfair.tornjak.monitor.active.url.CheckResponseNotContains.java

public void check(URLConnection urlConnection) throws Exception {
    InputStream inputStream = urlConnection.getInputStream();
    String response = IOUtils.toString(inputStream);
    if (response.contains(errorString)) {
        throw new RuntimeException(
                String.format("Service reports [%s], which contains [%s]", response, errorString));
    }//from  w  ww  .  j  av  a  2 s  .  com
}

From source file:com.blacklocus.qs.worker.es.ElasticSearchInitializer.java

private static String getElasticSearchJson(String file) {
    try {//from   ww  w  . j a  va 2 s . com
        return IOUtils.toString(ElasticSearchQSLogService.class.getResource(file).openStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.dhenton9000.batik.sandbox.BatikExplorer.java

private void testOutBatikTranscoder() throws Exception {

    InputStream docStream = this.getClass().getResourceAsStream("/sample-docs/sample.svg");
    String svgInput = IOUtils.toString(docStream);

    D3GraphBatikTransCoder tCoder = new D3GraphBatikTransCoder(svgInput);
    InputStream isImage = tCoder.getDocument();

    File f = new File(System.getProperty("user.dir") + "/docs/out/batik_out.png");
    OutputStream outputStream = new FileOutputStream(f);

    int read = 0;
    byte[] bytes = new byte[1024];

    while ((read = isImage.read(bytes)) != -1) {
        outputStream.write(bytes, 0, read);
    }//w w w .ja  va  2 s  .  c o m

}

From source file:es.sm2.openppm.core.plugin.PluginTemplate.java

/**
 * Load template for plugin/*www.ja v  a2  s  . c om*/
 * 
 * @param templateName
 * @param data
 * @return
 * @throws IOException
 * @throws TemplateException
 */
public static String getTemplate(String templateName, HashMap<String, Object> data)
        throws IOException, TemplateException {

    Logger log = LogManager.getLog(PluginTemplate.class);

    // Print data to logger
    if (log.isDebugEnabled()) {
        log.debug("Create Template: " + templateName);

        if (data != null && !data.isEmpty()) {

            for (String key : data.keySet()) {
                log.debug("Parameter: " + key + " Value: " + data.get(key));
            }
        }
    }

    // Load template
    InputStream stream = PluginTemplate.class.getResourceAsStream(templateName);
    String teamplate = IOUtils.toString(stream);

    // Create loader
    StringTemplateLoader stringLoader = new StringTemplateLoader();
    stringLoader.putTemplate("template", teamplate);

    // Create configuration
    Configuration cfg = new Configuration();
    cfg.setTemplateLoader(stringLoader);

    Template template = cfg.getTemplate("template");

    StringWriter writer = new StringWriter();
    template.process(data, writer);

    return writer.toString();
}

From source file:au.org.ala.bhl.TaxonGrabTest.java

@Test
public void testFind1() throws Exception {

    InputStream is = TaxonGrabTest.class.getResourceAsStream("/sample1.txt");
    String data = IOUtils.toString(is);

    TaxonGrab tg = new TaxonGrab();
    List<String> names = tg.findNames(data, "english");

    for (String name : names) {
        System.err.println(name);
    }// w  w  w  .  j  av  a 2 s  .co  m

}

From source file:io.apicurio.hub.core.js.OaiCommandExecutorTest.java

@Test
public void testExecuteCommands() throws Exception {
    OaiScriptEngineFactory.debug("::testExecuteCommands::");
    OaiCommandExecutor executor = new OaiCommandExecutor();

    String document = OAI_DOC;/*  ww  w. j a  v a2  s  . c  o  m*/
    // Load the commands
    List<String> commands = new LinkedList<String>();
    commands.add(IOUtils.toString(OaiCommandExecutorTest.class.getResource("change-title.command.json")));
    commands.add(IOUtils.toString(OaiCommandExecutorTest.class.getResource("change-version.command.json")));
    commands.add(IOUtils.toString(OaiCommandExecutorTest.class.getResource("change-license.command.json")));
    commands.add(
            IOUtils.toString(OaiCommandExecutorTest.class.getResource("add-schema-definition.command.json")));

    // Invoke the executeCommands function
    String actual = executor.executeCommands(document, commands);
    //        System.out.println("---");
    //        System.out.println(actual);
    //        System.out.println("---");

    String expected = IOUtils.toString(OaiCommandExecutorTest.class.getResource("__expected.json"));

    String actualNormalized = OaiCommandExecutorTest.normalizeString((String) actual);
    String expectedNormalized = OaiCommandExecutorTest.normalizeString(expected);
    Assert.assertEquals(expectedNormalized, actualNormalized);
}

From source file:com.android.nunes.sophiamobile.gsm.GcmSender.java

public static void enviar(String msg, String token) {

    try {//from w  w  w . j a va  2s.  co m
        // Prepare JSON containing the GCM message content. What to send and where to send.
        JSONObject jGcmData = new JSONObject();
        JSONObject jData = new JSONObject();
        jData.put("message", msg.trim());
        // Where to send GCM message.
        if (token != null) {
            jGcmData.put("to", token.trim());
        } else {
            jGcmData.put("to", "/topics/global");
        }
        // What to send in GCM message.
        jGcmData.put("data", jData);

        // Create connection to send GCM Message request.
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + API_KEY);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send GCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(jGcmData.toString().getBytes());

        // Read GCM response.
        InputStream inputStream = conn.getInputStream();
        String resp = IOUtils.toString(inputStream);
        System.out.println(resp);
        System.out.println("Check your device/emulator for notification or logcat for "
                + "confirmation of the receipt of the GCM message.");
    } catch (IOException e) {
        System.out.println("Unable to send GCM message.");
        System.out.println("Please ensure that API_KEY has been replaced by the server "
                + "API key, and that the device's registration token is correct (if specified).");
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.rhc.dynamic.pipeline.utils.TestUtils.java

public static String getStringFromFile(String fileName) throws IOException {
    InputStream stream = TestUtils.class.getClassLoader().getResourceAsStream(fileName);
    if (stream == null) {
        throw new RuntimeException("could not find file: " + fileName);
    }/*  w w w .j a v  a 2  s  . c o m*/
    return IOUtils.toString(stream);
}

From source file:de.mg.stock.server.util.HttpUtil.java

public String get(String urlStr) {

    try {//from w ww .  ja v  a  2  s .  c om
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream response = connection.getInputStream();
            return IOUtils.toString(response);
        } else {
            log.log(Level.WARNING, "retrieving " + urlStr + " returned " + connection.getResponseCode());
            return null;
        }
    } catch (IOException e) {
        log.log(Level.WARNING, "retrieving " + urlStr, e);
        return null;
    }
}