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:io.github.azige.mages.Util.java

static String readAll(Reader in) throws IOException {
    StringBuilder sb = new StringBuilder();
    char[] buffer = new char[BUFFER_SIZE];
    for (int b; (b = in.read(buffer)) != -1;) {
        sb.append(buffer, 0, b);//from www .  j ava 2  s.  c o m
    }
    return sb.toString();
}

From source file:Main.java

public static String descomprimeComGZIP(byte[] bytes, String encoding) {
    String decompressedString = "";
    try {//from   w  w  w .jav  a2  s  .c  o m
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decompressedString;
}

From source file:com.nebhale.jsonpath.testutils.JsonUtils.java

private static String readFile(String file) throws IOException {
    StringBuilder sb = new StringBuilder();

    Reader in = null;
    try {//from w  w w . j  av a 2  s .c om
        in = new FileReader(file);

        char[] buffer = new char[8192];
        int length;
        while ((length = in.read(buffer)) != -1) {
            sb.append(buffer, 0, length);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }

    return sb.toString();
}

From source file:Streams.java

public static void drain(Reader r, Writer w) throws IOException {
    char[] bytes = new char[BLOCK_SIZE];
    try {//from w  ww . j a v  a 2s  .co m
        int length = r.read(bytes);
        while (length != -1) {
            if (length != 0) {
                w.write(bytes, 0, length);
            }
            length = r.read(bytes);
        }
    } finally {
        bytes = null;
    }

}

From source file:Main.java

/**
 * Copies characters from a reader to a writer. When the reading is done,
 * the reader is closed./*w  ww.j  a  va2  s  .  c  o  m*/
 * 
 * @param reader
 *            The reader.
 * @param writer
 *            The writer.
 * @throws IOException
 */
public static void copy(Reader reader, java.io.Writer writer) throws IOException {
    int charsRead;
    char[] buffer = new char[2048];

    while ((charsRead = reader.read(buffer)) > 0) {
        writer.write(buffer, 0, charsRead);
    }

    writer.flush();
    reader.close();
}

From source file:Main.java

/**
 * Descomprime uma string utilizando o GZIP
 * /*from w  ww .  j a v a  2  s . c  o  m*/
 * @param str
 * @param encoding
 * @return
 */
public static String gzipDecompress(byte[] bytes, String encoding) {
    String decompressedString = "";

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return decompressedString;
}

From source file:Main.java

/**
 * Descomprime uma string utilizando o GZIP
 * // w w w.  j  a v  a 2s  .c  o m
 * @param str
 * @param encoding
 * @return
 */
public static String gzipDecompressString(String str, String encoding) {
    String decompressedString = "";

    try {
        byte[] bytes = Base64.decodeBase64(str.getBytes(encoding));
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return decompressedString;
}

From source file:Util.java

/**
 * Charge le contenu d'un stream dans un string
 * /*from   w w  w.j a  v  a 2s.c  o m*/
 * @param stream
 * @return
 * @throws IOException
 */
public static String loadStream(InputStream stream) throws IOException {
    Reader reader = new InputStreamReader(stream, Charset.forName("UTF-8"));
    char[] buffer = new char[1024];
    int count;
    StringBuilder str = new StringBuilder();
    while ((count = reader.read(buffer)) != -1)
        str.append(buffer, 0, count);
    return str.toString();
}

From source file:Main.java

/** Returns the remainder of 'reader' as a string, closing it when done. */
public static String readFully(Reader reader) throws IOException {
    try {/*from   w  w w . ja  v a 2s.  co  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:Main.java

public static int copy(Reader input, Writer output) throws IOException {
    char[] buffer = new char[10 * 1024];
    int count = 0;
    int n = 0;//from  www. j  a  v a2 s .c om
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}