Will Beason Will Beason - 1 year ago 100
Java Question

Why does StringBufferInputStream doc recommend StringReader for String to Stream conversion?

As has been answered repeatedly, you can easily convert a

to an

When browsing the Java 8 documentation I came across the long-deprecated
class, which states that

As of JDK 1.1, the preferred way to create a stream from a string is via the

What is way is this referring to? There are several methods requiring classes in non-default libraries such as the error-prone
from Apache Commons IO, but I'm looking for the preferred way mentioned in the documentation. The solutions referenced in other questions are sufficient for my use cases, but I'd still like to know what the documentation is referencing.


Apparently this is a 16 year old bug that hasn't been fixed. The proposed solution in the link is to use the deprecated class. I can't imagine that is what is intended from what the documentation says.

Answer Source

I don't know the answer to the javadoc part, but the first answer you pointed to is a reasonable one: just use String.getBytes(encoding) to get byte array, then use ByteArrayInputStream.

But usually the more important question is this: why on earth do you NEED such conversion? In a well designed system, you should never need to go in this direction: it is against the normal flow of things where within JDK you deal with chars and Strings, and outside with bytes and Streams. So conversions in this direction are quite rare and it does not seem necessary for JDK to have explicit support.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download