Java InputStream Read by Charset inputstreamToString(InputStream input, CharsetDecoder decoder)

Here you can find the source of inputstreamToString(InputStream input, CharsetDecoder decoder)

Description

Reads an input stream into a string.

License

Apache License

Parameter

Parameter Description
input the input stream to read
decoder character decoder to use, if null, system default character set is used

Exception

Parameter Description
IOException thrown if there is a problem reading from the stream and decoding it

Return

the string read from the stream

Declaration

public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException 

Method Source Code


//package com.java2s;
/*/* www  .  j  a  v a 2s . c om*/
 * Copyright 2005 University Corporation for Advanced Internet Development, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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;
import java.nio.charset.CharsetDecoder;

public class Main {
    /**
     * Reads an input stream into a string. The provide stream is <strong>not</strong> closed.
     * 
     * @param input the input stream to read
     * @param decoder character decoder to use, if null, system default character set is used
     * 
     * @return the string read from the stream
     * 
     * @throws IOException thrown if there is a problem reading from the stream and decoding it
     */
    public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException {
        CharsetDecoder charsetDecoder = decoder;
        if (decoder == null) {
            charsetDecoder = Charset.defaultCharset().newDecoder();
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder));

        StringBuilder stringBuffer = new StringBuilder();
        String line = reader.readLine();
        while (line != null) {
            stringBuffer.append(line).append("\n");
            line = reader.readLine();
        }

        reader.close();

        return stringBuffer.toString();
    }
}

Related

  1. getStreamAsString(InputStream source, Charset charset)
  2. getText(InputStream stream, Charset charset)
  3. input2String(InputStream input, Charset encoding)
  4. inputStreamAsString(InputStream stream, Charset cs)
  5. inputStreamToString(final InputStream inputStream, final String charsetName)
  6. inputStreamToString(InputStream inputStream, Charset charset)
  7. inputStreamToString(InputStream inputStream, String charset)
  8. inputStreamToString(InputStream is, Charset charset)
  9. isCharsetMisInterpreted(String input, String encoding)