Java Reader Read All readAllCharsFromReader(Reader reader)

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

Description

Reads and returns all chars from given reader until an EOF is read.

License

Open Source License

Declaration

public static CharArrayWriter readAllCharsFromReader(Reader reader)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Bruno Medeiros and other Contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w w w  .  ja  v  a2s .  c o m*/
 *     Bruno Medeiros - initial implementation
 *******************************************************************************/

import java.io.CharArrayWriter;
import java.io.IOException;

import java.io.Reader;

public class Main {
    public static final int EOF = -1;

    /** Reads and returns all chars from given reader until an EOF is read.
     * Closes reader afterwards. */
    public static CharArrayWriter readAllCharsFromReader(Reader reader)
            throws IOException {
        try {
            final int BUFFER_SIZE = 1024;
            char[] buffer = new char[BUFFER_SIZE];
            CharArrayWriter chars = new CharArrayWriter();

            int read;
            while ((read = reader.read(buffer)) != EOF) {
                chars.write(buffer, 0, read);
            }
            return chars;
        } finally {
            reader.close();
        }
    }
}

Related

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