Fai Ng Fai Ng - 3 months ago 12
Java Question

Any util class/method to take a large String and return an InputStream?

I am looking for some util class/method to take a large

String
and return an
InputStream
.

If the
String
is small, I can just do:

InputStream is = new ByteArrayInputStream(str.getBytes(<charset>));


But when the
String
is large(1MB, 10MB or more), a byte array 1 to 2 times(or more?) as large as my String is allocated on the spot. (And since you won't know how many bytes to allocate exactly before all the encoding is done, I think there must be other arrays/buffers allocated before the final byte array is allocated).

I have performance requirements, and want to optimize this operation.

Ideally I think, the class/method I am looking for would encode the characters on the fly one small block at a time as the InputStream is being consumed, thus no big surge of mem allocation.

Looking at the source code of apache commons
IOUtils.toInputStream(..)
, I see that it also converts the String to a big byte array in one go.

And
StringBufferInputStream
is Deprecated, and does not do the job properly.

Is there such util class/method from anywhere? Or I can just write a couple of lines of code to do this?

The functional need for this is that, elsewhere, I am using a util method that takes an
InputStream
and stream out the bytes from this
InputStream
.

I haven't seem other people looking for something like this. Am I mistaking something somewhere?

I have started writing a custom class for this, but would stop if there is a better/proper/right solution/correction to my need.

Answer

The Java built-in libraries assume you'd only need to go from chars to bytes in output, not input. The Apache Commons IO libraries have ReaderInputStream, however, which can wrap a StringReader to get what you want.