1057

Given a string:

String exampleString = "example";

How do I convert it to an InputStream?

1
  • 2
    InputStreams look at bytes, Readers look at characters. Commented Mar 28, 2021 at 18:11

5 Answers 5

1664

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".

Sign up to request clarification or add additional context in comments.

6 Comments

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?
StandardCharsets requires minimum API level 19.
@JonathanRamos it's not up to the stream to hold the conversion. It's up to whatever is decoding the bytes back into strings.
@Nantoka Instead of StandardCharsets.UTF_8 you can use Charset.forName("UTF-8") for any API Level.
Not pretty if you have large string objects and want to save on memory/allocations. A proper streaming solution would be nice.
|
297

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.

7 Comments

They used new ByteArrayInputStream(exampleString.getBytes("UTF-8")). So it will be optimized way to use InputStream stream = new ByteArrayInputStream(exampleString.getBytes("UTF-8"));
@PankajKumar: Java's JIT compiler is more than able to inline this.
Using a method which doesn't specify encoding is a terrible idea...
@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");
You can use StandardCharsets.UTF_8 definition instead of plain text.
|
46

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

Comments

22

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);

2 Comments

Hi @anil do you have opinion /experience which one is better?
my opinion, using Apache commons IO is better option
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.