Search in sources :

Example 31 with InputStreamReader

use of java.io.InputStreamReader in project Rutgers-Course-Tracker by tevjef.

the class Utils method parseResource.

public static String parseResource(Context context, int resource) throws IOException {
    InputStream is = context.getResources().openRawResource(resource);
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 32 with InputStreamReader

use of java.io.InputStreamReader in project playn by threerings.

the class AndroidAssets method getTextSync.

@Override
public String getTextSync(String path) throws Exception {
    InputStream is = openAsset(path);
    try {
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    } finally {
        is.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 33 with InputStreamReader

use of java.io.InputStreamReader in project blueprints by tinkerpop.

the class RestHelper method convertStreamToString.

private static String convertStreamToString(final InputStream is) throws Exception {
    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    return sb.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 34 with InputStreamReader

use of java.io.InputStreamReader in project blueprints by tinkerpop.

the class RestHelper method post.

private static JSONObject post(final String uri, final String postData, final String contentType, final String accept, final boolean noResult) {
    try {
        final HttpURLConnection connection = createConnection(uri, contentType, accept);
        connection.setDoOutput(true);
        final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        // post data with Content-Length automatically set
        writer.write(postData);
        writer.close();
        if (noResult) {
            new InputStreamReader(connection.getInputStream()).close();
            return null;
        } else {
            return new JSONObject(new JSONTokener(convertStreamToString(connection.getInputStream())));
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JSONObject(org.codehaus.jettison.json.JSONObject) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 35 with InputStreamReader

use of java.io.InputStreamReader in project blueprints by tinkerpop.

the class RestHelper method act.

private static void act(final String uri, final String verb, final String contentType, final String accept) {
    try {
        final HttpURLConnection connection = createConnection(uri, contentType, accept);
        connection.setRequestMethod(verb);
        new InputStreamReader(connection.getInputStream()).close();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Aggregations

InputStreamReader (java.io.InputStreamReader)4884 BufferedReader (java.io.BufferedReader)3422 IOException (java.io.IOException)2119 InputStream (java.io.InputStream)1277 FileInputStream (java.io.FileInputStream)859 URL (java.net.URL)611 ArrayList (java.util.ArrayList)560 File (java.io.File)520 Reader (java.io.Reader)518 Test (org.junit.Test)452 HttpURLConnection (java.net.HttpURLConnection)294 ByteArrayInputStream (java.io.ByteArrayInputStream)282 FileNotFoundException (java.io.FileNotFoundException)241 OutputStreamWriter (java.io.OutputStreamWriter)241 URLConnection (java.net.URLConnection)227 HashMap (java.util.HashMap)192 Socket (java.net.Socket)179 OutputStream (java.io.OutputStream)178 StringWriter (java.io.StringWriter)148 PrintWriter (java.io.PrintWriter)140