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.adguard.compiler.UrlUtils.java

public static String downloadString(URL url, String encoding, String userAgent) throws IOException {

    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {//from   w  w w  . j a  v a  2 s . c  om
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", userAgent);
        connection.connect();
        inputStream = connection.getInputStream();
        return IOUtils.toString(inputStream, encoding);
    } finally {
        IOUtils.closeQuietly(inputStream);
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:io.stallion.tests.unit.TransformJavascriptMultiline.java

@Test
public void testTransform() throws IOException {
    String source = IOUtils.toString(getClass().getResource("/samples/javascript-with-multiline-string.js"),
            "UTF-8");
    SqlMigrationAction action = new SqlMigrationAction();
    String transformed = action.transformJavascript(source);
    Log.info("Transformed {0}", transformed);
    assertEquals(//from  www  .  j av  a 2  s .  com
            "print(\"the start\")\n" + "\n" + "thing.run(\"a\\nline 'one'\\nline 'two'\\nline three\\n  z\");\n"
                    + "\n" + "print(\"The end of the script\")",
            transformed);
}

From source file:com.oskm.support.remote.httpclient.parser.ToJSONObjectResponseParser.java

protected JSONObject jSONObjectFromObject(InputStream responseBody) throws IOException {
    return JSONObject.fromObject(IOUtils.toString(responseBody, "utf-8"));
}

From source file:cool.pandora.modeller.SparqlUpdateReaderTest.java

@Test
public void sparqlUpdateReaderTest() {

    final InputStream requestBodyStream = SparqlUpdateReaderTest.class.getResourceAsStream("/data/res.update");

    try {//w  ww .  j  a v a2 s . co m
        final String requestBody = IOUtils.toString(requestBodyStream, UTF_8);
        System.out.println();
        System.out.println("#### ---- Write as application/sparql-update");
        System.out.println(requestBody);
        InputStream is = getClass().getResourceAsStream("/requestBody_out.txt");
        String out = TestUtils.streamToString(is);
        assertEquals(requestBody, out);
    } catch (final IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:com.github.aistomin.xml.XmlResource.java

@Override
public String content() throws Exception {
    return IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(this.name),
            StandardCharsets.UTF_8);
}

From source file:de.elomagic.vaadin.addon.speechrecognition.SpeechRecognitionEventDataTest.java

private String readJsonFile2() throws IOException {
    return IOUtils.toString(getClass().getClassLoader().getResourceAsStream("events/event2.json"), "utf-8");
}

From source file:info.dolezel.fatrat.plugins.util.FileUtils.java

/**
 * Reads the whole file and returns it as a String.
 * The UTF-8 encoding is assumed.//from   www  .ja  v  a  2 s  .co m
 * @param file The file path.
 * @return The file contents or <code>null</code> if the operation failed.
 */
public static String fileReadAll(String file) {
    FileInputStream fis = null;

    try {
        fis = new FileInputStream(file);

        return IOUtils.toString(fis, "UTF-8");
    } catch (IOException ex) {
        return null;
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:edu.unc.lib.dl.ui.util.StringFormatUtilTest.java

@Test
public void truncateText() throws IOException {
    String abstractText = IOUtils.toString(this.getClass().getResourceAsStream("multilineAbstract.txt"),
            "UTF-8");

    String truncated = StringFormatUtil.truncateText(abstractText, 100);
    assertEquals(truncated.length(), 99);

    abstractText = "t" + abstractText;
    truncated = StringFormatUtil.truncateText(abstractText, 100);
    assertEquals(truncated.length(), 100);

    try {/*from ww w.j av a 2  s.  co  m*/
        truncated = StringFormatUtil.truncateText(abstractText, -1);
        fail();
    } catch (IndexOutOfBoundsException e) {
    }

    truncated = StringFormatUtil.truncateText(abstractText, abstractText.length() + 10);
    assertEquals(truncated.length(), abstractText.length());

    truncated = StringFormatUtil.truncateText(null, 100);
    assertNull(truncated);
}

From source file:com.brainlounge.zooterrain.netty.WebSocketServerJSPage.java

public WebSocketServerJSPage() throws IOException {
    final InputStream resourceAsStream = WebSocketServerJSPage.class.getResourceAsStream("/hexdump.js");
    content = IOUtils.toString(resourceAsStream, "UTF-8");
}

From source file:com.mycorp.SwaggerArquillianTest.java

@RunAsClient
@Test//from  w  ww  .  ja  v  a2 s . co m
public void testEndpoints() throws Exception {
    String content = IOUtils.toString(new URL("http://127.0.0.1:8080/swagger.json"), Charset.forName("UTF-8"));
    assertThat(content).contains("\"tags\":[{\"name\":\"theapp\"}]");
}