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:io.servicecomb.common.rest.codec.produce.ProduceTextPlainProcessor.java

@Override
public Object doDecodeResponse(InputStream input, JavaType type) throws Exception {
    // plainTextstring?
    return IOUtils.toString(input, StandardCharsets.UTF_8);
    // TODO: /*from   w  w  w  . ja  va2s .  com*/
    //        Class<?> returnCls = type.getRawClass();
    //        if (returnCls.isPrimitive()) {
    //            // ?char
    //            if (returnCls == char.class) {
    //                return ((String)result).charAt(0);
    //            }
    //            // ?int, long, boolean
    //            return RestObjectMapper.INSTANCE.readValue((String)result, type);
    //        }
    //        else {
    //            // ?String??
    //            // ????"application/json"
    //            return returnCls.getConstructor(new Class<?>[] {String.class})
    //                    .newInstance((String)result);
    //        }
}

From source file:com.liferay.jenkins.tools.ResourceJsonGetter.java

@Override
public String getString(String file) throws Exception {
    URL fileURL = ResourceJsonGetter.class.getResource(file);

    if (fileURL == null) {
        throw new IOException(file + " not found");
    }/* w w w. j ava  2 s . c  o  m*/

    logger.debug("Retrieving resource from {}", file);

    return IOUtils.toString(fileURL.openStream(), Charset.defaultCharset());
}

From source file:ch.cyberduck.core.cryptomator.ContentReader.java

public String read(final Path file) throws BackgroundException {
    final Read read = session._getFeature(Read.class);
    final InputStream in = read.read(file, new TransferStatus(), new DisabledConnectionCallback());
    try {/*ww  w  .  j  av a 2s  .  c om*/
        return IOUtils.toString(in, "UTF-8");
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.nestlabs.sdk.SmokeCOAlarmTest.java

@Test
public void testCreateSmokeAlarmWithJacksonMapper_shouldSetAllValuesCorrectly() {
    try {// www  .j  a  v  a 2s  .  c o  m
        String json = IOUtils.toString(this.getClass().getResourceAsStream(TEST_SMOKEALARM_JSON), "utf-8");
        SmokeCOAlarm smokeCOAlarm = mapper.readValue(json, SmokeCOAlarm.class);

        assertEquals(smokeCOAlarm.getDeviceId(), "RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs");
        assertEquals(smokeCOAlarm.getSoftwareVersion(), "1.01");
        assertEquals(smokeCOAlarm.getStructureId(), "VqFabWH21nwVyd4RWgJgNb292wa7hG");
        assertEquals(smokeCOAlarm.getWhereId(), "UNCBGUnN24");
        assertEquals(smokeCOAlarm.getName(), "Hallway (upstairs)");
        assertEquals(smokeCOAlarm.getNameLong(), "Hallway Protect (upstairs)");
        assertEquals(smokeCOAlarm.isOnline(), true);
        assertEquals(smokeCOAlarm.getWhereId(), "UNCBGUnN24");
        assertEquals(smokeCOAlarm.getLocale(), "en-US");

        assertEquals(smokeCOAlarm.getBatteryHealth(), "ok");
        assertEquals(smokeCOAlarm.getCOAlarmState(), "ok");
        assertEquals(smokeCOAlarm.getSmokeAlarmState(), "ok");
        assertEquals(smokeCOAlarm.getUIColorState(), "gray");
        assertEquals(smokeCOAlarm.getIsManualTestActive(), true);
        assertEquals(smokeCOAlarm.getLastManualTestTime(), "2015-10-31T23:59:59.000Z");

    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:com.stratio.mojo.scala.crossbuild.FileRewriter.java

public void rewrite(final File file) throws IOException {
    if (!file.exists()) {
        throw new FileNotFoundException("File does not exist: " + file);
    }/* ww  w  . j a  v a 2 s .c o m*/
    backupFile(file);
    String result = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
    for (final RewriteRule rule : rules) {
        result = rule.replace(result);
    }
    IOUtils.write(result, new FileOutputStream(file), StandardCharsets.UTF_8);
}

From source file:com.google.api.ads.adwords.awreporting.server.appengine.processors.TaskHtml2PdfOverNet.java

@Test
public void test_run() throws IOException {

    String html = "<html>HOLA</html>";

    InputStream htmlSource = new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));

    MemoryReportWriter mrwHtml = MemoryReportWriter.newMemoryReportWriter();

    ReportExporterAppEngine.html2PdfOverNet(htmlSource, mrwHtml);

    IOUtils.toString(mrwHtml.getAsSource(), "UTF-8");

}

From source file:MyTest.java

@Test
public void test() throws Exception {
    InputStream input = null;/* w  w w .  j  a va2 s  . co  m*/
    InputStream expectedOutput = null;
    int i = 0;
    do {
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream("input" + String.valueOf(i));
        expectedOutput = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("output" + String.valueOf(i));

        if (input != null) {
            System.out.println("Running case #" + String.valueOf(i) + " input:");
            System.out.println(IOUtils.toString(Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("input" + String.valueOf(i)), "US-ASCII"));
            System.setIn(input);
            if (expectedOutput != null) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                PrintStream realOutput = new PrintStream(byteArrayOutputStream);
                PrintStream preservedOut = System.out;
                System.setOut(realOutput);
                long start = System.currentTimeMillis();
                MySolution.main(null);
                long end = System.currentTimeMillis();
                System.setOut(preservedOut);
                System.out.println("Output:");
                System.out.println(byteArrayOutputStream.toString("US-ASCII"));
                System.out.println("Time: " + (end - start));
                assertEquals(IOUtils.toString(expectedOutput, "US-ASCII"),
                        byteArrayOutputStream.toString("US-ASCII"));
            } else {
                MySolution.main(null);
            }
        }
        i++;
    } while (input != null);
}

From source file:com.epam.wilma.webapp.config.servlet.helper.InputStreamUtil.java

/**
 * Converts an {@link InputStream} to a string with an "UTF-8" character encoding.
 * @param inputStream the input stream to be converted
 * @return the result of the conversion//from   w  w w .ja  va 2 s.  c  o m
 * @throws IOException if an I/O error occured during the conversion
 */
public String toString(final InputStream inputStream) throws IOException {
    return IOUtils.toString(inputStream, "utf-8");
}

From source file:com.nebel_tv.content.wrapper.TestWrapper.java

private void init() {
    try {/*  w w w  .  j  a va2  s  .  c  o  m*/
        String response = IOUtils.toString(
                this.getClass().getResourceAsStream("resources/getVideoAssets_encodes.json"), "UTF-8");

        JSONObject root = new JSONObject(response);
        JSONObject item = (JSONObject) root.get("d");
        mediaItems.put(new Integer(749049), item.toString());

    } catch (IOException ex) {
        Logger.getLogger(TestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
        Logger.getLogger(TestWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.github.robozonky.common.remote.InterceptingInputStreamTest.java

@Test
void standard() throws IOException {
    final String contents = UUID.randomUUID().toString();
    final InputStream s = new ByteArrayInputStream(contents.getBytes());
    try (final InterceptingInputStream s2 = new InterceptingInputStream(s)) {
        // first check content has been intercepted
        assertThat(s2.getContents()).isEqualTo(contents);
        // then check content is still available
        assertThat(IOUtils.toString(s2, Defaults.CHARSET)).isEqualTo(contents);
    }/*from w w  w .  j a  va2 s.c o m*/
}