Non-Deprecated StringBufferInputStream equivalent

deprecated, inputstream, java, string

Solution

Documentation of `LogManager.readConfiguration()` says that it accepts data in `java.util.Properties` format. So, the really correct encoding-safe implementation is this:

String s = ...;

StringBuilder propertiesEncoded = new StringBuilder();
for (int i = 0; i < s.length(); i++)
{
    char c = s.charAt(i);
    if (c <= 0x7e) propertiesEncoded.append((char) c);
    else propertiesEncoded.append(String.format("\\u%04x", (int) c)); 
}
ByteArrayInputStream in = new ByteArrayInputStream(propertiesEncoded.toString().getBytes("ISO-8859-1"));

EDIT: Encoding algorithm corrected

EDIT2: Actually, `java.util.Properties` format have some other restrictions (such as escaping of `\` and other special characters), see docs

EDIT3: 0x00-0x1f escaping removed, as Alan Moore suggests

Problem

I'm working with `LogManager.readConfiguration()` which requires an InputStream whose contents I'd like to come from a string. Is there an equivalent of `StringBufferInputStream` that's not deprecated, such as a `ReaderToInputStreamAdaptor`?

Original source