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

/**
 * Gets the reader content as a String//ww w  . j a v  a 2s.co m
 */
private static String getString(Reader reader) {
    StringBuffer buf = new StringBuffer();
    char[] buffer = new char[1024];
    int count;
    try {
        while ((count = reader.read(buffer)) != -1)
            buf.append(buffer, 0, count);
    } catch (IOException e) {
        return null;
    }
    return buf.toString();
}

From source file:Main.java

public static void transfer(Reader in, StringBuilder out) throws IOException {
    char[] buffer = new char[4096];
    int charsRead;
    while ((charsRead = in.read(buffer)) != -1) {
        out.append(buffer, 0, charsRead);
    }/*from  w  w w  .j a  v  a 2 s .  co m*/
}

From source file:ca.mudar.parkcatcher.io.LocalExecutor.java

/**
 * Loads the JSON text resource with the given ID and returns the JSON
 * content./*w  w  w.j a v  a 2s .c  om*/
 */
public static String loadResourceJson(Context context, int resource) throws IOException {
    InputStream is = context.getResources().openRawResource(resource);
    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();
}

From source file:Main.java

public static String readInputStreamToString(InputStream in) throws IOException {
    Reader r = new InputStreamReader(in);

    StringBuilder sb = new StringBuilder();
    char[] buf = new char[256];
    int bytesRead;
    while ((bytesRead = r.read(buf)) != -1) {
        sb.append(buf, 0, bytesRead);/*from   w w w . j  a v  a2s  .  co  m*/
    }

    try {
        in.close();
    } catch (IOException ignored) {
    }

    return sb.toString();
}

From source file:Main.java

public static String readStr(InputStream in, String charset) throws IOException {
    if (TextUtils.isEmpty(charset))
        charset = "UTF-8";

    if (!(in instanceof BufferedInputStream)) {
        in = new BufferedInputStream(in);
    }//from w  ww.j  a v  a 2  s.  c  o m
    Reader reader = new InputStreamReader(in, charset);
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024];
    int len;
    while ((len = reader.read(buf)) >= 0) {
        sb.append(buf, 0, len);
    }
    return sb.toString().trim();
}

From source file:Main.java

/**
 * Copy a reader to writer.//from w  w w  . j  av  a 2  s  .c o m
 */
private static void copy(Reader in, Writer out) throws IOException {
    try {
        int bytesRead;
        char[] buffer = new char[COPY_BUFFER_SIZE];

        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        out.flush();
    } finally {
        in.close();
        out.close();
    }
}

From source file:Main.java

public static long copy(@NonNull Reader input, @NonNull Writer output) throws IOException {
    char[] buffer = new char[1024 * 4];
    long count = 0;
    int n = 0;/*from w ww.  j a v  a 2s .  c  o  m*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static final long copy(final Reader reader, final StringBuilder builder) throws IOException {
    final char[] buffer = new char[256];
    long count = 0;
    int n = 0;//www  . j  a  va 2 s.c om
    while (-1 != (n = reader.read(buffer))) {
        builder.append(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:org.carewebframework.ui.test.CommonTest.java

/**
 * Reads text from the specified resource on the classpath.
 * /*from w  ww .j ava  2 s .  co m*/
 * @param resourceName Name of the resource.
 * @return Text read from the resource.
 * @throws IOException IO exception.
 */
public static String getTextFromResource(String resourceName) throws IOException {
    Resource resource = desktopContext.getResource("classpath:" + resourceName);
    InputStream is = resource.getInputStream();
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];

    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, StrUtil.CHARSET));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    return writer.toString();
}

From source file:Main.java

public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[4096];
    long count = 0L;
    int n = 0;//  www  .  java  2s . c o m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}