/* PipeTester.java - shows the use of pipes and chaining of IO classes written by Robert Sheehan - 30/09/97 */ import java.io.*; public class PipeTester { public static void main(String[] args) { PipedOutputStream outPipe = new PipedOutputStream(); PipeProducer producer = new PipeProducer(outPipe); PipeConsumer consumer = new PipeConsumer(outPipe); producer.start(); consumer.start(); } } class PipeProducer extends Thread { PipedOutputStream outPipe; PipeProducer(PipedOutputStream outPipe) { this.outPipe = outPipe; } public void run() { try { DataOutputStream out = new DataOutputStream(outPipe); for (int i = 1; i <= 100; i++) out.writeInt(i); } catch (IOException e) { System.err.println("Producer: " + e); System.exit(1); } } } class PipeConsumer extends Thread { PipedInputStream inPipe; PipeConsumer(PipedOutputStream outPipe) { try { inPipe = new PipedInputStream(outPipe); } catch (IOException e) { System.err.println("Connecting the pipe: " + e); System.exit(1); } } public void run() { try { DataInputStream in = new DataInputStream(inPipe); for (;;) { int data = in.readInt(); System.out.println(data); } } catch (IOException e) { System.err.println("Consumer: " + e); System.exit(1); } } }