1  import java.awt.*;
  2  import java.io.*;
  3  import java.net.*;
  4  
  5  public class ReadStream extends java.applet.Applet implements Runnable {
  6  
  7    URL url;
  8    Thread runner;
  9    TextArea ta = new TextArea("Read Stream...");
 10    
 11    public void init() {
 12      try { 
 13        url = new URL(getCodeBase(), "ReadStream.java"); 
 14      } catch ( MalformedURLException e) {
 15        System.out.println("Bad URL: " + url);
 16      }
 17      setLayout(new BorderLayout());
 18      ta.setFont(new Font("TimesRoman", Font.BOLD, 18));
 19      add("Center", ta);
 20    }
 21    
 22    public void start() {
 23      if (runner == null) {
 24        runner = new Thread(this);
 25        runner.start();
 26      }
 27    }
 28    
 29    public void stop() {
 30      if (runner != null) {
 31        runner.stop();
 32        runner = null;
 33      }
 34    }
 35    
 36    public void run() {
 37      DataInputStream data = null;
 38      String line;
 39      StringBuffer buf = new StringBuffer();
 40      try { 
 41        ta.setText("Open Stream...");
 42        data = new DataInputStream(new BufferedInputStream(
 43          url.openStream()));
 44        ta.setText("Reading data...");
 45        while ((line = data.readLine()) != null)
 46          buf.append(line + "\n");
 47        ta.setText(buf.toString());
 48      } catch (IOException e) {
 49        System.out.println("IO Error:" + e.getMessage());
 50      }
 51    }
 52    
 53  } // end ReadStream class
 54  
 55        
 56