Is it easy to convert an InputStream to a BufferedReader using Guava?
I am looking for something like:
InputStream inputStream = ...; BufferedReader br = Streams.newBufferedReader(inputStream);
I can open files using Files.newReader(File file, Charset charset) . This is cool, and I want to do the same with InputStream .
UPDATE:
Using CharStreams.newReaderSupplier seems CharStreams.newReaderSupplier to me. Correct me if I am wrong, but in order to easily convert InputStream to BufferedReader using Guava, I have to do something like this:
final InputStream inputStream = new FileInputStream("/etc/fstab"); Reader bufferedReader = new BufferedReader(CharStreams.newReaderSupplier(new InputSupplier<InputStream>(){ public InputStream getInput() throws IOException { return inputStream; } }, Charset.defaultCharset()).getInput());
Of course, I can create a helper like:
return new BufferedReader(new InputStreamReader(inputStream));
However, I believe that such an assistant should offer Guava IO. I can do such a trick for a File instance. Why can't I use InputStream?
// Guava can do this Reader r = Files.newReader(new File("foo"), charset); // but cannot do this Reader r = SomeGuavaUtil.newReader(inputStream, charset);
Correct me if I am wrong, but it seems to me that this is a flaw in the API.
source share