我在“克隆”一个 InputStream 时遇到了问题。
这不起作用:
InputStream is = ClassLoader.getSystemResourceAsStream("myResource");
但这有效:
InputStream is = new BufferedInputStream(new FileInputStream("/afas.cfg"));
我的代码是:
// Create a piped input stream for one of the readers.
PipedInputStream in = new PipedInputStream();
// Create a tee-splitter for the other reader.(from apache commons io)
TeeInputStream tee = new TeeInputStream(is, new PipedOutputStream(in));
// Create the two buffered readers.
BufferedReader br1 = new BufferedReader(new InputStreamReader(tee));
BufferedReader br2 = new BufferedReader(new InputStreamReader(in));
// Do some interleaved reads from them.
System.out.println("One line from br1:");
System.out.println(br1.readLine());
System.out.println();
System.out.println("Two lines from br2:");
System.out.println(br2.readLine());
System.out.println(br2.readLine());
System.out.println();
System.out.println("One line from br1:");
System.out.println(br1.readLine());
System.out.println();
问题出现在第一次br1.readLine()
通话中。它只是卡住PipedInputStream.awaitSpace()
并处于无限循环中。
PipedStreams 是否仅适用于线程?这意味着当写入 PipedOutputStreams 时,PipedInputStream 将“唤醒”
无论如何,我该怎么做才能完成这项工作?