Java InputStream Read by Charset toString(InputStream stream, Charset chs)

Here you can find the source of toString(InputStream stream, Charset chs)

Description

Reads the stream into a string

License

Open Source License

Declaration

public static String toString(InputStream stream, Charset chs) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source 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 {
    /**// ww  w . j  a v a 2 s.co m
     * Reads the stream into a string
     */
    public static String toString(InputStream stream, Charset chs) throws IOException {
        if (stream == null)
            return "";

        BufferedReader rd = new BufferedReader(new InputStreamReader(stream, chs));
        int r;
        char[] buffer = new char[1024];
        StringBuilder response = new StringBuilder();
        while ((r = rd.read(buffer, 0, buffer.length)) != -1) {
            response.append(buffer, 0, r);
        }
        rd.close();
        return response.toString();
    }
}

Related

  1. toString(InputStream input, String charset)
  2. toString(InputStream inputStream, Charset charset)
  3. toString(InputStream is, Charset charset)
  4. toString(InputStream is, String charset)
  5. toString(InputStream source, Charset charset)
  6. tryParseString(InputStream is, String... charsets)