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:voldemort.utils.VoldemortIOUtils.java

public static long copyLarge(Reader input, Writer output, long limit) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;/*w w w  .j av a  2 s  .c  o  m*/
    long remaining = limit;
    while (remaining > 0) {
        n = (remaining > DEFAULT_BUFFER_SIZE) ? input.read(buffer) : input.read(buffer, 0, (int) remaining);
        if (n == -1) {
            break;
        }
        output.write(buffer, 0, n);
        count += n;
        remaining -= n;
    }
    return count;
}

From source file:com.teotigraphix.caustk.utils.RuntimeUtils.java

/**
 * Converts and {@link InputStream} to a String.
 * /*from w ww  .  j  a  v  a  2s  .com*/
 * @param is The {@link InputStream} to read into a String.
 * @return THe String read from the stream.
 * @throws IOException
 */
public static final String convertStreamToString(InputStream is) throws IOException {
    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();
}

From source file:com.opengamma.engine.calcnode.CalculationNodeProcess.java

private static String getConfigurationXml(final String url) {
    if (s_httpClient == null) {
        s_httpClient = new DefaultHttpClient();
    }//from w w w . j a v a  2  s .c  om
    s_logger.debug("Fetching {}", url);
    final HttpResponse resp;
    try {
        resp = s_httpClient.execute(new HttpGet(url));
    } catch (Exception e) {
        s_logger.warn("Error fetching {} - {}", url, e.getMessage());
        return null;
    }
    s_logger.debug("HTTP result {}", resp.getStatusLine());
    if (resp.getStatusLine().getStatusCode() != 200) {
        s_logger.warn("No configuration available (HTTP {})", resp.getStatusLine().getStatusCode());
        return null;
    }
    try {
        final Reader in = new InputStreamReader(resp.getEntity().getContent());
        final StringBuilder sb = new StringBuilder();
        final char[] buf = new char[1024];
        int i = in.read(buf);
        while (i > 0) {
            sb.append(buf, 0, i);
            i = in.read(buf);
        }
        s_logger.debug("Configuration document received - {} characters", sb.length());
        return sb.toString();
    } catch (IOException e) {
        s_logger.warn("Error retrieving response from {} - {}", url, e.getMessage());
        return null;
    }
}

From source file:Base64.java

/**
 * Decode base64 encoded data.//  w w  w  .  java  2s  . c om
 *
 * @param reader reader for the base64 encoded data to be decoded
 * @param out    stream where the decoded data should be written to
 * @throws java.io.IOException if an i/o error occurs
 */
public static void decode(Reader reader, OutputStream out) throws IOException {
    char[] chunk = new char[8192];
    int read;
    while ((read = reader.read(chunk)) > -1) {
        decode(chunk, 0, read, out);
    }
}

From source file:Main.java

public static String getJsonString(String urlString) throws Exception {
    InputStream is = null;// w  w  w.  j a  v a  2 s . c  o  m
    Reader reader = null;
    StringBuilder str = new StringBuilder();
    URL url = new URL(urlString);
    URLConnection URLConn = url.openConnection();
    URLConn.setRequestProperty("User-agent", "IE/6.0");
    is = URLConn.getInputStream();
    reader = new InputStreamReader(is, "UTF-8");
    char[] buffer = new char[1];
    while (reader.read(buffer) != -1) {
        str.append(new String(buffer));
    }
    return str.toString();
}

From source file:Main.java

public static String getStringFromFile(String pathUrlName, boolean encode) {
    Writer writer = null;/*from   w ww .  j ava 2s  . c om*/
    try {
        InputStream is = new FileInputStream(new File(pathUrlName));

        writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader;
            if (encode) {
                reader = new BufferedReader(new InputStreamReader(is, HTML_ENCODING));
            } else {
                reader = new BufferedReader(new InputStreamReader(is));
            }
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:mx.itesm.mexadl.metrics.util.Util.java

/**
 * Load the contents of a configuration file into a String.
 * /*from  w  w  w.  j av a 2 s .c o m*/
 * @param path
 * @return
 * @throws Exception
 */
public static String loadFileContent(final String path) throws Exception {
    String returnValue;
    InputStream inputStream;

    inputStream = Util.class.getClassLoader().getResourceAsStream(path);

    returnValue = null;
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        returnValue = writer.toString();
    }

    return returnValue;
}

From source file:Main.java

public static CharSequence getFileAsString(File file) {
    Reader reader = null;
    StringBuilder sb = new StringBuilder(1024);
    try {/*from w w  w  .  j a v a 2 s  .  com*/
        reader = new InputStreamReader(new FileInputStream(file), Charset.forName("ISO-8859-1"));
        char[] buf = new char[1024];
        int length;
        while ((length = reader.read(buf)) >= 0)
            sb.append(buf, 0, length);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(reader);
    }
    String string = sb.toString();
    return string;
}

From source file:com.freedomotic.plugin.purl.Purl.java

private static String readPage(URL url) throws Exception {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url.toURI());
    HttpResponse response = client.execute(request);

    Reader reader = null;
    try {//from   w  ww.j a  va2 s.  c o  m
        reader = new InputStreamReader(response.getEntity().getContent());

        StringBuilder sb = new StringBuilder();
        int read;
        char[] cbuf = new char[1024];
        while ((read = reader.read(cbuf)) != -1) {
            sb.append(cbuf, 0, read);
        }
        return sb.toString();

    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                //do nothing
            }
        }
    }
}

From source file:cz.incad.kramerius.k5indexer.Commiter.java

/**
 * Pipes everything from the reader to the writer via a buffer
 *///from   www .  j  a  va  2  s  .c  o m
private static void pipe(Reader reader, Writer writer) throws IOException {
    char[] buf = new char[1024];
    int read = 0;
    while ((read = reader.read(buf)) >= 0) {
        writer.write(buf, 0, read);
    }
    writer.flush();
}