I'm not used to working with streams in Java - how do I create an InputStream
from a String
?

- 76,817
- 74
- 166
- 248
-
4how are you going to use the InputStream created from String? – MohamedSanaulla Apr 19 '11 at 18:02
6 Answers
Here you go:
InputStream is = new ByteArrayInputStream( myString.getBytes() );
Update For multi-byte support use (thanks to Aaron Waibel's comment):
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString).array());
Please see ByteArrayInputStream manual.
It is safe to use a charset argument in String#getBytes(charset) method above.
After JDK 7+ you can use
java.nio.charset.StandardCharsets.UTF_16
instead of hardcoded encoding string:
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(myString).array());

- 761,203
- 64
- 569
- 643
-
1this will cause chars with their first byte as something besides 0x00 to be trimmed, to be more accurate, you should do `InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString()).array())` – Apr 19 '11 at 17:58
-
9@Aaron: No, `String.getBytes()` will use the platform default encoding (which may or may not be the one you want here). For me, this is UTF-8 (which can encode all Java Strings). The right answer would be "indicate the encoding you really want", not "use UTF-16". And you can use it with `.getBytes(charsetName)`, don't have to explicitly create a Charset object and encode the String to a ByteBuffer. – Paŭlo Ebermann Apr 19 '11 at 20:35
-
1How about not using String.getBytes? It allocates a copy of string, so it is not memory effective solution. The reason to use inputstream is usually to avoid keeping a copy of large object in memory – Vitamon May 19 '16 at 11:29
-
1This answer could be updated to mention `java.nio.charset.StandardCharsets.UTF_16`. – kevinarpe Dec 22 '17 at 12:39
-
-
use apache.commons.io `InputStream is=IOUtils.toInputStream(yourString,StandardCharsets.UTF_8);` – Khalid Shah Feb 22 '18 at 06:09
You could do this:
InputStream in = new ByteArrayInputStream(string.getBytes("UTF-8"));
Note the UTF-8
encoding. You should specify the character set that you want the bytes encoded into. It's common to choose UTF-8
if you don't specifically need anything else. Otherwise if you select nothing you'll get the default encoding that can vary between systems. From the JavaDoc:
The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

- 70,765
- 18
- 106
- 111
InputStream in = new ByteArrayInputStream(yourstring.getBytes());
Java 7+
It's possible to take advantage of the StandardCharsets
JDK class:
String str=...
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(str).array());

- 41,764
- 65
- 238
- 329
Beginning with Java 7, you can use the following idiom:
String someString = "...";
InputStream is = new ByteArrayInputStream( someString.getBytes(StandardCharsets.UTF_8) );

- 26,371
- 26
- 130
- 172
Instead of CharSet.forName, using com.google.common.base.Charsets from Google's Guava (http://code.google.com/p/guava-libraries/wiki/StringsExplained#Charsets) is is slightly nicer:
InputStream is = new ByteArrayInputStream( myString.getBytes(Charsets.UTF_8) );
Which CharSet you use depends entirely on what you're going to do with the InputStream, of course.

- 3,439
- 32
- 38