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:bad.robot.pingpong.HttpResponseContentMatcher.java

@Override
public boolean matchesSafely(HttpResponse response) {
    try {/*from   w  ww .j  a  v a  2 s. c  o m*/
        return content.equals(IOUtils.toString(response.getEntity().getContent()));
    } catch (IOException e) {
        return false;
    }
}

From source file:Authentication.DinserverAuth.java

public static boolean Login(String nick, String pass, Socket socket) throws IOException {
    boolean logged = false;

    JSONObject authRequest = new JSONObject();
    authRequest.put("username", nick);
    authRequest.put("auth_id", pass);

    String query = "http://127.0.0.1:5000/token";

    URL url = new URL(query);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5000);/* w  w w .  java 2  s. c  o  m*/
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");

    OutputStream os = conn.getOutputStream();
    os.write(authRequest.toString().getBytes(Charset.forName("UTF-8")));
    os.close();
    String output = new String();
    StringBuilder sb = new StringBuilder();
    int HttpResult = conn.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK) {
        output = IOUtils.toString(new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8")));
    } else {
        System.out.println(conn.getResponseMessage());
    }

    JSONObject jsonObject = new JSONObject(output);
    logged = jsonObject.getBoolean("ok");

    conn.disconnect();

    if (logged) {
        if (DinserverMaster.addUser(nick, socket)) {
            System.out.println("User " + nick + " logged in");
        } else {
            logged = false;
        }
    }
    return logged;
}

From source file:com.badlogicgames.packr.Packr.java

public static void main(String[] args) throws IOException {
    if (args.length > 1) {
        Map<String, String> arguments = parseArgs(args);
        Config config = new Config();
        config.platform = Platform.valueOf(arguments.get("platform"));
        config.jdk = arguments.get("jdk");
        config.executable = arguments.get("executable");
        config.jar = arguments.get("appjar");
        config.mainClass = arguments.get("mainclass");
        if (arguments.get("vmargs") != null) {
            config.vmArgs = Arrays.asList(arguments.get("vmargs").split(";"));
        }//w ww.j  av  a 2 s . c  o m
        config.outDir = arguments.get("outdir");
        if (arguments.get("minimizejre") != null) {
            if (new File(arguments.get("minimizejre")).exists()) {
                config.minimizeJre = FileUtils.readFileToString(new File(arguments.get("minimizejre")))
                        .split("\r?\n");
            } else {
                InputStream in = Packr.class.getResourceAsStream("/minimize/" + arguments.get("minimizejre"));
                if (in != null) {
                    config.minimizeJre = IOUtils.toString(in).split("\r?\n");
                    in.close();
                } else {
                    config.minimizeJre = new String[0];
                }
            }
        }
        if (arguments.get("resources") != null)
            config.resources = Arrays.asList(arguments.get("resources").split(";"));
        new Packr().pack(config);
    } else {
        if (args.length == 0) {
            printHelp();
        } else {
            JsonObject json = JsonObject.readFrom(FileUtils.readFileToString(new File(args[0])));
            Config config = new Config();
            config.platform = Platform.valueOf(json.get("platform").asString());
            config.jdk = json.get("jdk").asString();
            config.executable = json.get("executable").asString();
            config.jar = json.get("appjar").asString();
            config.mainClass = json.get("mainclass").asString();
            if (json.get("vmargs") != null) {
                for (JsonValue val : json.get("vmargs").asArray()) {
                    config.vmArgs.add(val.asString());
                }
            }
            config.outDir = json.get("outdir").asString();
            if (json.get("minimizejre") != null) {
                if (new File(json.get("minimizejre").asString()).exists()) {
                    config.minimizeJre = FileUtils
                            .readFileToString(new File(json.get("minimizejre").asString())).split("\r?\n");
                } else {
                    InputStream in = Packr.class.getResourceAsStream("/minimize/" + json.get("minimizejre"));
                    if (in != null) {
                        config.minimizeJre = IOUtils.toString(in).split("\r?\n");
                        in.close();
                    } else {
                        config.minimizeJre = new String[0];
                    }
                }
            }
            if (json.get("resources") != null) {
                config.resources = toStringArray(json.get("resources").asArray());
            }
            new Packr().pack(config);
        }
    }
}

From source file:com.ibm.sbt.services.client.base.transformers.AbstractBaseTransformer.java

protected String getTemplateContent(String templatepath) throws TransformerException {
    try {//  w  w w  . j av a  2s  .  com
        InputStream fisTargetFile = AbstractBaseTransformer.class.getResourceAsStream(templatepath);
        String targetFileStr = IOUtils.toString(fisTargetFile);
        return targetFileStr;
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}

From source file:com.photon.phresco.service.SampleService.java

@GET
@Produces({ MediaType.APPLICATION_JSON })
public String getConfig() {
    String jsonString = "";
    try {//from   w w  w  .  j  a  va  2 s  . co  m
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("sample.json");
        jsonString = IOUtils.toString(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return jsonString;
}

From source file:it.polimi.modaclouds.monitoring.monitoring_manager.server.ActionsExecutorTest.java

@Test
public void shouldSerializeMonitoringDatum() throws IOException {
    InputStream jsonStream = getResourceAsStream("MonitoringDatumRestCallAction.json");
    String json = IOUtils.toString(jsonStream);
    List<MonitoringDatum> data = ActionsExecutorServer.jsonToMonitoringDatum(json);
    assertNotNull(data);//from  w  w w  . j  a va  2  s  .  com
    assertTrue(data.size() == 1);
}

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

@Test
public void normalizeTest1() throws Exception {
    InputStream is = TaxonGrabTest.class.getResourceAsStream("/sample2.txt");
    String data = IOUtils.toString(is);

    String n = WordLists.normalizeText(data);
    System.err.println(n);//w ww .ja  va2s  . c om

}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLRenderer.java

public String render(String schemaLocation) {
    String lexmlManipulation = null;
    try {/*  w w  w.j a  v a2s.c  o  m*/
        InputStream template = getClass().getResourceAsStream("/lexml-templates/Norma.xml");
        try {
            lexmlManipulation = IOUtils.toString(template).replace("{localDataFecho}",
                    lexMLParser.getDataLocalFecho());
            String listAssinatura = "";

            for (String assinatura : lexMLParser.getAssinatura()) {
                listAssinatura += "<NomePessoa>" + assinatura.trim() + "</NomePessoa>\n";
            }
            lexmlManipulation = lexmlManipulation.replace("{schemaLocation}", schemaLocation);
            lexmlManipulation = lexmlManipulation.replace("{lista:assinatura}", listAssinatura);
            lexmlManipulation = lexmlManipulation.replace("{articulacao}", lexMLParser.getArticulacao());
            lexmlManipulation = lexmlManipulation.replace("{epigrafe}", lexMLParser.getEpigrafe());

            lexmlManipulation = lexmlManipulation.replace("{ementa}",
                    "Denomina Ponte Antnio Conselheiro a ponte sobre o "
                            + "Rio So Francisco, localizada na Rodovia BR-116, na divisa entre os "
                            + "Estados da Bahia e de Pernambuco.");
            lexmlManipulation = lexmlManipulation.replace("{preambulo}",
                    "<p>A PRESIDENTA DA REPBLICA</p><p>Fao saber que o Congresso Nacional "
                            + "decreta e eu sanciono a seguinte Lei:</p>");
            lexmlManipulation = lexmlManipulation.replace("{urn}", "urn:lex:br:federal:lei:2014-10-28;13042");
        } finally {
            template.close();
        }

    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return LexMLUtil.formatLexML(lexmlManipulation);
}

From source file:com.savoirtech.json.JsonComparatorIT.java

@Test
public void testCompare01() throws Exception {
    String actual01 = IOUtils.toString(JsonComparatorIT.class.getResourceAsStream("spec01.json"));
    String rules01 = IOUtils.toString(JsonComparatorIT.class.getResourceAsStream("rules01.json"));

    JsonComparatorResult result = this.comparator.compare(rules01, actual01);

    assertTrue("expect match; error=" + result.getErrorMessage(), result.isMatch());
    assertNull(result.getErrorPath());/* w  w w .j  a v  a  2 s  . co m*/
}

From source file:hmock.http.impl.DefaultResponseSpecTest.java

@Test
public void testReponseSpecGenerateCorrectResponse() throws Exception {

    DefaultResponseSpec responseSpec = new DefaultResponseSpec(null);

    responseSpec.body("hello world").status(400).header("header1", "value1").header("header2", "value2");

    ResponseDetail response = responseSpec.generateResponse(null);

    assertEquals(400, response.status());
    assertEquals("hello world", IOUtils.toString(response.body()));
    assertEquals("value1", response.headers().get("header1"));
    assertEquals("value2", response.headers().get("header2"));
    assertEquals("text/plain;charset=us-ascii",
            response.headers().get(CommonHttpHeaders.CONTENT_TYPE.toHttpString()));
}