PipedOutputStream producer = new PipedOutputStream();
PipedInputStream consumer = new PipedInputStream(producer);
// This thread reads the input from one process, and publishes it to another thread
byte[] buffer = new byte[8192];
int amountRead;
while ((amountRead = input.read(buffer)) != -1) {
producer.write(buffer, 0, amountRead);
}
// This thread consumes what has been published, and writes it to another process
byte[] buffer = new byte[8192];
int amountRead;
while ((amountRead = consumer.read(buffer)) != -1) {
output.write(buffer, 0, amountRead);
}
1条答案
按热度按时间gj3fmq9x1#
从单个线程可以使用:
如果要使用两个线程,可以使用
PipedInputStream
以及PipedOutputStream
: