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.jk.examples.dropwizard.util.FilesUtil.java

/**
 * Read files contents as string using Apache IO library,.
 *
 * @param fileName/*w  w  w. ja  v  a2 s  .  c o m*/
 *            the file name
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static String readResourceFile(final String fileName) throws IOException {
    InputStream in = null;
    try {
        in = FilesUtil.class.getResourceAsStream(fileName);
        if (in == null) {
            System.out.print("Resource " + fileName + " is not found");
            return null;
        }
        return IOUtils.toString(in);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.jaxio.celerio.util.XsdHelper.java

public static String getResourceContentAsString(String resourcePath) {
    PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();

    try {//w w w.ja  va 2s  .c o m
        Resource packInfosAsResource[] = o.getResources(resourcePath);
        for (Resource r : packInfosAsResource) {
            return IOUtils.toString(r.getInputStream());
        }
        return null;
    } catch (IOException ioe) {
        throw new RuntimeException("Error while searching for : " + resourcePath, ioe);
    }
}

From source file:com.moz.fiji.mapreduce.TestingResources.java

/**
 * Loads a text resource by name.// w w w . j  a va  2 s. c om
 *
 * @param resourcePath Path of the resource to load.
 * @return the resource content, as a string.
 * @throws IOException on I/O error.
 */
public static String get(final String resourcePath) throws IOException {
    final InputStream istream = FijiTableLayouts.class.getClassLoader().getResourceAsStream(resourcePath);
    try {
        return IOUtils.toString(istream);
    } finally {
        istream.close();
    }
}

From source file:LogOutput.java

public void logOutput(BuildListener listener, Process process) throws IOException {

    if (process != null) {
        String output = IOUtils.toString(process.getInputStream());
        String errorOutput = IOUtils.toString(process.getErrorStream());

        logToJvm(output, errorOutput);/*from  www .  j  av  a 2  s  . com*/

        logToConsole(listener, output, errorOutput);
    }

}

From source file:Controller.MainClass.java

public static String getLocationFromAPi(String CityName) throws ParseException {
    String[] list;// w  w  w.j a  va2 s . c  o m
    list = CityName.replaceAll("[^a-zA-Z]", "").toLowerCase().split("\\s+");
    CityName = list[0];
    String Address = "";
    String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + CityName + "&sensor=false";

    try {
        String genreJson = IOUtils.toString(new URL(url));
        JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
        JSONArray genreArray = (JSONArray) genreJsonObject.get("results");
        if (genreArray.size() == 0) {
            return "";
        } else {
            JSONObject firstGenre = (JSONObject) genreArray.get(0);
            String City = firstGenre.get("formatted_address").toString();
            JSONObject obj = (JSONObject) firstGenre.get("geometry");
            JSONObject loc = (JSONObject) obj.get("location");
            String lat = loc.get("lat").toString();
            String lng = loc.get("lng").toString();
            Address = (String) (lat + "/" + lng + "/" + City);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Address;
}

From source file:com.msuite.mongodb.application.definition.JSONDefinition.java

public String getJsonFromFile(String fileName) {

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {/* w ww .j  a  va 2s .c  om*/
        result = IOUtils.toString(classLoader.getResourceAsStream(fileName + ".json"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:cz.vutbr.fit.xhriba01.bc.lib.Utils.java

public static String inputStreamToString(InputStream stream) {

    try {/*from  www.j av  a 2 s. co m*/

        return IOUtils.toString(stream);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:be.solidx.hot.utils.SSLUtils.java

public static PrivateKey toPrivateKey(InputStream pemInputStream)
        throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
    String data = IOUtils.toString(pemInputStream);
    String[] tokens = data.split(PEM_KEY_BEGIN_DELIMITER);
    tokens = tokens[1].trim().split(PEM_KEY_END_DELIMITER);
    return generatePrivateKeyFromDER(DatatypeConverter.parseBase64Binary(tokens[0]));
}

From source file:edu.purdue.cybercenter.dm.util.Helper.java

public static String fileToString(String filename) {
    String content;//from www. j  ava2s  . c  o m
    File file = new File(filename);
    try (InputStream is = new FileInputStream(file)) {
        content = IOUtils.toString(is);
    } catch (FileNotFoundException ex) {
        throw new RuntimeException(String.format("%s: file does not exist: %s", ex.getMessage(), filename));
    } catch (IOException ex) {
        throw new RuntimeException(
                String.format("%s: Unable to convert file to string: %s", ex.getMessage(), filename));
    }

    return content;
}

From source file:com.iblsoft.iwxxm.webservice.ws.TestUtils.java

public static String loadTestResource(String resourcePath) throws IOException {
    try (InputStream stream = TestUtils.class.getResourceAsStream("/iwxxm-messages/" + resourcePath)) {
        return IOUtils.toString(stream);
    }/*from ww w .j a va  2 s . com*/
}