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.microsoft.tfs.jni.helpers.FileCopyHelper.java

private static void copy(final Reader source, final Writer destination) throws IOException {
    final char[] buffer = new char[4096];
    int len;//from ww w .  j  a  v  a  2s  .  c o  m

    while ((len = source.read(buffer)) != -1) {
        destination.write(buffer, 0, len);
    }
}

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

/**
 * Pipes everything from the reader to the writer via a buffer except lines
 * starting with '<?'/*  ww  w  . ja  v a 2 s.c  om*/
 */
private static void pipeString(Reader reader, StringBuilder writer) throws IOException {
    char[] buf = new char[1024];
    int read = 0;
    while ((read = reader.read(buf)) >= 0) {
        if (!(buf[0] == '<' && buf[1] == '?')) {
            writer.append(buf, 0, read);
        }
    }
}

From source file:FileUtil.java

/**
 *  Just copies all characters from <I>in</I> to <I>out</I>.  The copying
 *  is performed using a buffer of bytes.
 *
 *  @since 1.5.8/*from  w  w w. j  a v a 2 s  .c o  m*/
 *  @param in The reader to copy from
 *  @param out The reader to copy to
 *  @throws IOException If reading or writing failed.
 */
public static void copyContents(Reader in, Writer out) throws IOException {
    char[] buf = new char[BUFFER_SIZE];
    int bytesRead = 0;

    while ((bytesRead = in.read(buf)) > 0) {
        out.write(buf, 0, bytesRead);
    }

    out.flush();
}

From source file:Base64.java

private static char[] readChars(final File file) {
    final CharArrayWriter caw = new CharArrayWriter();
    try {//from w ww  . java2s  .c om
        final Reader fr = new FileReader(file);
        final Reader in = new BufferedReader(fr);
        int count;
        final char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0) {
                caw.write(buf, 0, count);
            }
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:eu.prestoprime.p4gui.connection.SearchConnection.java

public static String solrSuggest(P4Service service, final String term) {
    Writer writer = new StringWriter();

    String results = "";
    try {//w  ww .j a v a2 s.  c om
        StringBuilder sb = new StringBuilder();
        sb.append(service.getURL());
        sb.append(SUGGEST_URI);
        sb.append("?term=");
        sb.append(term);
        String path = sb.toString();

        logger.debug("Query to P4WS:\n" + path);

        P4HttpClient client = new P4HttpClient(service.getUserID());

        client.getParams().setParameter("http.protocol.content-charset", "UTF-8");

        HttpRequestBase request = new HttpGet(path);
        HttpResponse response = client.executeRequest(request);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream is = entity.getContent();
            if (is != null) {
                char[] buffer = new char[1024];

                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
                is.close();
            }
        }
        results = writer.toString();
        EntityUtils.consume(entity);
        writer.close();
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error("Either the query was invalid or P4WS could not be accessed.");
        results = null;
    }
    return results;
}

From source file:niclients.main.getni.java

/**
* To convert the InputStream to String we use the
* Reader.read(char[] buffer) method. We iterate until the
* Reader return -1 which means there's no more data to
* read. We use the StringWriter class to produce the string.
* 
* @param is inputStream from where content to be converted is read
*
* @exception throws IOException/*  w w w  .  j av a 2  s. c om*/
*
* @return converted stream string (null if stream is empty)
*/
public static String convertStreamToString(InputStream is) throws IOException {

    if (is != null) {
        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();
    } else {
        return "";
    }
}

From source file:FileCopyUtils.java

/**
 * Copy the contents of the given Reader to the given Writer.
 * Closes both when done./*from w w w  .  j a va 2s  . 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 {

    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) {
            System.out.println("Could not close Reader" + ex);
        }
        try {
            out.close();
        } catch (IOException ex) {
            System.out.println("Could not close Writer:" + ex);
        }
    }
}

From source file:org.kontalk.upload.KontalkBoxUploadConnection.java

public static String responseToString(HttpURLConnection conn, final Charset charset) throws IOException {
    final InputStream instream = conn.getInputStream();
    if (instream == null) {
        return null;
    }/*from  w  ww.jav a 2 s. c om*/
    try {
        int i = conn.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        final Reader reader = new InputStreamReader(instream, charset);
        final StringBuilder buffer = new StringBuilder(i);
        final char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}

From source file:com.moss.appsnap.keeper.freedesktop.FreeDesktopAppHandler.java

private static StringBuilder read(Reader reader) throws IOException {
    StringBuilder appsMenu = new StringBuilder();
    final char[] buf = new char[1024];
    for (int x = reader.read(buf); x != -1; x = reader.read(buf)) {
        appsMenu.append(buf, 0, x);/*from   w w w  .j a  va  2  s .co  m*/
    }
    reader.close();

    return appsMenu;
}

From source file:de.feanor.yeoldemensa.data.MensaFactory.java

private static String convertStreamToString(InputStream is) throws IOException {
    /*/*from   www .ja  v  a  2s.  c o  m*/
     * To convert the InputStream to String we use the Reader.read(char[]
     * buffer) method. We iterate until the Reader return -1 which means
     * there's no more data to read. We use the StringWriter class to
     * produce the string.
     */
    if (is != null) {
        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();
    }
    return "";
}