Add conversion from Reader to InputStream
See original GitHub issueIt appears there are use cases where one wants to convert a java.io.Reader
to a java.io.InputStream
using a given charset, see this Stack Overflow question or this Baeldung article.
Guava already has this functionality implemented as ReaderInputStream
, but only exposes it for CharSource
(therefore not for Reader
), see also https://github.com/google/guava/issues/642#issuecomment-380502353.
Would it make sense to expose it also for Reader
, e.g. as com.google.common.io.CharStreams.asInputStream(Reader, Charset)
?
Considerations:
- Users might want to specify buffer size?
- Users very likely want to specify behavior on unencodable chars, currently
ReaderInputStream
usesCodingErrorAction.REPLACE
for them
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Java - Reader to InputStream - Baeldung
In this quick tutorial we're going to look at the conversion from a Reader to an InputStream – first with plain Java, then...
Read more >Convert Reader to InputStream and Vice versa
We first get the content from Reader to byte[]. Use BufferedReader for better performance. Then we use the byte[] to create an InputStream....
Read more >java - How to convert a Reader to InputStream and a Writer to ...
I like this solution for it works when you need to unit test code that accepts input on (e.g.) standard input. – Kedar...
Read more >Convert Reader to InputStream - Java2s.com
This class convert Reader to InputStream. It works by converting * the characters to the encoding specified in constructor parameter.
Read more >Convert Reader to InputStream in Java | FrontBackend
In this article we are going to learn how to convert Reader to InputStream using plain Java solution and external libraries like Guava...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It is possible for anyone to do this with the existing API, like this:
Not very elegant, but the
com.google.common.io
API does put the emphasis on reusable sources and sinks rather than one-shot streams likeReader
andInputStream
.The Guava team has talked this over and we don’t think there’s enough evidence of demand for such a method. The
com.google.common.io
package has existed at Google for 15 years and we seem to have got by just fine all that time without the method. People who do need it for some reason can do what I suggested above.