/* * File: ReadStream.java * * Read a file on the server * * Copyright: Northeast Parallel Architectures Center * */ import java.applet.Applet; import java.awt.*; import java.net.URL; import java.net.MalformedURLException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class ReadStream extends Applet implements Runnable { URL url; Thread thread; TextArea textarea = new TextArea( "Read Stream..." ); public void init() { try { url = new URL( getCodeBase(), "ReadStream.java" ); } catch ( MalformedURLException e ) { System.err.println( "Bad URL: " + url ); } setLayout( new BorderLayout() ); textarea.setFont( new Font( "Monospaced", Font.BOLD, 14 ) ); add( textarea, BorderLayout.CENTER ); } public void start() { if ( thread == null ) { thread = new Thread( this ); thread.start(); } } public void stop() { if ( thread != null ) { thread.stop(); thread = null; } } public void run() { String line; StringBuffer buf = new StringBuffer(); try { textarea.setText( "Opening stream..." ); BufferedReader data = new BufferedReader( new InputStreamReader( url.openStream() ) ); textarea.setText( "Reading data..." ); while ( ( line = data.readLine() ) != null ) { buf.append( line + "\n" ); } data.close(); textarea.setText( buf.toString() ); } catch ( IOException e ) { textarea.setText( "ReadStream failed!" ); System.err.println( "I/O Error:" + e.getMessage() ); } } } // end ReadStream class