Java Reader Read All readAllChars(Reader reader)

Here you can find the source of readAllChars(Reader reader)

Description

Reads all characters from a reader.

License

Open Source License

Parameter

Parameter Description
reader the reader to read from

Exception

Parameter Description
IOException If an I/O error occurs

Return

an array of Char's

Declaration

public static char[] readAllChars(Reader reader) throws IOException 

Method Source Code

//package com.java2s;
/*//from w  w w.ja v a 2s.  c  om
 * Copyright 2004-2009 Luciano Vernaschi
 *
 * This file is part of MeshCMS.
 *
 * MeshCMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MeshCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MeshCMS.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.CharArrayWriter;

import java.io.IOException;

import java.io.Reader;

public class Main {
    /**
     * A standard size for buffers.
     */
    public static final int BUFFER_SIZE = 2048;

    /**
     * Reads all characters from a reader.
     *
     * @param reader the reader to read from
     *
     * @return  an array of <code>Char</code>'s
     *
     * @throws IOException If an I/O error occurs
     */
    public static char[] readAllChars(Reader reader) throws IOException {
        CharArrayWriter caw = new CharArrayWriter();
        char[] cbuf = new char[BUFFER_SIZE];
        int n;

        while ((n = reader.read(cbuf)) != -1) {
            caw.write(cbuf, 0, n);
        }

        return caw.toCharArray();
    }
}

Related

  1. readAll(Reader reader)
  2. readAll(Reader reader)
  3. readAll(Reader reader)
  4. ReadAll_Variant1(Reader rd, Writer wr)
  5. readAllChars(Reader reader)
  6. readAllCharsAndClose(Reader reader)
  7. readAllCharsFromReader(Reader reader)
  8. readAllFrom(final Reader reader)
  9. readAllLines(final BufferedReader br)