Java InputStream to String getStreamAsString(final InputStream stream)

Here you can find the source of getStreamAsString(final InputStream stream)

Description

Gets a stream as String.

License

Open Source License

Parameter

Parameter Description
stream The input stream

Exception

Parameter Description
IOException Thrown if the resource cannot be located.

Return

The resource as String.

Declaration

public static String getStreamAsString(final InputStream stream) throws IOException 

Method Source Code


//package com.java2s;
/*/*w w w  . ja v a2  s.c  o  m*/
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at license/ESCIDOC.LICENSE
* or http://www.escidoc.org/license.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at license/ESCIDOC.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

import java.io.BufferedReader;

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

public class Main {
    /**
     * Gets a stream as String.
     *
     * @param stream The input stream
     * @return The resource as String.
     * @throws IOException Thrown if the resource cannot be located.
     */
    public static String getStreamAsString(final InputStream stream) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        String line = null;
        StringBuilder result = new StringBuilder();
        while ((line = br.readLine()) != null) {
            result.append(line);
            result.append("\n");
        }
        stream.close();
        return result.toString();
    }
}

Related

  1. getInputStreamAsString(InputStream stream)
  2. getInputStreamContent(InputStream i, String encoding)
  3. getInputStreamContent(InputStream inputStream)
  4. getInputStreamContents(InputStream inputStream)
  5. getInputStreamContents(InputStream inputStream)
  6. getStreamAsString(InputStream input, String charset)
  7. getStreamAsString(InputStream is, String encoding)
  8. getStreamAsString(InputStream stream)
  9. getStreamAsString(InputStream stream, String charset)