是否可以在 java.nio(或常规 java io)中分叉(克隆)流?
是否可以(使用标准 java.nio api,无需重大黑客攻击或代理或外观)获取输出流并克隆它,以便对流的每次写入都从一个复制到两个独立的输出流?
Is it possible (using the standard java.nio api, without major hacking or proxying or facading) to take an output stream, and clone it, so that every write to the stream gets copied to two, independent output streams from one ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。您必须编写委托给两个(所有)目标通道的
WritableByteChannel
实现。不太难,只需要写一个构造函数和三个方法。编辑 您可以通过编写 FilterOutputStream 派生类对流执行相同的操作。又非常容易。
No. You would have to write an implementation of
WritableByteChannel
that delegated to both (all) target channels. Not too difficult, only one constructor and three methods to write.EDIT You can do the same thing for streams by writing a FilterOutputStream derived class. Again very easy.
正如 EJP 所说,这非常简单,但您也可以使用 Apache 的 commons-io 库来实现这一点。
TeeOutputStream< /a> - 来自文档:“OutputStream 的经典拆分器。以 unix 'tee' 命令命名。它允许对流进行分支,因此现在有两个流。”
Like EJP said it is very easy, but you could also use the commons-io library from Apache for that.
TeeOutputStream - From the Docs: "Classic splitter of OutputStream. Named after the unix 'tee' command. It allows a stream to be branched off so there are now two streams."