1 /* 2 * File: ReadStream.java 3 * 4 * Read a file on the server 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.applet.Applet; 11 import java.awt.*; 12 import java.net.URL; 13 import java.net.MalformedURLException; 14 import java.io.InputStreamReader; 15 import java.io.BufferedReader; 16 import java.io.IOException; 17 18 public class ReadStream extends Applet implements Runnable { 19 20 URL url; 21 Thread thread; 22 TextArea textarea = new TextArea( "Read Stream..." ); 23 24 public void init() { 25 try { 26 url = new URL( getCodeBase(), "ReadStream.java" ); 27 } catch ( MalformedURLException e ) { 28 System.err.println( "Bad URL: " + url ); 29 } 30 setLayout( new BorderLayout() ); 31 textarea.setFont( new Font( "Monospaced", Font.BOLD, 14 ) ); 32 add( textarea, BorderLayout.CENTER ); 33 } 34 35 public void start() { 36 if ( thread == null ) { 37 thread = new Thread( this ); 38 thread.start(); 39 } 40 } 41 42 public void stop() { 43 if ( thread != null ) { 44 thread.stop(); 45 thread = null; 46 } 47 } 48 49 public void run() { 50 String line; 51 StringBuffer buf = new StringBuffer(); 52 try { 53 textarea.setText( "Opening stream..." ); 54 55 BufferedReader data = 56 new BufferedReader( 57 new InputStreamReader( url.openStream() ) ); 58 59 textarea.setText( "Reading data..." ); 60 while ( ( line = data.readLine() ) != null ) { 61 buf.append( line + "\n" ); 62 } 63 data.close(); 64 65 textarea.setText( buf.toString() ); 66 } catch ( IOException e ) { 67 textarea.setText( "ReadStream failed!" ); 68 System.err.println( "I/O Error:" + e.getMessage() ); 69 } 70 } 71 72 } // end ReadStream class 73 74 75