293

How can I transform a String value into an InputStreamReader?

dantiston
  • 5,161
  • 2
  • 26
  • 30
Yossale
  • 14,165
  • 22
  • 82
  • 109

6 Answers6

338

ByteArrayInputStream also does the trick:

InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) );

Then convert to reader:

InputStreamReader reader = new InputStreamReader(is);
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Guido
  • 46,642
  • 28
  • 120
  • 174
  • 3
    You might *want* to inherit the platform's default charset. – slim Oct 29 '08 at 15:21
  • 2
    Thanks. Done. What's the best way to detect the plataform's default charset ? – Guido Oct 29 '08 at 16:14
  • 5
    Or just use getBytes() without a parameter... Arguably it should not exist, but it will use the default encoding. – Michael Borgwardt Aug 11 '09 at 15:34
  • 1
    [`ByteArrayInputStream `](http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html): **Since:**  JDK1.0   There’s not the slightest reason to assume that this class is “since Java 1.4”. That wrong version number is especially weird as Java 1.4 introduced `NIO` and it makes little sense to introduce an API and its conceptional successor within the same version. – Holger Mar 31 '15 at 11:00
64

I also found the apache commons IOUtils class , so :

InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(myString));
Termininja
  • 6,620
  • 12
  • 48
  • 49
Yossale
  • 14,165
  • 22
  • 82
  • 109
  • 36
    Converting String->byte[] or vice versa without mentioning a character encoding is almost always a bug. – Joachim Sauer Aug 11 '09 at 15:31
  • 1
    This may cause data loss, depending on the default platform encoding and characters in the string. Specifying a Unicode encoding for both encoding and decoding operations would be better. Read this for more details: http://illegalargumentexception.blogspot.com/2009/05/java-rough-guide-to-character-encoding.html#javaencoding_lossyconversions – McDowell Aug 11 '09 at 15:44
  • Isn't the encoding param just needed to get bytes in a certain encoding? I think as long as you pick the same encoding for the String#getBytes call as you pick for the InputSTreamReader constructor, it doesn't really matter which one you pick. I am pretty sure that Joachim and McDowell are wrong for this specific case. No encoding knowledge needed for wrapping an InputStreamReader around a String. – Stijn de Witt Dec 03 '10 at 15:10
  • 4
    @Stijn de Witt - belated reply I know, but I just saw this. If the default platform encoding does not support the code points in the string, data loss will result. If your default encoding is UTF-8, no problem. If you're running on Windows, you'd lose most of the Unicode set because those JREs default to legacy "ANSI" encodings. The proof is in the link I posted. `new InputStreamReader(IOUtils.toInputStream(myString, "UTF-16"), "UTF-16")` would be lossless. – McDowell Feb 22 '11 at 20:46
  • hooray, using a 3rd party library to turn a one-liner into…a larger one-liner. Not to speak about useless conversion of a `String` into a `byte[]` array to convert the bytes back to `char`s then… – Holger Mar 31 '15 at 11:04
  • Actually taking a look at the implementation: `IOUtils.toInputStream(myString, StandardCharsets.UTF_8)` also just creates a ByteArrayInputStream: `new ByteArrayInputStream(input.getBytes(Charsets.toCharset(encoding)))`. – Matthias B Nov 22 '18 at 10:20
34

Does it have to be specifically an InputStreamReader? How about using StringReader?

Otherwise, you could use StringBufferInputStream, but it's deprecated because of character conversion issues (which is why you should prefer StringReader).

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
17

Same question as @Dan - why not StringReader ?

If it has to be InputStreamReader, then:

String charset = ...; // your charset
byte[] bytes = string.getBytes(charset);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais);
Community
  • 1
  • 1
toolkit
  • 49,809
  • 17
  • 109
  • 135
4

Are you trying to get a) Reader functionality out of InputStreamReader, or b) InputStream functionality out of InputStreamReader? You won't get b). InputStreamReader is not an InputStream.

The purpose of InputStreamReader is to take an InputStream - a source of bytes - and decode the bytes to chars in the form of a Reader. You already have your data as chars (your original String). Encoding your String into bytes and decoding the bytes back to chars would be a redundant operation.

If you are trying to get a Reader out of your source, use StringReader.

If you are trying to get an InputStream (which only gives you bytes), use apache commons IOUtils.toInputStream(..) as suggested by other answers here.

Fai Ng
  • 760
  • 1
  • 6
  • 14
2

You can try Cactoos:

InputStream stream = new InputStreamOf(str);

Then, if you need a Reader:

Reader reader = new ReaderOf(stream);
yegor256
  • 102,010
  • 123
  • 446
  • 597