Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 312
Kaleb Pederson Avatar asked Jan 28 '10 22:01

Kaleb Pederson


2 Answers

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

ByteArrayInputStream(str.getBytes("UTF8"));

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

like image 156
Brian Agnew Avatar answered Oct 13 '22 22:10

Brian Agnew


See java.io.ByteArrayInputStream

String s = "test";
InputStream input = new ByteArrayInputStream(s.getBytes("UTF8"));
like image 41
Kevin Avatar answered Oct 13 '22 23:10

Kevin