You stated in your comment on Calum's answer that you were going to use
CharStreams.toString(new InputStreamReader(supplier.get(), Charsets.UTF_8))
This code is problematic because the overload
Does not close the
This means that your
InputStreamReader, and by extension the
InputStream returned by
supplier.get(), will not be closed after this code completes.
If, on the other hand, you take advantage of the fact that you appear to already have an
InputSupplier<InputStream> and used the overload
CharStreams.toString(InputSupplier<R extends Readable & Closeable>), the
toString method will handle both the creation and closing of the
Reader for you.
This is exactly what Jon Skeet suggested, except that there isn't actually any overload of
CharStreams.newReaderSupplier that takes an
InputStream as input... you have to give it an
InputSupplier<? extends InputStream> supplier = ...
InputSupplier<InputStreamReader> readerSupplier =
// InputStream and Reader are both created and closed in this single call
String text = CharStreams.toString(readerSupplier);
The point of
InputSupplier is to make your life easier by allowing Guava to handle the parts that require an ugly
try-finally block to ensure that resources are closed properly.
Edit: Personally, I find the following (which is how I'd actually write it, was just breaking down the steps in the code above)
String text = CharStreams.toString(
to be far less verbose than this:
InputStreamReader reader = new InputStreamReader(supplier.get(),
boolean threw = true;
text = CharStreams.toString(reader);
threw = false;
Which is more or less what you'd have to write to handle this properly yourself.
Edit: Feb. 2014
OutputSupplier and the methods that use them have been deprecated in Guava 16.0. Their replacements are
CharSink. Given a
ByteSource, you can now get its contents as a
String like this:
ByteSource source = ...
String text = source.asCharSource(Charsets.UTF_8).read();