Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream
to a String.
Since I am trying to move away from Apache Common
Based on the accepted answer, here is a utility method that mocks the behavior of IOUtils.toString()
(and an overloaded version with a charset, as well). This version should be safe, right?
public static String toString(final InputStream is) throws IOException{
return toString(is, Charsets.UTF_8);
}
public static String toString(final InputStream is, final Charset cs)
throws IOException{
Closeable closeMe = is;
try{
final InputStreamReader isr = new InputStreamReader(is, cs);
closeMe = isr;
return CharStreams.toString(isr);
} finally{
Closeables.closeQuietly(closeMe);
}
}