public static String convertStreamToString ( InputStream is , String charset ) { if ( is == null ) return null ; StringBuilder sb = new StringBuilder ( ) ; String line ; long linecount = 0 ; long size = 0 ; try { // TODO: Can this be a performance killer if many many lines or is BufferedReader handling that in a good way? boolean emptyBuffer = true ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( new BOMStripperInputStream ( is ) , charset ) ) ; while ( ( line = reader . readLine ( ) ) != null ) { // Skip adding line break before the first line if ( emptyBuffer ) { emptyBuffer = false ; } else { sb . append ( ' ' ) ; size ++ ; } sb . append ( line ) ; linecount ++ ; size += line . length ( ) ; if ( logger . isTraceEnabled ( ) ) { if ( linecount % 50000 == 0 ) { logger . trace ( "Lines read: {}, {} characters and counting..." , linecount , size ) ; printMemUsage ( ) ; } } } } catch ( IOException e ) { throw new RuntimeException ( e ) ; } finally { if ( logger . isTraceEnabled ( ) ) { logger . trace ( "Lines read: {}, {} characters" , linecount , size ) ; printMemUsage ( ) ; } // Ignore exceptions on call to the close method try { if ( is != null ) is . close ( ) ; } catch ( IOException e ) { } } return sb . toString ( ) ; }
Converts an InputStream to a String .