Java BufferedReader Read All readAll(InputStream input)

Here you can find the source of readAll(InputStream input)

Description

Read everything from an input stream into a String, reconstructing line endings.

License

Open Source License

Parameter

Parameter Description
input the stream to be read

Return

a string that contains the content of input

Declaration

public static String readAll(InputStream input) 

Method Source Code

//package com.java2s;
/*/* w  w w.j a  v  a  2  s  .  co  m*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2004-2009, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

import java.io.BufferedReader;

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

public class Main {
    /**
     * Read everything from an input stream into a String, reconstructing line endings.
     * 
     * @param input
     *            the stream to be read
     * @return a string that contains the content of input
     */
    public static String readAll(InputStream input) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        StringBuffer buffer = new StringBuffer();
        while (true) {
            String line;
            try {
                line = reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (line == null) {
                break;
            } else {
                buffer.append(line);
                buffer.append("\n");
            }
        }
        return buffer.toString();
    }
}

Related

  1. readAll(File input)
  2. readAll(final Reader reader)
  3. readAll(InputStream in)
  4. readAll(InputStream in)
  5. readAll(InputStream in, String encoding)
  6. readAll(InputStream stream)
  7. readAll(Reader reader)
  8. readAll(Reader reader)
  9. readAll(Reader reader)