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:org.crazydog.util.spring.FileCopyUtils.java

/**
 * Copy the contents of the given Reader to the given Writer.
 * Closes both when done.//from  ww w.j  a v  a  2  s.  c o  m
 * @param in the Reader to copy from
 * @param out the Writer to copy to
 * @return the number of characters copied
 * @throws IOException in case of I/O errors
 */
public static int copy(Reader in, Writer out) throws IOException {
    org.springframework.util.Assert.notNull(in, "No Reader specified");
    org.springframework.util.Assert.notNull(out, "No Writer specified");
    try {
        int byteCount = 0;
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
        }
        try {
            out.close();
        } catch (IOException ex) {
        }
    }
}

From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java

public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }//from w  ww .j  ava2s . c  o  m

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

                "HTTP entity too large to be buffered in memory");
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {

        charset = HTTP.DEFAULT_CONTENT_CHARSET;

    }

    Reader reader = new InputStreamReader(instream, charset);

    StringBuilder buffer = new StringBuilder();

    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:com.tussle.main.Utility.java

public static String readAll(Reader reader) throws IOException {
    StringBuilder toReturn = new StringBuilder();
    while (reader.ready()) {
        char[] holder = new char[1024];
        int charsRead = reader.read(holder);
        toReturn.append(holder, 0, charsRead);
    }//  ww w .j a va  2 s  . c o  m
    return toReturn.toString();
}

From source file:org.wso2.mdm.agent.proxy.utils.ServerUtilities.java

public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException {

    InputStream instream = entity.getContent();

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory.");
    }// www.  j a  v  a 2 s .com

    String charset = getContentCharSet(entity);

    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();

    try {
        char[] bufferSize = new char[1024];
        int length;

        while ((length = reader.read(bufferSize)) != -1) {
            buffer.append(bufferSize, 0, length);
        }
    } finally {
        reader.close();
    }

    return buffer.toString();

}

From source file:com.calebgomer.roadkill_reporter.AsyncReporter.java

private static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*ww w . j a v  a2  s .com*/
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();
    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:com.github.gekoh.yagen.ddl.DDLGenerator.java

public static String read(Reader reader) {
    StringWriter wr = new StringWriter();
    char[] buf = new char[1024];
    int read;//  ww  w  .j a  va2s.co  m
    try {

        while ((read = reader.read(buf)) > -1) {
            wr.write(buf, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return wr.toString();
}

From source file:es.uvigo.ei.sing.jarvest.core.HTTPUtils.java

public static String getURLBodyAsString(String url, Map<String, String> additionalHeaders) throws IOException {

    InputStream in = null;/*from  w ww.  j a  v a  2s. c  o  m*/
    try {
        StringBuffer charsetb = new StringBuffer();
        in = getURLBody(url, charsetb, additionalHeaders);

        String charset = validCharset(charsetb.toString());
        Reader reader = new InputStreamReader(in, Charset.forName(charset));

        StringBuffer sbuf = new StringBuffer();

        char[] characters = new char[1024];
        int readed = 0;
        while ((readed = reader.read(characters)) != -1) {
            sbuf.append(characters, 0, readed);
        }
        return sbuf.toString();
    } finally {

        if (in != null)
            in.close();

    }
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java

/**
 * Calculates CRC value for bytes read from provided input stream.
 *
 * @param in// ww w  . j a va  2 s . c  o m
 *            reader to read bytes for CRC calculation
 *
 * @return calculated CRC value
 *
 * @throws IOException
 *             if input can't be read.
 */
protected static Long getInputCrc(Reader in) throws IOException {
    char[] buff = new char[BYTES_TO_COUNT_CRC];
    Checksum crc = new CRC32();
    int readLen = in.read(buff);

    if (readLen > 0) {
        String str = new String(buff, 0, readLen);
        final byte[] bytes = str.getBytes(Utils.UTF8);
        crc.update(bytes, 0, bytes.length);
    }

    return crc.getValue();
}

From source file:com.glaf.core.util.IOUtils.java

/**
 * write./* w  w  w. jav a 2  s .c o m*/
 * 
 * @param reader
 *            Reader.
 * @param writer
 *            Writer.
 * @param bufferSize
 *            buffer size.
 * @return count.
 * @throws IOException
 */
public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
    int read;
    long total = 0;
    char[] buf = new char[BUFFER_SIZE];
    while ((read = reader.read(buf)) != -1) {
        writer.write(buf, 0, read);
        total += read;
    }
    return total;
}

From source file:com.t3.persistence.FileUtil.java

/**
 * Because this method takes a <code>Reader</code> it can be locale-correct by
 * creating a FileReader (for example) that uses UTF-8 or some other charset.
 * /*w w w .  j a v a  2s.co m*/
 * @param r the <code>Reader</code> to obtain textual data from
 * @return an array of <code>char</code>
 * @throws IOException
 */
private static String getChars(Reader r) throws IOException {
    if (r == null) {
        throw new IllegalArgumentException("Reader cannot be null");
    }
    try (StringWriter sw = new StringWriter(10 * 1024)) {
        char[] c = new char[4096];
        while (true) {
            int read = r.read(c);

            if (read == -1) {
                break;
            }
            sw.write(c, 0, read);
        }
        return sw.toString();
    }
}