/* * File: ReadConnection.java * * Read a file on the server using the URLConnection class * * Copyright: Northeast Parallel Architectures Center * */ import java.applet.Applet; import java.awt.*; import java.net.URL; import java.net.URLConnection; import java.net.MalformedURLException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class ReadConnection extends Applet implements Runnable { URL url; Thread thread; TextArea textarea = new TextArea( "Read Text..." ); public void init() { try { url = new URL( getCodeBase(), "ReadConnection.java" ); } catch ( MalformedURLException e ) { System.err.println( "Bad URL: " + url.toString() ); } 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 { URLConnection conn = url.openConnection(); textarea.setText( "Opening connection..." ); conn.connect(); BufferedReader data = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); textarea.setText( "Reading data..." ); while ( ( line = data.readLine() ) != null ) { buf.append( line + "\n" ); } data.close(); textarea.setText( buf.toString() ); } catch ( IOException e ) { textarea.setText( "ReadConnection failed!" ); System.err.println( "I/O Error:" + e.getMessage() ); } } } // end ReadConnection class