未弃用的 StringBufferInputStream 等效项
我正在使用 LogManager.readConfiguration()
需要一个输入流,我希望其内容来自字符串。是否有相当于 StringBufferInputStream
尚未弃用,例如 ReaderToInputStreamAdaptor
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用ByteArrayInputStream,并且要小心指定适当的字符编码。例如,
您需要担心字符编码以确定每个字符如何转换为一组字节。请注意,您可以使用默认的 getBytes() 方法并通过 -Dfile.encoding=... 指定 JVM 运行的编码
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=...
请参阅 java.io.ByteArrayInputStream
See java.io.ByteArrayInputStream
LogManager.readConfiguration()
表示它接受java.util.Properties
格式。所以,真正正确的编码安全实现是这样的:编辑:编码算法更正
编辑2:实际上,
java.util.Properties
格式有一些其他限制(例如\
和其他特殊字符的转义),请参阅文档EDIT3: 0x00-0x1f 转义已删除,正如 Alan Moore 建议的那样
Documentation of
LogManager.readConfiguration()
says that it accepts data injava.util.Properties
format. So, the really correct encoding-safe implementation is this:EDIT: Encoding algorithm corrected
EDIT2: Actually,
java.util.Properties
format have some other restrictions (such as escaping of\
and other special characters), see docsEDIT3: 0x00-0x1f escaping removed, as Alan Moore suggests