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.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void transfer(Reader reader, Writer writer, long limit) throws IOException {
    char[] buf = new char[1024];
    int readcount;
    long total = 0;
    while ((readcount = reader.read(buf)) != -1) {
        if (limit != -1) {
            if (total + readcount > limit)
                readcount = (int) (limit - total);
        }/*from   ww  w .  j  a  v a 2  s  .  co m*/

        if (readcount > 0)
            writer.write(buf, 0, readcount);

        total += readcount;
        if (limit != -1 && total >= limit)
            break;
    }
    writer.flush();
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private static String readContents(InputStream is) {
    StringWriter wr = new StringWriter();

    try {//from  ww w. ja  va 2 s.  c  o m
        Reader rd = new InputStreamReader(is, "UTF-8");

        char[] buf = new char[1024];
        int read;
        while ((read = rd.read(buf)) > 0) {
            wr.write(buf, 0, read);
        }

    } catch (Exception e) {
        LOG.error("An error occurred while reading the template file.", e);
    }

    return wr.toString();
}

From source file:net.opentracker.android.OTSend.java

/**
 * getResponseBody function gives out the HTTP POST data from the given
 * httpResponse output: data from the http as string input : httpEntity type
 * //from   w w  w.  j a v a2s  .  co  m
 * based on:
 * http://thinkandroid.wordpress.com/2009/12/30/getting-response-body
 * -of-httpresponse/
 */
private static String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    LogWrapper.v(TAG, "getResponseBody(final HttpEntity entity)");

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    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 = EntityUtils.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:de.thischwa.pmcms.tool.file.FileTool.java

/**
 * Return the content of a file as string. Encoding is utf-8.
 *//*from   ww  w  .  j a  v  a2 s . c  o m*/
public static String toString(File file) throws IOException {
    StringBuffer fileData = new StringBuffer(2048 * 5);
    Reader reader = null;
    try {
        reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), "utf-8");
        char[] buf = new char[2048];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(reader);
    }
    return fileData.toString();

}

From source file:com.cloudant.tests.UnicodeTest.java

/**
 * Copies the content of an entity to an Appendable, as a sequence of chars.
 *
 * @param destination An Appendable (such as a StringBuilder, a Writer, or a PrintStream).
 * @param reader      A Reader that wraps the InputStream of the entity returned by the given
 *                    URI.//from   ww w.  jav  a  2 s .c  o  m
 *                    Should be buffered.
 * @throws RuntimeException if there is an exception reading the entity
 * @throws IOException      if there is an exception writing to the destination
 */
private static void pipeEntityContentAsChars(Appendable destination, Reader reader, URI uri)
        throws IOException {
    char[] buffer = new char[1024];
    for (;;) {
        int n;
        try {
            n = reader.read(buffer);
        } catch (SocketException e) {
            // At EOF, we may get this exception:
            // java.net.SocketException: Socket is closed
            //   at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1284)
            //   at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)
            //   at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer
            // (AbstractSessionInputBuffer.java:149)
            //   at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer
            // .java:110)
            //   at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine
            // (AbstractSessionInputBuffer.java:264)
            //   at org.apache.http.impl.io.ChunkedInputStream.getChunkSize
            // (ChunkedInputStream.java:246)
            //   at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream
            // .java:204)
            //   at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:167)
            //   at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream
            // .java:138)
            //   at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
            //   at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
            //   at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
            //   at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
            //   at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
            //   at java.io.InputStreamReader.read(InputStreamReader.java:167)
            //   at java.io.Reader.read(Reader.java:123)
            // See also http://stackoverflow
            // .com/questions/17040698/httpcomponentss-ssl-connection-results-in-socket-is
            // -closed
            break;
        } catch (IOException e) {
            throw new RuntimeException("Error reading from " + uri, e);
        }
        if (n < 0) {
            break;
        }
        destination.append(CharBuffer.wrap(buffer, 0, n));
    }
}

From source file:de.alpharogroup.file.read.ReadFileUtils.java

/**
 * The Method reader2String() reads the data from the Reader into a String.
 *
 * @param reader//from   ww  w  .ja  va  2s  .c om
 *            The Reader from where we read.
 * @return The String that we read from the Reader.
 */
public static String reader2String(final Reader reader) {
    final StringBuffer stringBuffer = new StringBuffer();
    final char[] charArray = new char[FileConst.BLOCKSIZE];
    int tmp;
    try {
        while ((tmp = reader.read(charArray)) > 0) {
            stringBuffer.append(charArray, 0, tmp);
        }
    } catch (final IOException e) {
        LOGGER.log(Level.SEVERE, "reader2String failed...\n" + e.getMessage(), e);
    } finally {
        StreamUtils.closeReader(reader);
    }
    return stringBuffer.toString();
}

From source file:de.alpharogroup.file.read.ReadFileExtensions.java

/**
 * The Method reader2String() reads the data from the Reader into a String.
 *
 * @param reader//from   www  . ja v a2  s .  com
 *            The Reader from where we read.
 * @return The String that we read from the Reader.
 */
public static String reader2String(final Reader reader) {
    final StringBuffer stringBuffer = new StringBuffer();
    final char[] charArray = new char[FileConst.BLOCKSIZE];
    int tmp;
    try {
        while ((tmp = reader.read(charArray)) > 0) {
            stringBuffer.append(charArray, 0, tmp);
        }
    } catch (final IOException e) {
        LOGGER.log(Level.SEVERE, "reader2String failed...\n" + e.getMessage(), e);
    } finally {
        StreamExtensions.closeReader(reader);
    }
    return stringBuffer.toString();
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * //from  w w  w  .j ava 2s  .  c  o m
 * @param authnRequest
 *            the authn request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectDecode(final String encodedRequest) throws IOException {
    String inflatedRequest = null;

    ByteArrayInputStream bytesIn = null;
    InflaterInputStream inflater = null;

    final byte[] decodedBytes = Base64.decode(encodedRequest);

    try {
        bytesIn = new ByteArrayInputStream(decodedBytes);
        inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];

        final Reader reader = new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        inflatedRequest = writer.toString();
    } finally {
        if (bytesIn != null) {
            bytesIn.close();
        }
        if (inflater != null) {
            inflater.close();
        }
    }

    return inflatedRequest;
}

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

/**
 * ??, ?. ?./*from  www.j a  v  a  2  s .  co  m*/
 * 
 * @param in ?
 * @param out ?
 * @param bufferSize ?()
 * @throws IOException 
 */
public static void io(Reader in, Writer out, int bufferSize) throws IOException {
    if (bufferSize == -1) {
        bufferSize = DEFAULT_BUFFER_SIZE >> 1;
    }

    char[] buffer = new char[bufferSize];
    int amount;

    while ((amount = in.read(buffer)) >= 0) {
        out.write(buffer, 0, amount);
    }
}

From source file:com.navercorp.pinpoint.profiler.modifier.connector.httpclient4.interceptor.AbstractHttpRequestExecute.java

/**
 * copy: EntityUtils//  www  . j ava 2s .  c o m
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 */
public static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, int maxLength)
        throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        if (entity.getContentLength() > Integer.MAX_VALUE) {
            return "HTTP entity too large to be buffered in memory length:" + entity.getContentLength();
        }
        String charset = getContentCharSet(entity);
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEFAULT_CONTENT_CHARSET;
        }
        Reader reader = new InputStreamReader(instream, charset);
        final StringBuilder buffer = new StringBuilder(maxLength * 2);
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
            if (buffer.length() >= maxLength) {
                break;
            }
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}