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:Main.java

private static String inputStreamToString(InputStream stream) throws IOException {
    Reader reader = new InputStreamReader(stream, "UTF-8");
    StringBuilder sb = new StringBuilder();
    char[] buffer = new char[1024];
    int num;//from w  w w  . j a v  a2  s  .c  o m
    while (0 < (num = reader.read(buffer))) {
        sb.append(buffer, 0, num);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Transfers all characters that can be read from <tt>in</tt> to
 * <tt>out</tt>.// w  w w  . jav  a  2 s  .  c o m
 *
 * @param in The Reader to read characters from.
 * @param out The Writer to write characters to.
 * @return The total number of characters transfered.
 */
public static final long transfer(Reader in, Writer out) throws IOException {
    long totalChars = 0;
    int charsInBuf = 0;
    char[] buf = new char[4096];

    while ((charsInBuf = in.read(buf)) != -1) {
        out.write(buf, 0, charsInBuf);
        totalChars += charsInBuf;
    }

    return totalChars;
}

From source file:de.micromata.genome.util.text.StandardHeaderSplitter.java

/**
 * Slurp./*  w w w .  j a va  2 s . c  o  m*/
 *
 * @param in the in
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static String slurp(Reader in) throws IOException {
    StringBuilder sb = new StringBuilder();
    char[] b = new char[4096];
    for (int n; (n = in.read(b)) != -1;) {
        sb.append(new String(b, 0, n));
    }
    return sb.toString();
}

From source file:com.swordlord.gozer.file.GozerFileLoader.java

public static String convertInputStreamToString(InputStream is) {
    String strReturn = "";

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

        char[] buffer = new char[1024];
        try {/* ww w . j  a v  a 2s . com*/
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            reader.close();
            strReturn = writer.toString();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    return strReturn;
}

From source file:Main.java

public static String readerToString(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    int bufferSize = 4096;
    char[] buf = new char[bufferSize];

    int readed;//from  w  w  w  . j av  a 2s  .  co  m
    while ((readed = reader.read(buf)) > 0) {
        builder.append(buf, 0, readed);
    }

    return builder.toString();
}

From source file:net.sf.keystore_explorer.utilities.io.CopyUtil.java

/**
 * Copy data from a reader to a writer and do not close I/O.
 *
 * @param reader/* w w w . ja v  a  2 s .  c o  m*/
 *            Reader
 * @param writer
 *            Writer
 * @throws IOException
 *             If an I/O problem occurred
 */
public static void copy(Reader reader, Writer writer) throws IOException {
    char[] buffer = new char[2048];
    int i;
    while ((i = reader.read(buffer)) > 0) {
        writer.write(buffer, 0, i);
    }
}

From source file:llc.rockford.webcast.StringUtils.java

public static String convertStreamToString(InputStream is) throws IOException {
    /*//from www. ja va  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();
    } else {
        return "";
    }
}

From source file:Main.java

static String readFully(Reader reader) throws IOException {
    try {/*from ww w. j  a va  2 s.c o  m*/
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}

From source file:com.scorpio4.util.io.StreamCopy.java

public static void copy(Reader input, Writer output, int buffer_size) throws IOException {
    char[] buffer = new char[buffer_size + 16];
    int n = 0;/*w  w  w.ja  va  2 s. com*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        output.flush();
    }
    output.flush();
}

From source file:Main.java

/**
 * Read and return the entire contents of the supplied {@link File}.
 * /*  w  w  w .  j  a  v a2s  .c  o m*/
 * @param file the file containing the information to be read; may be null
 * @return the contents, or an empty string if the supplied reader is null
 * @throws IOException if there is an error reading the content
 */
public static String read(File file) throws IOException {
    if (file == null)
        return "";
    StringBuilder sb = new StringBuilder();
    boolean error = false;
    Reader reader = new FileReader(file);
    try {
        int numRead = 0;
        char[] buffer = new char[1024];
        while ((numRead = reader.read(buffer)) > -1) {
            sb.append(buffer, 0, numRead);
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error closing reader
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error closing reader
        throw e;
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            if (!error)
                throw e;
        }
    }
    return sb.toString();
}