Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

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

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:Main.java

public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor)
        throws Exception {

    Process proc = null;/*  w ww  . ja  v a 2  s. c  o m*/
    int exitCode = -1;

    if (runAsRoot)
        proc = Runtime.getRuntime().exec("su");
    else
        proc = Runtime.getRuntime().exec("sh");

    OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());

    for (int i = 0; i < cmds.length; i++) {
        Log.d("the-onion-phone",
                "executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);

        out.write(cmds[i]);
        out.write("\n");
    }

    out.flush();
    out.write("exit\n");
    out.flush();

    if (waitFor) {

        final char buf[] = new char[10];

        // Consume the "stdout"
        InputStreamReader reader = new InputStreamReader(proc.getInputStream());
        int read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null)
                log.append(buf, 0, read);
        }

        // Consume the "stderr"
        reader = new InputStreamReader(proc.getErrorStream());
        read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null)
                log.append(buf, 0, read);
        }

        exitCode = proc.waitFor();

    }

    return exitCode;

}

From source file:Main.java

public static String readFromFile(final File file) throws Exception {
    final FileInputStream filestream = new FileInputStream(file);
    final InputStreamReader reader = new InputStreamReader(filestream);
    try {//from  w w w  .j av  a 2 s . co m
        final StringBuilder buffer = new StringBuilder();
        final char[] tmp = new char[2048];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        reader.close();
    }
}

From source file:common.DBHelper.java

/**
 * Reads SQL statements from file. SQL commands in file must be separated by
 * a semicolon.//w w w  . j  a v  a 2s .c o  m
 *
 * @param is input stream of the file
 * @return array of command strings
 */
private static String[] readSqlStatements(InputStream is) {
    try {
        char buffer[] = new char[256];
        StringBuilder result = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(is, "UTF-8");
        while (true) {
            int count = reader.read(buffer);
            if (count < 0) {
                break;
            }
            result.append(buffer, 0, count);
        }
        return result.toString().split(";");
    } catch (IOException ex) {
        throw new RuntimeException("Cannot read ", ex);
    }
}

From source file:common.DBHelper.java

private static String[] readSqlStatements(URL url) {
    try {/*w  w w. j a va  2 s  .c  o  m*/
        char buffer[] = new char[256];
        StringBuilder result = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(url.openStream(), "UTF-8");
        while (true) {
            int count = reader.read(buffer);
            if (count < 0) {
                break;
            }
            result.append(buffer, 0, count);
        }
        return result.toString().split(";");
    } catch (IOException ex) {
        throw new RuntimeException("Cannot read " + url, ex);
    }
}

From source file:Main.java

public static String readInputStream(InputStream inputStream) throws IOException {
    InputStreamReader inputStreamReader = null;
    try {//from   w ww .  j  ava 2 s.c  om
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        char[] buffer = new char[512];
        StringBuilder str = new StringBuilder();
        int i = 0;
        while ((i = inputStreamReader.read(buffer)) != -1)
            str.append(buffer, 0, i);
        return str.toString();
    } finally {
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
    }
}

From source file:bluej.collect.CollectUtility.java

/**
 * Reads a source code file from the project, and anonymises it
 *///from w w w.  j a v a2  s  . c  om
static String readFileAndAnonymise(ProjectDetails proj, File f) {
    try {
        StringBuilder sb = new StringBuilder();
        FileInputStream inputStream = new FileInputStream(f);
        InputStreamReader reader = new InputStreamReader(inputStream, proj.charset);
        char[] buf = new char[4096];

        int read = reader.read(buf);
        while (read != -1) {
            sb.append(buf, 0, read);
            read = reader.read(buf);
        }

        reader.close();
        inputStream.close();
        return CodeAnonymiser.anonymise(sb.toString());
    } catch (IOException ioe) {
        return null;
    }
}

From source file:org.teleportr.Connector.java

public static String loadString(HttpURLConnection conn) throws Exception {
    StringBuilder result = new StringBuilder();
    InputStreamReader in = new InputStreamReader(new BufferedInputStream(conn.getInputStream()));
    int read;/*  ww w.j a v  a 2 s  .  c o  m*/
    char[] buff = new char[1024];
    while ((read = in.read(buff)) != -1) {
        result.append(buff, 0, read);
    }
    conn.disconnect();
    return result.toString();
}

From source file:Main.java

public static ArrayList autocomplete(String input) {
    ArrayList resultList = null;/*w w  w  .  j a  v  a 2s. com*/

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?key=" + API_KEY);
        //sb.append("&components=country:gr");
        sb.append("&sensor=false");
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));

        URL url = new URL(sb.toString());
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        //Log.e(LOG_TAG, "Error processing Places API URL", e);
        return resultList;
    } catch (IOException e) {
        // Log.e(LOG_TAG, "Error connecting to Places API", e);
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

        // Extract the Place descriptions from the results
        resultList = new ArrayList(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            System.out.println(predsJsonArray.getJSONObject(i).getString("description"));
            System.out.println("============================================================");
            resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
        }
    } catch (JSONException e) {
        //Log.e(LOG_TAG, "Cannot process JSON results", e);
    }

    return resultList;
}

From source file:com.whp.android.location.geocode.GeocodeHelper.java

/**
 * getAddresses/* w  ww .ja  v  a2  s .co  m*/
 * 
 * @param url
 * @return
 */
public static GeocodeResult getAddresses(StringBuilder url) {
    GeocodeResult result = null;

    try {

        HttpURLConnection httpURLConnection = null;
        StringBuilder stringBuilder = new StringBuilder();

        httpURLConnection = (HttpURLConnection) new URL(url.toString()).openConnection();
        InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());

        int read;
        char[] buff = new char[1024];
        while ((read = inputStreamReader.read(buff)) != -1) {
            stringBuilder.append(buff, 0, read);
        }
        httpURLConnection.disconnect();

        JSONObject jObject = new JSONObject(stringBuilder.toString());

        result = GeocodeJsonParser.parse(jObject);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.google.android.feeds.ContentHandlerUtils.java

/**
 * Returns the body of a {@link URLConnection} as a {@link String}.
 *///from www .  j a  v a 2s. c o m
public static String toString(URLConnection connection) throws IOException {
    if (connection == null) {
        throw new IllegalArgumentException("URLConnection is null");
    }
    int contentLength = connection.getContentLength();
    if (contentLength < 0) {
        contentLength = DEFAULT_BUFFER_SIZE;
    }
    String charset = getCharSet(connection);
    InputStream input = getUncompressedInputStream(connection);
    try {
        InputStreamReader reader = new InputStreamReader(input, charset);
        StringBuilder builder = new StringBuilder(contentLength);
        char[] buffer = new char[1024];
        for (int n = reader.read(buffer); n != -1; n = reader.read(buffer)) {
            builder.append(buffer, 0, n);
        }
        return builder.toString();
    } finally {
        input.close();
    }
}