Java InputStreamReader Read All readAll(Reader source)

Here you can find the source of readAll(Reader source)

Description

Read all of the data from a source.

License

Open Source License

Exception

Parameter Description
IOException When an attempt to read from the source fails.

Declaration

public static String readAll(Reader source) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  www  .  ja v  a2s.c o  m*/
 * Copyright (c) 2013-14 Samuel A. Rebelsky.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Main {
    /**
     * The default buffer size (e.g., for reading data)
     */
    static final int DEFAULT_BUFFER_SIZE = 1024;

    /**
     * Read all of the data from a source using a specified buffer size.
     *
     * Based on code by Paul de Vrieze and found at
     *   http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
     *
     * @param source
     *   The source to read the data from.
     * @param bufsize
     *    The number of bytes to read at a time.  <code>DEFAULT_BUFFER_SIZE</code>
     *    is a reasonable buffer size.
     * @throws IOException
     *   If an I/O error occurs.
     */
    public static String readAll(Reader source, final int bufsize)
            throws IOException {
        final char[] buf = new char[bufsize];
        final StringBuilder result = new StringBuilder();
        int size;

        while ((size = source.read(buf, 0, bufsize)) >= 0) {
            result.append(buf, 0, size);
        } // while

        return result.toString();
    }

    /**
     * Read all of the data from a source.
     *
     * @throws IOException
     *   When an attempt to read from the source fails.
     */
    public static String readAll(Reader source) throws IOException {
        return readAll(source, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Read all of the data from a source, using a particular encoding.
     *
     * @throws IOException
     *   When an attempt to read from the source fails.
     */
    public static String readAll(InputStream source, String encoding)
            throws IOException {
        InputStreamReader reader = new InputStreamReader(source, encoding);
        String result = readAll(reader);
        reader.close();
        return result;
    }

    /**
     * Read all of the data from a source, using the default (UTF-8)
     * encoding.
     *
     * @throws IOException
     *   When an attempt to read from the source fails.
     */
    public static String readAll(InputStream source) throws IOException {
        return readAll(source, "UTF-8");
    }
}

Related

  1. readAll(File file)
  2. readAll(InputStream input)
  3. readAll(InputStreamReader reader)