Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

In this page you can find the example usage for java.io BufferedReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:Main.java

/**
 * Run a command and return its output./*  w  w  w  .  j av a2s . co m*/
 */
public static String systemOut(String command) throws Exception {
    int c;
    String[] cmd = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);

    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuilder s = new StringBuilder();
    while ((c = r.read()) != -1) {
        s.append((char) c);
    }
    p.waitFor();

    return s.toString();
}

From source file:Main.java

public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
    BufferedReader input11 = toBufferedReader(input1);
    BufferedReader input21 = toBufferedReader(input2);

    int ch2;/* w ww  . j  a v a 2  s . c  o  m*/
    for (int ch = input11.read(); -1 != ch; ch = input11.read()) {
        ch2 = input21.read();
        if (ch != ch2) {
            return false;
        }
    }

    ch2 = input21.read();
    return ch2 == -1;
}

From source file:com.github.jknack.handlebars.internal.Files.java

/**
 * Read a file source./* w  ww .  j  av  a2  s . c om*/
 *
 * @param source The file source.
 * @return The file content.
 * @throws IOException If the file can't be read.
 */
public static String read(final BufferedReader source) throws IOException {
    notNull(source, "The input is required.");
    try {
        int ch = source.read();
        StringBuilder script = new StringBuilder();
        while (ch != -1) {
            script.append((char) ch);
            ch = source.read();
        }
        return script.toString();
    } finally {
        source.close();
    }
}

From source file:com.nexmo.sdk.core.client.Client.java

private static String getResponseString(InputStream inputStream) throws IOException {
    StringBuilder builder = new StringBuilder();
    Reader reader = new InputStreamReader(inputStream);
    BufferedReader buffReader = new BufferedReader(reader);
    int intChar;//from  ww w . jav  a  2  s .c  o  m

    while ((intChar = buffReader.read()) != -1)
        builder.append((char) intChar);

    return builder.toString();
}

From source file:Main.java

/**
 * Loads the contents of a file in the assets directory as a string. Use '/' as separator.
 *
 * @param assetManager  assetManager//from   w w  w. ja  v a 2s.com
 * @param assetFileName assetFileName (relative path to asset file from assets folder)
 * @return The string contents of the asset
 * @throws IOException Any problem loading asset
 */
public static String loadAssetAsString(AssetManager assetManager, String assetFileName) throws IOException {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(assetManager.open(assetFileName), "UTF-8"));
    int character;
    StringBuilder builder = new StringBuilder();
    do {
        character = reader.read();
        if (character == -1) {
            break;
        } else {
            builder.append((char) character);
        }
    } while (true);
    return builder.toString();
}

From source file:org.kuali.rice.core.test.JAXBAssert.java

public static void assertEqualXmlMarshalUnmarshalWithResource(Object objectToMarshal, InputStream expectedXml,
        Class<?>... classesToBeBound) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(expectedXml));
    StringWriter writer = new StringWriter();
    int data = -1;
    while ((data = reader.read()) != -1) {
        writer.write(data);//from ww w .j  av a  2s . c  om
    }
    assertEqualXmlMarshalUnmarshal(objectToMarshal, writer.toString(), classesToBeBound);
}

From source file:org.infoscoop.request.ProxyRequestTest.java

public static void testMaximizeGadgetFilter() throws Exception {
    ProxyRequest info = new ProxyRequest("http://localhost:8080/gadget-examples/maximize2.xml",
            "MaximizeGadget");

    //System.out.println(info.executeGet());

    Map headers = info.getResponseHeaders();
    for (Iterator keys = headers.keySet().iterator(); keys.hasNext();) {
        Object key = keys.next();
        System.out.println(key + ": " + headers.get(key));
    }//from ww  w .j ava  2s  .c  om

    BufferedReader br = new BufferedReader(new InputStreamReader(info.getResponseBody(), "UTF-8"));

    int c = br.read();
    while (c != -1) {
        System.out.print((char) c);
        c = br.read();
    }

    br.close();
}

From source file:com.mopaas_mobile.http.BaseHttpRequester.java

public static String doGET(String urlstr, List<BasicNameValuePair> params) throws IOException {
    String result = null;/* www.j  ava2s. c o  m*/
    String content = "";
    for (int i = 0; i < params.size(); i++) {
        content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "="
                + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8");
    }
    URL url = new URL(urlstr + "?" + content.substring(1));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.connect();
    InputStream is = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    StringBuffer b = new StringBuffer();
    int ch;
    while ((ch = br.read()) != -1) {
        b.append((char) ch);
    }
    result = b.toString().trim();
    connection.disconnect();
    return result;
}

From source file:com.mopaas_mobile.http.BaseHttpRequester.java

public static String doGETwithHeader(String urlstr, String token, List<BasicNameValuePair> params)
        throws IOException {
    String result = null;//from   ww w  .java  2  s . c o m
    String content = "";
    for (int i = 0; i < params.size(); i++) {
        content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "="
                + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8");
    }
    URL url = new URL(urlstr + "?" + content.substring(1));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("token", token);
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.connect();
    InputStream is = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    StringBuffer b = new StringBuffer();
    int ch;
    while ((ch = br.read()) != -1) {
        b.append((char) ch);
    }
    result = b.toString().trim();
    connection.disconnect();
    return result;
}

From source file:itdelatrisu.opsu.downloads.BloodcatServer.java

/**
 * Returns a JSON object from a URL.//ww  w . j av  a 2s  . co m
 * @param url the remote URL
 * @return the JSON object
 * @author Roland Illig (http://stackoverflow.com/a/4308662)
 */
public static JSONObject readJsonFromUrl(URL url) throws IOException {
    // open connection
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(Download.CONNECTION_TIMEOUT);
    conn.setReadTimeout(Download.READ_TIMEOUT);
    conn.setUseCaches(false);
    try {
        conn.connect();
    } catch (SocketTimeoutException e) {
        ErrorHandler.error("Connection to server timed out.", e, false);
        throw e;
    }

    if (Thread.interrupted())
        return null;

    // read JSON
    JSONObject json = null;
    try (InputStream in = conn.getInputStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();
        int c;
        while ((c = rd.read()) != -1)
            sb.append((char) c);
        json = new JSONObject(sb.toString());
    } catch (SocketTimeoutException e) {
        ErrorHandler.error("Connection to server timed out.", e, false);
        throw e;
    } catch (JSONException e1) {
        ErrorHandler.error("Failed to create JSON object.", e1, true);
    }
    return json;
}