1011

Given a string:

String exampleString = "example";

How do I convert it to an InputStream?

Iain
  • 10,433
  • 16
  • 56
  • 62

5 Answers5

1596

Like this:

InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));

Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.

For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".

anishpatel
  • 1,472
  • 1
  • 16
  • 23
Iain
  • 10,433
  • 16
  • 56
  • 62
  • 1
    Doesn't this charset get lost when doing: String --> bytes --> ByteArrayInputStream ? When trying this on a for example "ü", the bytes will have the right letter, but the ByteArrayInputStream will have lost the right conversion. Or am I wrong? – Jonathan Ramos Sep 18 '13 at 11:28
  • 21
    StandardCharsets requires minimum API level 19. – Nantoka Jan 30 '14 at 15:05
  • 3
    @JonathanRamos it's not up to the stream to hold the conversion. It's up to whatever is decoding the bytes back into strings. – Cruncher Mar 12 '14 at 18:28
  • 27
    @Nantoka Instead of StandardCharsets.UTF_8 you can use Charset.forName("UTF-8") for any API Level. – PJ_Finnegan Dec 24 '14 at 09:19
  • its work just i add String newStr = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"),"UTF-8"); for (utf8) – sirmagid Feb 22 '17 at 21:20
  • for okhttp3 users: don't use this approach, it will broke your images that should be displayed on webview. just use : response.body().byteStream() `byteStream()` method. – Siwei Aug 12 '17 at 13:35
  • 1
    Not pretty if you have large string objects and want to save on memory/allocations. A proper streaming solution would be nice. – Dave Moten Jan 31 '23 at 23:25
290

I find that using Apache Commons IO makes my life much easier.

String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");

You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.

Elijah
  • 13,368
  • 10
  • 57
  • 89
  • 6
    They used new ByteArrayInputStream(exampleString.getBytes("UTF-8")). So it will be optimized way to use InputStream stream = new ByteArrayInputStream(exampleString.getBytes("UTF-8")); – Pankaj Kumar Aug 24 '11 at 12:34
  • 10
    @PankajKumar: Java's JIT compiler is more than able to inline this. – Andrew White Jul 20 '12 at 12:14
  • 9
    Using a method which doesn't specify encoding is a terrible idea... – arkon Jun 05 '13 at 02:01
  • 2
    @b1naryatr0phy: Apache commons includes another form of this method which takes the encoding as a second parameter (which, you're right, is preferable): InputStream in = IOUtils.toInputStream(source, "UTF-8"); – Cuga Jan 11 '14 at 05:48
  • 8
    You can use `StandardCharsets.UTF_8` definition instead of plain text. – douglaslps Jul 07 '15 at 14:34
  • Why is "Using a method which doesn't specify encoding is a terrible idea..."? – alex Jun 18 '19 at 14:08
  • Careful, that one causes a crash on Android 7 as `ThreadLocal.withInitial` is not available before API26. – Paul W Oct 25 '21 at 19:45
42

You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.

Community
  • 1
  • 1
A_M
  • 7,693
  • 6
  • 33
  • 37
15

There are two ways we can convert String to InputStream in Java,

  1. Using ByteArrayInputStream

Example :-

String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
  1. Using Apache Commons IO

Example:-

String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
Anil Nivargi
  • 1,473
  • 3
  • 27
  • 34
0

You can try cactoos for that.

final InputStream input = new InputStreamOf("example");

The object is created with new and not a static method for a reason.

andreoss
  • 1,570
  • 1
  • 10
  • 25