Java InputStream Read by Charset convertStreamToString(final InputStream is, final Charset charset)

Here you can find the source of convertStreamToString(final InputStream is, final Charset charset)

Description

Converts an input stream into a string value.

License

Open Source License

Parameter

Parameter Description
is an input stream

Exception

Parameter Description
IOException upon a failure reading the input stream

Return

a string containing the content of the input stream

Declaration

public static String convertStreamToString(final InputStream is, final Charset charset) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w . j a va  2 s .c o m
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {
    /**
     * Converts an input stream into a string value.
     *
     * @param is an input stream
     * @return a string containing the content of the input stream
     * @throws IOException upon a failure reading the input stream
     */
    public static String convertStreamToString(final InputStream is, final Charset charset) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));
        final StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    }
}

Related

  1. asString(final InputStream is, Charset charset)
  2. collectStream(InputStream stream, Charset charset)
  3. consume(InputStream stream, Charset encoding)
  4. convertEncoding(Charset output_charset, String input_string)
  5. convertFromUnicode(String input, String targetCharset)
  6. convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)
  7. copyToString(InputStream in, Charset charset)
  8. createInput(String s, String charsetName)
  9. createZipInputStream(InputStream inStream, Charset charset)