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 ( ' ' ) ; } } } finally { if ( isr != null ) { isr . close ( ) ; } } return sb . toString ( ) ; }
Read the text stream using the given encoding .