Non-Deprecated StringBufferInputStream equivalent

Non-Deprecated StringBufferInputStream equivalent

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?


Answer 1:

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

Answer 2:

Use the ByteArrayInputStream, and be careful to specify an appropriate character encoding. e.g.


You need to worry about the character encoding to determine how each character is converted to a set of bytes. Note you can use the default getBytes() method and specify the encoding the JVM runs with via -Dfile.encoding=...

Answer 3:


String s = "test";
InputStream input = new ByteArrayInputStream(s.getBytes("UTF8"));