我正在使用PipedInputStream& PipedOuputStream将大型XML文档写入OutputStream,然后需要从InputStream中使用它。
我有这个工作用于简单的一个流出/一个流的情况,但我需要循环一个非常大的产品集,并为每个产品写两个略有不同的项目,流式传输到两个不同的文件。< / p>
以下的伪代码显示了我为实现此要求而尝试采用的方法:
PipedInputStream is1 = new PipedInputStream();
PipedInputStream is2 = new PipedInputStream();
PipedOutputStream os1 = new PipedOutputStream(is1);
PipedOutputStream os2 = new PipedOutputStream(is2);
new Thread(new Runnable(){
public void run(){
try {
XMLStreamWriter xmlWriter1 = createXMLStreamWriter(os1);
// Write header content with xmlWriter1
XMLStreamWriter xmlWriter2 = createXMLStreamWriter(os2);
// Write header content with xmlWriter2
for (Product product : someProducts) {
// marshal item for product to xmlWriter1
// marshal item for product to xmlWriter2
}
// Writer footer, close & flush with xmlWriter1
// Writer footer, close & flush with xmlWriter2
} finally {
// Additional try/catch excluded for simplicity
os1.close();
os2.close();
}
}
}).start();
// Consume is1
// Consume is2
如前所述,这适用于一个PipedInputStream / PipedOutputStream,但如上所示有两个,它只在编写低于特定大小的内容时才起作用。当内容超过特定大小时,它会在写入第二个XML文档的结束元素时挂起。
任何想法为什么会发生这种情况,以及解决方案可能是什么?上述方法是否可行?