// File: IO4.java // // Author: Rahul Simha // Created: Nov 18, 1998 // // Sequential Producer-consumer using a file. import java.awt.*; import java.awt.event.*; import java.io.*; class Producer extends Frame { Label L; OutputStream out_stream; // For the output. public Producer (OutputStream out_stream) { // Store the reference to the buffer. this.out_stream = out_stream; // Create the frame. this.setSize (600,100); this.setLocation (0,100); this.setTitle ("Producer"); this.setBackground (Color.white); // this.setLayout (new BorderLayout()); // This is where we will write to. L = new Label (""); this.add (L, BorderLayout.CENTER); this.setVisible (true); } // Must implement the run() method. public void run () { // Write 25 random bytes to the buffer. for (int i=1; i<=25; i++) { // Create a random byte byte k = (byte) UniformRandom.uniform (1, 100); // Write to label first. L.setText (L.getText() + " " + k); // Write it to the screen. System.out.println ("Producer: writing " + k); // Write integer to buffer. try { out_stream.write (k); } catch (IOException e) { System.out.println (e); } // Sleep for a while. try { Thread.sleep ((int)UniformRandom.uniform(100,1000)); } catch (InterruptedException e) { System.out.println (e); } } try { out_stream.write ((byte)-1); } catch (IOException e) { System.out.println (e); } L.setText (L.getText() + " Done!"); } } class Consumer extends Frame { Label L; InputStream in_stream; // To get data from. public Consumer (InputStream in_stream) { this.in_stream = in_stream; this.setSize (600,100); this.setLocation (0, 200); this.setTitle ("Consumer"); this.setBackground (Color.white); // this.setLayout (new BorderLayout()); L = new Label (""); this.add (L, BorderLayout.CENTER); this.setVisible (true); } public void run () { // Read byte values until EOF. while (true) { // Get the next byte int i = -1; try { i = in_stream.read (); } catch (IOException e) { System.out.println (e); } byte k = (byte) i; // Check if end-of-data. if (k < 0) break; System.out.println ("Consumer: just read " + k); // Write it on the frame. L.setText (L.getText() + " " + k); // Sleep for a while. try { Thread.sleep ((int)UniformRandom.uniform(500,1000)); } catch (InterruptedException e) { System.out.println (e); } } L.setText (L.getText() + " Done!"); } } // This is an independent quit button to quit the application. class QuitButton extends Frame { public QuitButton () { this.setSize (80,50); this.setLocation (0, 0); this.setTitle ("Quit button"); Button quitb = new Button ("QUIT"); quitb.setBackground (Color.red); quitb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); this.add (quitb, BorderLayout.CENTER); this.setVisible (true); } } public class IO4 { public static void main (String[] argv) { // Create an independent quit button. QuitButton q = new QuitButton (); try { // ByteArrayOutputStream buf_out = new ByteArrayOutputStream (1000); // Set up a temporary file. File f = new File ("IO4_temp"); // Create an output stream for it. FileOutputStream file_out = new FileOutputStream (f); // Create a producer instance and run it. // Producer p = new Producer (buf_out); Producer p = new Producer (file_out); p.run(); // Close the file so it can be read. file_out.close (); // ByteArrayInputStream buf_in = new ByteArrayInputStream (buf); FileInputStream file_in = new FileInputStream (f); // Create a consumer instance and run it. // Consumer c = new Consumer (buf_in); Consumer c = new Consumer (file_in); c.run(); file_in.close (); } catch (IOException e) { System.out.println (e); } } }