在 Java 1.4 InputStream 上替换字符

发布于 2024-10-20 03:12:56 字数 308 浏览 3 评论 0原文

例如,我有一个正在返回的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妖妓 2024-10-27 03:12:57

一种方法是将您的 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.

只是在用心讲痛 2024-10-27 03:12:57

流读取/写入字节。尝试用二进制表示替换文本是自找麻烦。因此,要做的第一件事就是将此流包装到 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?

不一样的天空 2024-10-27 03:12:57

这可能不是最有效的方法,但它确实有效。

    InputStream is = // input;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos));

    String line = null;

    while((line = reader.readLine()) != null)
    {
        if(line.contains("com.foo"))
        {
            line = line.replace("com.foo", "org.bar");
        }

        writer.write(line);
    }

    return baos.toByteArray();

This may not be the most efficient way to do it, but it certainly works.

    InputStream is = // input;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos));

    String line = null;

    while((line = reader.readLine()) != null)
    {
        if(line.contains("com.foo"))
        {
            line = line.replace("com.foo", "org.bar");
        }

        writer.write(line);
    }

    return baos.toByteArray();
回忆躺在深渊里 2024-10-27 03:12:56

如果你有一个字节数组,你可以将其转换为字符串。注意编码,在示例中我使用utf-8。我认为这是一个简单的方法:

String newString = new String(byteArray, "utf-8");
newString = newString.replace("com.foo", "org.bar");
return newString.getBytes("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:

String newString = new String(byteArray, "utf-8");
newString = newString.replace("com.foo", "org.bar");
return newString.getBytes("utf-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文