Here you can find the source of convertToString(InputStream is, String encoding)
public static String convertToString(InputStream is, String encoding) throws IOException
//package com.java2s; import java.io.*; public class Main { public static String convertToString(InputStream is, String encoding) throws IOException { if (null == is) { return null; }// www . j a va 2 s . c o m BufferedReader reader = new BufferedReader(new InputStreamReader( is, encoding)); char cache[] = new char[2 * 512]; int cacheSize = -1; StringBuilder buffer = new StringBuilder(); while ((cacheSize = reader.read(cache)) != -1) { buffer.append(new String(cache, 0, cacheSize)); cacheSize = reader.read(cache); } is.close(); return buffer.toString(); } public static String toString(InputStream is) throws IOException { if (null == is) { return null; } return toStringBuffer(is).toString(); } public static StringBuilder toStringBuffer(InputStream is) throws IOException { if (null == is) { return null; } BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuilder buffer = new StringBuilder(); String line = null; while ((line = in.readLine()) != null) { buffer.append(line).append("\n"); } is.close(); return buffer; } }