Java InputStream to String inputStreamToString(InputStream inputStream)

Here you can find the source of inputStreamToString(InputStream inputStream)

Description

Converts the inputStream to String.

License

Open Source License

Parameter

Parameter Description
inputStream the input stream

Return

the content as string.

Declaration

public static String inputStreamToString(InputStream inputStream) 

Method Source Code


//package com.java2s;
/*/*from  w  w w.  j a v  a2s . co m*/
 * dataZ - Test Support For Data Stores.
 *
 * Copyright 2014-2017 the original author or authors.
 *
 * 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
 */

import java.io.*;

public class Main {
    /**
     * Converts the {@code inputStream} to String. The {@code inputStream} will be consumed.
     *
     * @param inputStream the input stream
     * @return the content as string.
     */
    public static String inputStreamToString(InputStream inputStream) {
        final StringBuilder output = new StringBuilder();
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while (null != (line = reader.readLine())) {
                output.append(line).append('\n');
            }
        } catch (IOException ex) {
            throw new RuntimeException("Caught IO Exception", ex);
        }

        return output.toString();
    }
}

Related

  1. inputStreamToString(InputStream input)
  2. InputStreamToString(InputStream input_stream)
  3. inputStreamToString(InputStream inputStream)
  4. InputStreamToString(InputStream inputStream)
  5. InputStreamToString(InputStream inputStream)
  6. inputStreamToString(InputStream inputStream)
  7. inputStreamToString(InputStream inputStream)
  8. inputStreamToString(InputStream inputStream)
  9. inputStreamToString(InputStream inputStream, String charsetName)