-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add conversion from Reader to InputStream #5376
Comments
Something looks like this maybe? @Marcono1234 |
Yes that looks reasonable, but it would probably be good to have an |
@ManishOffi, I am not a member of this project, so I cannot assign you, sorry. |
Hi @Marcono1234 & @pt657407064 is this issue still relevant? I'd like to contribute and try to fix it with a PR. |
It is possible for anyone to do this with the existing API, like this: public static InputStream asInputStream(Reader reader, Charset charset) throws IOException {
return new CharSource() {
@Override public Reader openStream() {
return reader;
}
}.asByteSource(charset).openStream();
} Not very elegant, but the |
Great thanks @eamonnmcmanus. Can I be assigned to the issue? |
The Guava team has talked this over and we don't think there's enough evidence of demand for such a method. The |
I think also that the functionality is slightly unusual. Normally encoding is part of writing, and decoding associated with reading. (Where "encode/decode" really mean "transcode from/to utf-16".) It's enough that I would at least inquire into how the user got into that situation. |
@eamonnmcmanus Unfortunately guava/guava/src/com/google/common/io/ReaderInputStream.java Lines 87 to 88 in 0a17f4a
CharSource.asByteSource(...) with CharsetEncoder . Alternatively ReaderInputStream could be made public then a CharsetEncoder can be passed.
ReaderInputStream from Apache Commons explains it quite good. One uses a third-party API that requires an
Guava should provide such an adapter too. |
It appears there are use cases where one wants to convert a
java.io.Reader
to ajava.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 forCharSource
(therefore not forReader
), see also #642 (comment).Would it make sense to expose it also for
Reader
, e.g. ascom.google.common.io.CharStreams.asInputStream(Reader, Charset)
?Considerations:
ReaderInputStream
usesCodingErrorAction.REPLACE
for themThe text was updated successfully, but these errors were encountered: