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.jims.oauth2.common.utils.OAuthUtils.java

/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity./*  ww w  .  j a  v  a  2 s.c  o m*/
 * If defaultCharset is null, the default "UTF-8" is used.
 *
 * @param is             input stream to be saved as string
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws java.io.IOException              if an error occurs reading the input stream
 */
public static String toString(final InputStream is, final String defaultCharset) throws IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream may not be null");
    }

    String charset = defaultCharset;
    if (charset == null) {
        charset = DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(is, charset);
    StringBuilder sb = new StringBuilder();
    int l;
    try {
        char[] tmp = new char[4096];
        while ((l = reader.read(tmp)) != -1) {
            sb.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return sb.toString();
}

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static Object getURLContent(String url) throws Exception {
    HttpMethod method;/*from  w w w  .j  av  a  2s .co m*/
    try {
        ToolboxConfiguration toolboxConfiguration;
        toolboxConfiguration = ToolboxConfiguration.getInstance();

        String host = "http://services.eoportal.org"; //toolboxConfiguration.getConfigurationValue(ToolboxConfiguration.SSE_PORTAL);
        String fullUrl;

        URLConnection conn;
        URL connUrl;

        if (url.startsWith("http://"))
            fullUrl = url;
        else
            fullUrl = host + url;

        connUrl = new URL(fullUrl);
        conn = connUrl.openConnection();

        Reader in = new InputStreamReader(conn.getInputStream());
        StringBuffer out = new StringBuffer();
        char[] buffer = new char[1024];
        for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) {
            out.append(buffer, 0, count);
        }
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:IOUtil.java

/**
 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
 * @param bufferSize Size of internal buffer to use.
 *///from   ww  w.j  a v  a 2  s. c o  m
public static void copy(final Reader input, final Writer output, final int bufferSize) throws IOException {
    final char[] buffer = new char[bufferSize];
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
}

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static Object getURLContent(String host, String url, int port) throws Exception {
    HttpMethod method;/* w  w  w  . j  av a  2  s .c  o m*/
    try {
        method = new GetMethod(url);
        HttpConnection conn = new HttpConnection(host, port);
        String proxyHost;
        String proxyPort;
        if ((proxyHost = System.getProperty("http.proxyHost")) != null
                && (proxyPort = System.getProperty("http.proxyPort")) != null) {
            conn.setProxyHost(proxyHost);
            conn.setProxyPort(Integer.parseInt(proxyPort));
        }
        conn.open();
        method.execute(new HttpState(), conn);
        String inpTmp = method.getResponseBodyAsString();
        Reader in = new InputStreamReader(method.getResponseBodyAsStream());
        StringBuffer out = new StringBuffer();
        char[] buffer = new char[1024];
        for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) {
            out.append(buffer, 0, count);
        }
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:com.icesoft.faces.webapp.parser.JspPageToDocument.java

static String getInputAsString(Reader in) {
    char[] buf = new char[1024];
    StringWriter out = new StringWriter();

    try {/*from   w w  w  .j a va  2  s  . com*/
        int l = 1;
        while (l > 0) {
            l = in.read(buf);
            if (l > 0) {
                out.write(buf, 0, l);
            }
        }
    } catch (IOException e) {
        if (log.isWarnEnabled()) {
            log.warn(e.getMessage(), e);
        }
    }
    return out.toString();

}

From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static void writeReaderToDiskFile(Reader data, String fullPath, String fileName) throws IOException {

    File outDir = new File(fullPath);

    if (!outDir.exists())
        outDir.mkdirs();// w  w  w  .j a  v  a2 s  .c  o m

    BufferedWriter out = null;

    try {
        FileWriter writer = new FileWriter(fullPath + fileName);
        out = new BufferedWriter(writer);

        String readData = null;
        char[] buf = new char[1024];
        int numRead = 0;

        while ((numRead = data.read(buf)) != -1) {
            readData = String.valueOf(buf, 0, numRead);
            out.write(readData);
            buf = new char[1024];
        }

        //out.write(data);
        out.flush();
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:ac.elements.io.Signature.java

/**
 * Read stream into string./*from   w w w  . ja  va 2  s . c om*/
 * 
 * @param input
 *            stream to read
 * 
 * @return the respons body as string
 * 
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static String getResponsBodyAsString(InputStream input) throws IOException {
    String responsBodyString = null;
    try {
        Reader reader = new InputStreamReader(input, DEFAULT_ENCODING);
        StringBuilder b = new StringBuilder();
        char[] c = new char[1024];
        int len;
        while (0 < (len = reader.read(c))) {
            b.append(c, 0, len);
        }
        responsBodyString = b.toString();
    } finally {
        input.close();
    }
    return responsBodyString;
}

From source file:org.apache.drill.test.framework.Utils.java

private static String getHttpResponseAsString(HttpResponse response) throws IOException {
    Reader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    StringBuilder builder = new StringBuilder();
    char[] buffer = new char[1024];
    int l = 0;/*  ww  w. j  ava  2s.c o m*/
    while (l >= 0) {
        builder.append(buffer, 0, l);
        l = reader.read(buffer);
    }
    return builder.toString();
}

From source file:com.zotoh.core.io.StreamUte.java

private static String readTextFile(File fn, String encoding) throws IOException {

    Reader rdr = new InputStreamReader(readStream(fn), isEmpty(encoding) ? UTF8 : encoding);
    StringBuilder sb = new StringBuilder(4096);
    char[] cs = new char[4096];
    int n;/*from   w  w  w. j  a v  a 2  s .  co  m*/

    try {
        while ((n = rdr.read(cs)) > 0) {
            sb.append(cs, 0, n);
        }
        return sb.toString();
    } finally {
        close(rdr);
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.meridio.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {// www.j  a  v  a 2s .c o  m
            String charSet = methodThread.getCharSet();
            if (charSet == null)
                charSet = "utf-8";
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}