Java 中的管道或交换输入/输出流
分析器类的列表,用于分析输入流的依赖关系,更改一些内容并将其写入输出流:
public Set<Dependency> analyse(InputStream i, OutputStream o);
分析器应该像这样链接:
for(DocumentAnalyser analyser : a) {
o.getDependencies().addAll(analyser.analyse(in, out));
in = new ByteArrayInputStream(out.toByteArray());
}
现在我正在一个 in 是最终的环境中工作。
- 有没有更好的方法来“链接”流?
- 使用 ByteArrayInputStream 从“out”到“in”的“交换”操作是否昂贵?
- 如何处理“in”是最终的问题?
A list of analyser classes which analyse a InputStream for dependencies, changes a few things and write it to an OutputStream:
public Set<Dependency> analyse(InputStream i, OutputStream o);
The analysers should be chained like:
for(DocumentAnalyser analyser : a) {
o.getDependencies().addAll(analyser.analyse(in, out));
in = new ByteArrayInputStream(out.toByteArray());
}
Now I'm working in a environment where in is final.
- Is there a better way to "chain" the streams?
- Is the "swap" operation from "out" to "in" with ByteArrayInputStream expensive?
- How to deal with the problem that "in" is final?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
。请注意,通过应用 1.,您不需要担心 2,因为您实际上是在管道流。
java.io.PipedInputStream
/java.io.PipedOutputStream
pairs.Note that by applying 1. you do not need to worry about 2 because you are in fact piping the streams.
对于问题 2。
我将提供我自己的子类,它可以直接访问 ByteArrayInputStream 和 ByteArrayOutputStream 的缓冲区。这样,您就不会因为在
toByteArray
中进行额外的复制而浪费内存和时间。对于问题 3。
将其分配给本地非最终变量,
但请注意,原始
in
将不再有效(它将位于流末尾)For question 2.
I would provide my own subclass that has a direct access to ByteArrayInputStream's and ByteArrayOutputStream's buffer. That way you don't waist memory and time by making extra copy in
toByteArray
.For question 3.
Assign it to a local non-final variable,
Beware, though, that the original
in
will no longer be valid ( it will be at the end-of-stream )