public static String readStream(InputStream stream, String encoding) throws IOException {
    InputStreamReader isr = null;
    StringBuilder sb = new StringBuilder();
    try {
      if (encoding == null) {
        isr = new InputStreamReader(stream);
      } else {
        isr = new InputStreamReader(stream, encoding);
      }
      try (BufferedReader br = new BufferedReader(isr)) {
        String line;
        while ((line = br.readLine()) != null) {
          sb.append(line);
          sb.append('\n');
        }
      }
    } finally {
      if (isr != null) {
        isr.close();
      }
    }
    return sb.toString();
  }
public static String readStream ( InputStream stream , String encoding ) throws IOException { InputStreamReader isr = null ; StringBuilder sb = new StringBuilder ( ) ; try { if ( encoding == null ) { isr = new InputStreamReader ( stream ) ; } else { isr = new InputStreamReader ( stream , encoding ) ; } try ( BufferedReader br = new BufferedReader ( isr ) ) { String line ; while ( ( line = br . readLine ( ) ) != null ) { sb . append ( line ) ; sb . append ( '\n' ) ; } } } finally { if ( isr != null ) { isr . close ( ) ; } } return sb . toString ( ) ; }
Read the text stream using the given encoding.

@param stream InputStream the stream to be read
@param encoding the stream's character encoding, e.g. {@code utf-8}, or {@code null} to use the system encoding
@return a string with the stream's content, lines separated by {@code \n} (note that {@code \n} will
be added to the last line even if it is not in the stream)
@since 2.3