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 abstract int read(char cbuf[], int off, int len) throws IOException;

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:Main.java

/**
 * Load the characters contained in specified source.
 * //from w  w  w .j a  v  a  2 s. c  o  m
 * @param in the character source
 * @return the contents of the character source
 * @exception IOException if there is an I/O problem
 */
public static String loadChars(Reader in) throws IOException {
    StringBuilder buffer = new StringBuilder();
    char[] chars = new char[65536];
    int count;

    while ((count = in.read(chars, 0, chars.length)) != -1) {
        if (count > 0) {
            buffer.append(chars, 0, count);
        }
    }

    return buffer.toString();
}

From source file:Util.java

public static CharSequence contentsAsCharSequence(Reader reader) throws java.io.IOException {
    final int BUFSIZE = 2048;
    char[] buf = new char[BUFSIZE];
    int count;/*w ww.  ja v  a2s .  co m*/
    StringBuffer sb = new StringBuffer(BUFSIZE);
    do {
        count = reader.read(buf, 0, BUFSIZE);
        if (count == -1)
            break;
        //System.out.println ("count="+count);
        sb.append(buf, 0, count);
    } while (count == BUFSIZE);
    return sb;
}

From source file:org.ocpsoft.redoculous.tests.HttpAction.java

/**
 * Return a {@link String} containing the contents of the given {@link InputStream}
 *//*  w w  w. ja  va  2 s. c  om*/
public static String toString(final InputStream stream) {
    StringBuilder out = new StringBuilder();
    try {
        final char[] buffer = new char[0x10000];
        Reader in = new InputStreamReader(stream, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toString();
}

From source file:com.joliciel.talismane.utils.StringUtils.java

public static String readerToString(Reader reader) {
    try {/* w  w  w.ja v a  2 s. c  om*/
        char[] chars = new char[1024];
        StringBuilder sb = new StringBuilder();
        int numChars;

        while ((numChars = reader.read(chars, 0, chars.length)) > 0) {
            sb.append(chars, 0, numChars);
        }

        return sb.toString();
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.hagreve.android.lib.HaGreveApi.java

private static String readStream(InputStream in) throws IOException {
    Reader reader = new BufferedReader(new InputStreamReader(in));
    char buffer[] = new char[READ_BUFFER_SIZE];
    String contents = "";
    int read = 0;
    while ((read = reader.read(buffer, 0, READ_BUFFER_SIZE)) > 0) {
        contents += String.valueOf(buffer, 0, read);
    }/* w w w. j a  v  a  2 s  .  c  o m*/

    return contents;
}

From source file:StringIO.java

/**
 * Read the entire contents of the specified Reader and return a
 * single String object containing the contents of the InputStream.
 * This method does not return until the end of the input file or
 * stream is reached./*from  w w  w  . ja v  a 2 s .  c om*/
 *
 * @param reader a Reader from which to read
 *
 * @return a String containing the contents of the stream
 *
 * @throws IOException if an I/O error occurs while reading the input stream
 */
public static String readFully(Reader reader) throws IOException {
    char[] arr = new char[8 * 1024]; // 8K at a time
    StringBuffer buf = new StringBuffer();
    int numChars;

    while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
        buf.append(arr, 0, numChars);
    }

    return buf.toString();
}

From source file:com.googlecode.starflow.core.script.spel.SpelScriptEngine.java

private static final String readFully(Reader reader) throws ScriptException {
    char[] arr = new char[8 * 1024];
    StringBuilder buf = new StringBuilder();
    int numChars;
    try {/*from   w  w w  .  j  a  v a  2  s .c  o  m*/
        while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
            buf.append(arr, 0, numChars);
        }
    } catch (IOException exp) {
        throw new ScriptException(exp);
    }
    return buf.toString();
}

From source file:com.google.dart.tools.designer.model.HtmlRenderHelper.java

/**
 * Read the requested number of characters or fail if there are not enough left.
 * <p>//from  w  w w .ja  v  a2  s  . c om
 * TODO(scheglov) update to modern Commons-IO which has this method.
 */
private static void readFully(Reader input, char[] buffer) throws IOException {
    int length = buffer.length;
    int remaining = length;
    while (remaining > 0) {
        int location = length - remaining;
        int count = input.read(buffer, location, remaining);
        if (count == -1) {
            throw new EOFException("Length to read: " + length + " actual: " + (length - remaining));
        }
        remaining -= count;
    }
}

From source file:ar.com.tadp.xml.rinzo.core.utils.FileUtils.java

public static char[] readContents(Reader reader) throws IOException {
    int i = 4096;
    char ac[] = new char[i];
    char ac1[] = new char[0];
    int j = 0;//from   w ww .  j a v a2 s . c  o m
    do {
        int k = reader.read(ac, 0, i);
        if (k < 0)
            break;
        if (k > 0) {
            int l = ac1.length;
            char ac2[] = new char[l + k];
            System.arraycopy(ac1, 0, ac2, 0, l);
            System.arraycopy(ac, 0, ac2, l, k);
            ac1 = ac2;
            if (++j >= 8 && i < 300000) {
                j = 0;
                i *= 2;
                ac = new char[i];
            }
        }
    } while (true);
    return ac1;
}

From source file:com.galactogolf.genericobjectmodel.levelloader.LevelSet.java

private static LevelSet LoadLevelSetFromStream(InputStream input) throws LevelLoadingException {
    LevelSet levelSet;//  ww w.  j  a  va2  s .c  o m

    StringBuilder sb = new StringBuilder();
    final char[] buffer = new char[500];

    Reader in = new InputStreamReader(input);
    int read;
    try {
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                sb.append(buffer, 0, read);
            }
        } while (read >= 0);
    } catch (IOException e) {
        Log.e("File loading error", e.getMessage());
        throw new LevelLoadingException(e.getMessage());
    }
    String data = sb.toString();
    try {
        levelSet = (JSONSerializer.fromLevelSetJSON(new JSONObject(data)));
    } catch (JSONException e) {
        Log.e("File loading error", e.getMessage());
        throw new LevelLoadingException(e.getMessage());
    }

    return levelSet;
}