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.boozallen.cognition.test.utils.TestResourceUtils.java

public static String getResourceAsString(Class<?> testClass, String resource) throws IOException {
    return IOUtils.toString(getResourceAsStream(testClass, resource));
}

From source file:example.rhino.DynamicScopesWithHandlebars.java

private static String readFile(String name) throws IOException {
    return IOUtils.toString(DynamicScopesWithHandlebars.class.getResource(name).openStream());
}

From source file:com.moz.fiji.schema.tools.TestCreateTableTool.java

public static String readResource(String resourcePath) throws IOException {
    final InputStream istream = TestCreateTableTool.class.getClassLoader().getResourceAsStream(resourcePath);
    try {// w  ww  .jav a 2s. c om
        return IOUtils.toString(istream);
    } finally {
        ResourceUtils.closeOrLog(istream);
    }
}

From source file:com.fluke.parser.YahooIntradayParserTest.java

@Test
public void testFilter() throws IOException {
    String data = IOUtils
            .toString(YahooIntradayParser.class.getClassLoader().getResourceAsStream("raw_intraday_data"));
    YahooIntradayParser parser = new YahooIntradayParser();
    String processedData = parser.filter(data);
    System.out.println(processedData);

}

From source file:myproject.MyServer.java

public static void Add(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {//  ww w.  j  a va  2  s  . co  m
        String jsonString = IOUtils.toString(request.getInputStream());
        JSONObject json = (JSONObject) JSONValue.parse(jsonString);
        String student_name = (String) json.get("student_name");
        Long regno = (Long) json.get("regno");
        Double cgpa = (Double) json.get("cgpa");

        String query = String.format(
                "INSERT INTO student " + "(student_name, regno, cgpa) " + "VALUES('%s',%d,%f)",
                JSONValue.escape(student_name), regno, cgpa);

        database.runUpdate(query);

        String result = database.getStudent(regno);
        response.getWriter().write(result);
    } catch (Exception ex) {
        JSONObject output = new JSONObject();
        output.put("error", "Connection failed: " + ex.getMessage());
        response.getWriter().write(JSONValue.toJSONString(output));
    }
}

From source file:com.grantedbyme.example.ServletUtils.java

public static GrantedByMe getSDK(HttpServlet context) throws IOException {
    // read private key
    String privateKey = null;// w  w w.  ja v  a 2s. c  o  m
    InputStream privateKeyInputStream = context.getClass().getResourceAsStream("/private_key.pem");
    try {
        privateKey = IOUtils.toString(privateKeyInputStream);
    } finally {
        privateKeyInputStream.close();
    }
    // read server key
    String serverKey = null;
    InputStream serverKeyInputStream = context.getClass().getResourceAsStream("/server_key.pem");
    try {
        serverKey = IOUtils.toString(serverKeyInputStream);
    } finally {
        serverKeyInputStream.close();
    }
    // _log(serverKey);
    // initialize BouncyCastle security provider
    Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 0);
    // create sdk
    GrantedByMe sdk = new GrantedByMe(privateKey, serverKey);
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // set SDK parameters
    sdk.apiURL = "https://api-dev.grantedby.me/v1/service/";
    //sdk.isDebug = true;
    return sdk;
}

From source file:ld.ldhomework.crawler.DownloadedFile.java

public DownloadedFile(InputStream is, String contentType) {
    this.contentType = contentType;
    try {/*from w  w w. j  a  v  a  2s .co  m*/
        this.content = IOUtils.toString(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.opengamma.web.bundle.BundleUtils.java

/**
 * Reads and combines a bundle.//from  w  w  w  . j ava 2  s  .co  m
 * 
 * @param bundle  the bundle to read, not null
 * @return the combined source code, not null
 */
public static String readBundleSource(Bundle bundle) {
    List<Fragment> allFragments = bundle.getAllFragments();
    StringBuilder buf = new StringBuilder(1024);
    for (Fragment fragment : allFragments) {
        try {
            buf.append(IOUtils.toString(fragment.getUri()));
            buf.append("\n");
        } catch (IOException ex) {
            throw new DataNotFoundException("IOException reading " + fragment.getUri());
        }
    }
    return buf.toString();
}

From source file:com.hangum.tadpole.rdb.core.dialog.export.sqltoapplication.application.SQLToRealGridConvert.java

/**
 * sql to string//  ww  w . j a v  a2s . c  o m
 * 
 * @param name
 * @param sql
 * @return
 */
public static String sqlToString(UserDBDAO userDB, String name, String sql) {
    String retHtml = "";
    try {
        String STR_TEMPLATE = IOUtils.toString(SQLToRealGridConvert.class.getResource("realgrid.js.template"));

        QueryExecuteResultDTO queryResult = QueryUtils.executeQuery(userDB, sql, 0, 4);
        Map<Integer, String> columnLabel = queryResult.getColumnLabelName();
        Map<Integer, Integer> columnType = queryResult.getColumnType();

        String strField = "";
        StringBuffer sbField = new StringBuffer();
        for (int i = 0; i < columnLabel.size(); i++) {
            String strColumnLabel = columnLabel.get(i);
            boolean isNumber = RDBTypeToJavaTypeUtils.isNumberType(columnType.get(i));
            sbField.append(String.format(GROUP_TEMPLATE, strColumnLabel, isNumber ? "number" : "text"));
        }
        strField = StringUtils.removeEnd(sbField.toString(), ",");

        String strColumn = "";
        StringBuffer sbColumn = new StringBuffer();
        for (int i = 0; i < columnLabel.size(); i++) {
            String strColumnLabel = columnLabel.get(i);
            sbColumn.append(String.format(GROUP_DATA_TEMPLATE, strColumnLabel, strColumnLabel));
        }
        strColumn = StringUtils.removeEnd(sbColumn.toString(), ",");

        retHtml = StringUtils.replaceOnce(STR_TEMPLATE, "_TDB_TEMPLATE_FIELD_", strField);
        retHtml = StringUtils.replaceOnce(retHtml, "_TDB_TEMPLATE_COLUMN_", strColumn);
    } catch (Exception e) {
        logger.error("SQL template exception", e);
    }

    return retHtml;
}

From source file:com.moz.fiji.schema.layout.FijiTableLayouts.java

/**
 * Loads a table layout descriptor from a JSON resource.
 *
 * @param resourcePath Path of the resource to load the JSON descriptor from.
 * @return the decoded TableLayoutDesc.//  w ww . j ava2 s. c  om
 * @throws IOException on I/O error.
 */
public static TableLayoutDesc getLayout(String resourcePath) throws IOException {
    final InputStream istream = FijiTableLayouts.class.getClassLoader().getResourceAsStream(resourcePath);
    try {
        final String json = IOUtils.toString(istream);
        return (TableLayoutDesc) FromJson.fromJsonString(json, TableLayoutDesc.SCHEMA$);
    } finally {
        ResourceUtils.closeOrLog(istream);
    }
}