Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read(char cbuf[]) throws IOException 

Source Link

Document

Reads characters into an array.

Usage

From source file:com.websqrd.catbot.scraping.HttpHandler.java

/**
 *  ? .// w w  w . j  a  va2  s  . c  o m
 */
public static String executeGet2(HttpClient httpclient, String url, String encoding, String[] a)
        throws IOException {
    String type = "text";
    HttpGet httget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httget);
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentType();
    if (header != null) {
        type = header.getValue().toLowerCase();
    }

    //InputStream to String
    InputStream is = entity.getContent();
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, encoding));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    a[0] = writer.toString();
    return type;
}

From source file:Main.java

private static String readAsString(URLConnection connection) throws IOException {
    InputStream inputStream = connection.getInputStream();
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    try {//from   ww  w .  j  a  v a 2s .  c o m
        StringBuilder sb = new StringBuilder();
        {
            char[] buffer = new char[2048];
            while (true) {
                int read = reader.read(buffer);
                if (read < 0)
                    break;
                sb.append(buffer, 0, read);
            }
        }

        return sb.toString();
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:Main.java

public static String getStringFromFile(String filePath) {
    String s = null;//from  w  w w .j a va  2  s  .  c o  m
    File f = new File(filePath);
    Reader reader = null;
    try {
        reader = new BufferedReader(new FileReader(f));
        StringBuilder sb = new StringBuilder();
        char[] buff = new char[50];
        int length;
        while ((length = reader.read(buff)) != -1) {
            sb.append(buff, 0, length);
        }
        s = sb.toString();
    } catch (FileNotFoundException e) {
        s = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static StringBuilder getResponse(InputStream data) throws IOException {
    Reader in = new BufferedReader(new InputStreamReader(data, "UTF-8"), 2048);
    StringBuilder buffer = new StringBuilder();
    char[] buf = new char[2048];
    int l = 0;//from   w ww.  j a va2 s  . com
    while (l >= 0) {
        buffer.append(buf, 0, l);
        l = in.read(buf);
    }
    return buffer;
}

From source file:Main.java

public static String gzipToString(final HttpEntity entity, final String defaultCharset)
        throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*  ww w . ja va 2  s.  co m*/
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    // gzip logic start
    if (entity.getContentEncoding().getValue().contains("gzip")) {
        instream = new GZIPInputStream(instream);
    }
    // gzip logic end
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    int i = (int) entity.getContentLength();
    if (i < 0) {
        i = 4096;
    }
    String charset = EntityUtils.getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    CharArrayBuffer buffer = new CharArrayBuffer(i);
    try {
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:Main.java

private static CharSequence consume(URLConnection connection, int maxChars) throws IOException {
    String encoding = getEncoding(connection);
    StringBuilder out = new StringBuilder();
    Reader in = null;
    try {/*  www  .j  a va 2  s. c  o m*/
        in = new InputStreamReader(connection.getInputStream(), encoding);
        char[] buffer = new char[1024];
        int charsRead;
        while (out.length() < maxChars && (charsRead = in.read(buffer)) > 0) {
            out.append(buffer, 0, charsRead);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
                // continue
            } catch (NullPointerException ioe) {
                // continue
            }
        }
    }
    return out;
}

From source file:com.grouptuity.venmo.VenmoUtility.java

public static String convertStreamToString(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {//from w  w w.  ja va 2 s  .  c om
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else
        return "";
}

From source file:Main.java

/** Reads an <code>InputStream</code> into a string.
 * @param in The stream to read into a string.
 * @return The stream as a string/*w w  w  .j  a va 2s. co m*/
 * @throws IOException
 */
public static String toString(InputStream in) throws IOException {
    if (in == null) {
        return null;
    }
    final Reader reader = new InputStreamReader(in);
    final char[] buffer = new char[100];
    final CharArrayWriter writer = new CharArrayWriter(1000);
    int read;
    while ((read = reader.read(buffer)) > -1)
        writer.write(buffer, 0, read);
    return writer.toString();
}

From source file:Main.java

public static void write(Reader input, Writer output) throws IOException {
    int len;//from  w ww .  j a v a 2  s.  c o m
    char[] buffer = new char[4096];
    while (-1 != (len = input.read(buffer)))
        output.write(buffer, 0, len);
}

From source file:Main.java

public static String request(String url, Map<String, String> cookies, Map<String, String> parameters)
        throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
    if (cookies != null && !cookies.isEmpty()) {
        StringBuilder cookieHeader = new StringBuilder();
        for (String cookie : cookies.values()) {
            if (cookieHeader.length() > 0) {
                cookieHeader.append(";");
            }/*from w w w. j a  va  2 s .c o m*/
            cookieHeader.append(cookie);
        }
        connection.setRequestProperty("Cookie", cookieHeader.toString());
    }
    connection.setDoInput(true);
    if (parameters != null && !parameters.isEmpty()) {
        connection.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(parametersToWWWFormURLEncoded(parameters));
        osw.flush();
        osw.close();
    }
    if (cookies != null) {
        for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
            if (headerEntry != null && headerEntry.getKey() != null
                    && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) {
                for (String header : headerEntry.getValue()) {
                    for (HttpCookie httpCookie : HttpCookie.parse(header)) {
                        cookies.put(httpCookie.getName(), httpCookie.toString());
                    }
                }
            }
        }
    }
    Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringWriter w = new StringWriter();
    char[] buffer = new char[1024];
    int n = 0;
    while ((n = r.read(buffer)) != -1) {
        w.write(buffer, 0, n);
    }
    r.close();
    return w.toString();
}