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:org.neo4j.nlp.examples.author.main.java

private static String executePost(String targetURL, String payload) {
    try {/*from   w w  w .j av  a2s.c  o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(targetURL);

        StringEntity input = new StringEntity(payload);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        StringBuilder output = new StringBuilder();
        while (br.read() != -1) {
            output.append(br.readLine()).append('\n');
        }

        httpClient.getConnectionManager().shutdown();

        return output.toString();

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;
}

From source file:com.claresco.tinman.servlet.XapiServletUtility.java

protected static String getStringFromReader(BufferedReader theReader)
        throws XapiServletOperationProblemException {
    int c;/* w w w . j  a  va 2s.co  m*/
    StringBuffer theStringBuffer = new StringBuffer();
    try {
        while ((c = theReader.read()) != -1) {
            theStringBuffer.append((char) c);
        }
        String s = theStringBuffer.toString();
        return theStringBuffer.toString();
    } catch (IOException e) {
        throw new XapiServletOperationProblemException("Having problem reading the document");
    }

}

From source file:com.pureinfo.srm.outlay.action.SearchCheckCodeAction.java

/**
 * /*from w  w w.ja va 2  s .c om*/
 * @param strUrl
 * @param strPostRequest
 * @return
 */
public static String getPageContent(String strUrl, String strPostRequest) {
    //
    StringBuffer buffer = new StringBuffer();
    System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
    System.setProperty("sun.net.client.defaultReadTimeout", "5000");
    try {
        URL newUrl = new URL(strUrl);
        HttpURLConnection hConnect = (HttpURLConnection) newUrl.openConnection();
        //POST
        if (strPostRequest.length() > 0) {
            hConnect.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(hConnect.getOutputStream());
            out.write(strPostRequest);
            out.flush();
            out.close();
        }
        //
        BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream()));
        int ch;
        for (int length = 0; (ch = rd.read()) > -1; length++)
            buffer.append((char) ch);
        rd.close();
        hConnect.disconnect();
        return buffer.toString().trim();
    } catch (Exception e) {
        // return ":";
        return null;
    } finally {
        buffer.setLength(0);
    }
}

From source file:net.wedjaa.elasticparser.tester.ESSearchTester.java

private static String getQuery(String queryName) {

    StringBuffer sb = new StringBuffer();

    BufferedReader br;

    try {// w w  w .  ja  v a2  s .c o m
        br = new BufferedReader(new InputStreamReader(
                Thread.currentThread().getContextClassLoader().getResourceAsStream(queryName), "UTF-8"));
        for (int c = br.read(); c != -1; c = br.read())
            sb.append((char) c);
    } catch (Exception e) {
        System.err.println("Failed to read query: " + e);
    }

    String queryString = sb.toString();
    System.out.println("Loaded Query: " + queryString);
    return queryString;
}

From source file:org.neo4j.nlp.examples.sentiment.main.java

private static String executePost(String targetURL, String payload) {
    try {//from w  w w  .  jav a 2 s .c o m

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(targetURL);

        StringEntity input = new StringEntity(payload);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        StringBuilder output = new StringBuilder();
        while (br.read() != -1) {
            output.append(br.readLine()).append('\n');
        }

        httpClient.getConnectionManager().shutdown();

        return "{" + output.toString();

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;
}

From source file:com.prowidesoftware.swift.utils.Lib.java

/**
 * Read the content of the given file into a string.
 *
 * @param file the file to be read/*from   w  ww. ja  v  a 2 s  .  com*/
 * @param encoding encoding to use
 * @return the file contents or null if file is null or does not exist, or can't be read, or is not a file
 * @throws IOException if an error occurs during read
 */
public static String readFile(final File file, final String encoding) throws IOException {
    if (file == null || !file.exists() || !file.canRead() || !file.isFile()) {
        return null;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
    final StringBuilder sb = new StringBuilder((int) file.length());
    try {
        int c = 0;
        while ((c = in.read()) != -1) {
            sb.append((char) c);
        }
    } finally {
        in.close();
    }
    return sb.toString();
}

From source file:Main.java

public static String getProcessName(Context context, int pID) {
    BufferedReader cmdlineReader = null;
    try {// ww w.j a va 2  s  .  c o  m
        cmdlineReader = new BufferedReader(
                new InputStreamReader(new FileInputStream("/proc/" + pID + "/cmdline"), "iso-8859-1"), 100);
        int c;
        StringBuilder processName = new StringBuilder();
        while ((c = cmdlineReader.read()) > 0) {
            processName.append((char) c);
        }
        return processName.toString();
    } catch (Throwable ignore) {
    } finally {
        if (cmdlineReader != null) {
            try {
                cmdlineReader.close();
            } catch (IOException e) {
            }
        }
    }
    return getProcessName_PM(context, pID);
}

From source file:com.qweex.callisto.moar.twit.java

public static String callURL(String myURL) {
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;/*from  w w w .  j a  va2 s .  co  m*/
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:" + myURL, e);
    }

    return sb.toString();
}

From source file:org.cobaltians.cobalt.font.CobaltFontManager.java

private static String readFileFromAssets(String file) {
    try {/*from   ww  w.ja  v a 2 s.com*/
        AssetManager assetManager = mContext.getAssets();
        InputStream inputStream = assetManager.open(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder fileContent = new StringBuilder();
        int character;

        while ((character = bufferedReader.read()) != -1) {
            fileContent.append((char) character);
        }

        return fileContent.toString();
    } catch (FileNotFoundException exception) {
        if (Cobalt.DEBUG)
            Log.e(TAG, TAG + " - readFileFromAssets: " + file + "not found.");
    } catch (IOException exception) {
        if (Cobalt.DEBUG)
            Log.e(TAG, TAG + " - readFileFromAssets: IOException");
        exception.printStackTrace();
    }

    return "";
}

From source file:com.rukman.emde.smsgroups.client.NetworkUtilities.java

private static String executeHttpRequest(HttpUriRequest request)
        throws ClientProtocolException, IOException, JSONException {
    final HttpClient client = getHttpClient();
    StringBuilder sb = new StringBuilder();
    String output = null;//from   www  .  j  a  va  2 s . c o m
    int responseCode;
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream istream = entity.getContent();
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(istream));
                int value = reader.read();
                while (value != -1) {
                    sb.append((char) value);
                    value = reader.read();
                }
            } finally {
                if (istream != null) {
                    istream.close();
                }
            }
        }
        responseCode = response.getStatusLine().getStatusCode();
        if (responseCode == HttpStatus.SC_OK) {
            output = sb.toString();
            Log.v(TAG, String.format("Response is: %1$s", output));
        } else {
            setErrorString(new JSONObject(sb.toString()).getString(JSONKeys.KEY_MESSAGE));
            Log.v(TAG, String.format("Error is: %1$s",
                    new JSONObject(sb.toString()).getString(JSONKeys.KEY_MESSAGE)));
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
    return output;
}