question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add conversion from Reader to InputStream

See original GitHub issue

It 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 uses CodingErrorAction.REPLACE for them

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
eamonnmcmanuscommented, Feb 28, 2022

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 com.google.common.io API does put the emphasis on reusable sources and sinks rather than one-shot streams like Reader and InputStream.

1reaction
eamonnmcmanuscommented, Mar 1, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found