在 Java 1.4 InputStream 上替换字符
例如,我有一个正在返回的 InputStream:
<?xml version='1.0' ?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><bbs:rule xmlns:bbs="http://com.foo/bbs">
然后我将流传递给返回字节数组的方法。 在传递给 byte[] 方法之前,我想用其他东西替换“com.foo”,例如“org.bar”。
有什么好的方法可以做到这一点?
I have an InputStream that is returning, for example:
<?xml version='1.0' ?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><bbs:rule xmlns:bbs="http://com.foo/bbs">
I then pass the stream to a method that return a byte array.
I'd like to substitute "com.foo" with something else, like "org.bar" before I pass to the byte[] method.
What is a good way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种方法是将您的 InputStream 包装在您自己的 FilterInputStream 子类中,该子类可以动态执行转换。它必须是一个前瞻流,检查每个“c”字符以查看它后面是否跟着“om.foo”,如果是则进行替换。您可能只需重写
read()
方法。One way is to wrap your InputStream in your own FilterInputStream subclass that does the transformation on the fly. It will have to be a look-ahead stream that checks every "c" character to see if it is followed by "om.foo" and if so make the substitution. You'll probably have to override just the
read()
method.流读取/写入字节。尝试用二进制表示替换文本是自找麻烦。因此,要做的第一件事就是将此流包装到 Reader(如 InputStreamReader)中,它将负责将二进制数据转换为字符信息。但是,您必须知道流数据的编码,以确保正确解释它。例如,UTF-8 或 ISO-8859-1。
一旦获得文本数据,您就可以考虑如何替换其中的部分内容。一种方法是使用正则表达式。但是,这意味着您首先必须将整个流读入字符串中,进行替换,然后返回字节数组。对于大量数据,这可能效率低下。
由于您正在处理 XML 数据,因此您可以利用更高级别的方法并以某种方式解析 XML,从而允许您处理内容,而不必将它们完全存储为中间格式。带有您自己的 ContentHandler 的 SAXParser 就可以解决问题。当事件发生时,只需将它们再次写出来,但进行适当的更改即可。另一种方法是使用一些扩展函数魔法进行 XSLT 转换。
java.nio 中不是应该支持像这样的流操作吗?或者这是为即将推出的 Java 版本计划的?
A stream reads/writes bytes. Trying to replace text in a binary representation is asking for trouble. So the first thing to do would be wrapping this stream into a Reader (like InputStreamReader) which will take care of translating the binary data into character information for you. You'll have to know the encoding of your streamed data, however, to make sure it is interpreted correctly. For example, UTF-8 or ISO-8859-1.
Once you have your textual data, you can think of how to replace parts of it. One way to do this is using regular expressions. However, this means you'll first have to read the entire stream into a string, do the substitution and then return the byte array. For large amounts of data, this might be inefficient.
Since you're dealing with XML data, you could make use of a higher-level approach and parse the XML in some way that allows you to process the contents without having to store them entirely in an intermediate format. A SAXParser with your own ContentHandler would do the trick. As events arrive, simply write them out again but with the proper alterations. Another approach would be an XSLT transformation with some extension function magic.
Wasn't there supposed to be some support for stream manipulations like this in java.nio? Or was this planned for an upcoming Java version?
这可能不是最有效的方法,但它确实有效。
This may not be the most efficient way to do it, but it certainly works.
如果你有一个字节数组,你可以将其转换为字符串。注意编码,在示例中我使用utf-8。我认为这是一个简单的方法:
If you have a bytearray you can transform it into a String. Pay attention to the encoding, in the example I use utf-8. I think this is a simple way to do that: