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.cloudera.oryx.contrib.flume.test.OryxEventParserTest.java

@Test
public void jsonEventParserTest() throws IOException {
    String record;/* w w w.  ja  va 2 s .  co  m*/
    FileInputStream fis = new FileInputStream("src/test/resources/example.json");
    try {
        record = IOUtils.toString(fis);
    } finally {
        fis.close();
    }

    Event event = EventBuilder.withBody(record.getBytes());
    List<String> batch = Lists.newArrayList();

    List<List<String>> fields = Lists.newArrayList();
    fields.add(Lists.newArrayList("$.user.id", "$.product.product-code", "1.0"));
    fields.add(Lists.newArrayList("$.user.id", "$.product.search-terms"));

    OryxEventParser ep = new OryxJSONEventParser();
    ep.parseEvent(event, fields, batch);

    Assert.assertEquals("fd156752df3d,488589e7c994,1.0", batch.get(0));
    Assert.assertEquals("fd156752df3d,natural running", batch.get(1));
}

From source file:com.sap.research.connectivity.gw.GwUtils.java

public static void createClassFileFromTemplate(String packageName, String subFolder, String templateFileName,
        String targetFileName, Map<String, String> replacements, FileManager fileManager,
        Class<?> loadingClass) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    String targetFile = subFolder + SEPARATOR + targetFileName;
    MutableFile mutableFile = fileManager.exists(targetFile) ? fileManager.updateFile(targetFile)
            : fileManager.createFile(targetFile);
    try {//from   w  w  w . j  ava2  s.  c om
        inputStream = FileUtils.getInputStream(loadingClass, templateFileName);
        outputStream = mutableFile.getOutputStream();
        String inputStreamString = IOUtils.toString(inputStream);

        for (Map.Entry<String, String> entry : replacements.entrySet()) {
            inputStreamString = inputStreamString.replace(entry.getKey(), entry.getValue());
        }
        //System.out.println(inputStreamString);
        inputStream = IOUtils.toInputStream(inputStreamString);
        IOUtils.copy(inputStream, outputStream);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.alibaba.stonelab.toolkit.autoconf.AutoconfTemplate.java

private void parser() throws Exception {
    InputStream in = new FileInputStream(file);
    String content = IOUtils.toString(in);
    IOUtils.closeQuietly(in);// w w  w.jav  a 2  s . c  o m
    props.addAll(AutoconfUtil.parsePlaceholder(content));
}

From source file:com.ikanow.aleph2.analytics.spark.utils.SparkPyTechnologyUtils.java

/** Create a zip containing the Aleph2 driver
 * @param bucket/*  www.j  ava  2 s .  com*/
 * @param job
 * @throws IOException
 */
public static String writeAleph2DriverZip(final String signature) throws IOException {
    final String tmp_dir = System.getProperty("java.io.tmpdir");
    final String filename = tmp_dir + "/aleph2_driver_py_" + signature + ".zip";

    final InputStream io_stream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("aleph2_driver.py");
    final FileOutputStream fout = new FileOutputStream(filename);
    final ZipOutputStream zout = new ZipOutputStream(fout);
    final ZipEntry ze = new ZipEntry("aleph2_driver.py");
    zout.putNextEntry(ze);
    zout.write(IOUtils.toString(io_stream).getBytes());
    zout.closeEntry();
    zout.close();

    return filename;
}

From source file:com.machinelinking.converter.ScriptableConverterTest.java

@Test
public void testConvert() throws IOException, ConverterException, ScriptableFactoryException {
    final String script = IOUtils
            .toString(this.getClass().getResourceAsStream("scriptable-converter-test1.py"));
    final ScriptableConverter converter = ScriptableConverterFactory.getInstance().createConverter(script);
    final ByteArrayOutputStream serializerBAOS = new ByteArrayOutputStream();
    final JSONSerializer serializer = new JSONSerializer(serializerBAOS);
    final ByteArrayOutputStream writerBAOS = new ByteArrayOutputStream();
    final Writer writer = new BufferedWriter(new OutputStreamWriter(writerBAOS));
    converter.convertData(JSONUtils// www  . j av  a2s  . c  om
            .parseJSONAsMap("{\"@type\":\"reference\",\"label\":\"List of Nobel laureates in Physics\","
                    + "\"content\":{\"@an0\":\"1921\"}}"),
            serializer, writer);
    serializer.close();
    writer.close();
    Assert.assertEquals(serializerBAOS.toString(), "{\"link\":\"List of Nobel laureates in Physics 1921\"}");
    Assert.assertEquals(writerBAOS.toString(), "<a href=\"List of Nobel laureates in Physics\">1921</a>");
}

From source file:de.tudarmstadt.ukp.dkpro.core.RSTParseOutputReaderTest.java

@Test
public void testReadParseOutput() throws Exception {
    InputStream stream = this.getClass().getClassLoader().getResourceAsStream("parse1.txt");
    String s = IOUtils.toString(stream);

    RSTParseOutputReader reader = new RSTParseOutputReader();

    // mock jcas/* w ww .  j  av  a  2 s.co m*/
    JCas jCas = JCasFactory.createJCas();

    InputStream textStream = this.getClass().getClassLoader().getResourceAsStream("roomfordebate1.txt");
    String text = IOUtils.toString(textStream);

    jCas.setDocumentText(text);
    jCas.setDocumentLanguage("en");

    // tokenize
    SimplePipeline.runPipeline(jCas, AnalysisEngineFactory.createEngineDescription(StanfordSegmenter.class));

    reader.readParseOutput(s, jCas);
}

From source file:jatoo.weather.openweathermap.JaTooWeatherOpenWeatherMapTest.java

@Test
public void test1() throws Throwable {

    JaTooWeatherService service = new AbstractJaTooWeatherOpenWeatherMap() {
        @Override//from ww w .  j a  v a  2 s .co  m
        protected String getJSONResponse(String url) throws IOException {
            return IOUtils.toString(JaTooWeatherOpenWeatherMapTest.class.getResource("response1.json"));
        }
    };

    JaTooWeather weather = service.getWeather(CITY, false);

    Assert.assertEquals("broken clouds", weather.getDescriptionText());
    Assert.assertEquals("Temperature: 14.41 C", weather.getTemperatureTextFull());
    Assert.assertEquals("Humidity: 77 %", weather.getHumidityTextFull());
    Assert.assertEquals("Pressure: 1023.89 hPa", weather.getPressureTextFull());
    Assert.assertEquals("Wind: 2.42 meters/sec", weather.getWindTextFull());
    Assert.assertEquals("Wind Direction: 9.50104 degrees (meteorological)", weather.getWindDirectionTextFull());
    Assert.assertEquals("Clouds: 56 %", weather.getCloudsTextFull());
    Assert.assertEquals("Rain: -", weather.getRainTextFull());
    Assert.assertEquals("Snow: -", weather.getSnowTextFull());
    Assert.assertEquals("Sunrise: 07:21", weather.getSunriseTextFull());
    Assert.assertEquals("Sunset: 18:43", weather.getSunsetTextFull());
}

From source file:io.lightlink.translator.JSBeautifyPostProcessor.java

@Override
public String process(String script) throws IOException {
    String beautifier = IOUtils.toString(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("io/lightlink/translator/beautify.js"));

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    try {/*from  w  w w.j a  v  a  2s .co  m*/
        engine.eval(beautifier);
        engine.getBindings(ScriptContext.ENGINE_SCOPE).put("$originalScript", script);
        return engine.eval("js_beautify($originalScript, {})") + "";
    } catch (ScriptException e) {
        LOG.error(e.toString(), e);
        return script;
    }

}

From source file:io.sidecar.event.EventWithIdsJsonValidationTest.java

@BeforeMethod
public void loadValidEventJsonAsString() throws Exception {
    String validEvent = IOUtils.toString(this.getClass().getResource("/proposed_event.json"));
    ObjectNode eventAsJson = (ObjectNode) mapper.readTree(validEvent);
    eventUnderTest = mapper.readValue(eventAsJson.traverse(), Event.class);

    eventWithAppIdAsJson = JsonNodeFactory.instance.objectNode();
    eventWithAppIdAsJson.set("event", eventAsJson);
    eventWithAppIdAsJson.put("appId", appIdUnderTest.toString());
    eventWithAppIdAsJson.put("orgId", appIdUnderTest.toString());
    eventWithAppIdAsJson.put("userId", appIdUnderTest.toString());

}

From source file:hudson.plugins.sonar.template.SimpleTemplate.java

public SimpleTemplate(String path) {
    InputStream stream = null;/*  ww  w.j  a v a 2  s.com*/
    try {
        stream = getClass().getClassLoader().getResourceAsStream(path);
        if (stream == null) {
            throw new TemplateException("Template not found in classloader: " + path);
        }
        template = IOUtils.toString(stream);
    } catch (IOException e) {
        throw new TemplateException("Could not read template: " + path, e);
    } finally {
        closeQuietly(stream);
    }
}