Here you can find the source of toString(InputStream in, Charset charset)
public static String toString(InputStream in, Charset charset) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class Main { public static String toString(InputStream in) throws IOException { return toString(in, StandardCharsets.UTF_8); }/*from w w w . j a v a 2 s . co m*/ public static String toString(InputStream in, Charset charset) throws IOException { return toString(new InputStreamReader(in, charset)); } public static String toString(Reader reader) throws IOException { try { char[] arr = new char[8 * 1024]; StringBuilder buffer = new StringBuilder(); int numCharsRead; while ((numCharsRead = reader.read(arr, 0, arr.length)) != -1) { buffer.append(arr, 0, numCharsRead); } return buffer.toString(); } finally { reader.close(); } } }