/* Simple file reading applet demonstrating access from JavaScript
   by Danny Goodman  (http://www.dannyg.com)
   
   (To modify and recompile, rename file to 'FileReader.java')
*/
import java.net.*;
import java.io.*;

public class FileReader extends java.applet.Applet implements Runnable {

	Thread thread;
	URL url;
	String output;
	String fileName = "Bill of rights.txt";

	public void getFile(String fileName) throws IOException {
		String result, line;
		InputStream connection;
		DataInputStream dataStream;
		StringBuffer buffer = new StringBuffer();

		try {
			url = new URL(getDocumentBase(),fileName);
		}
		catch (MalformedURLException e) {
			output = "AppletError " + e;
		}

		try {
			connection = url.openStream();
			dataStream = new DataInputStream(new BufferedInputStream(connection));

			while ((line = dataStream.readLine()) != null) {
				buffer.append(line + "\n");
			}
			result = buffer.toString();
		}
		catch (IOException e) {
			result = "AppletError: " + e;
		}
		output = result;
	}

	public String fetchText() {
		return output;
	}

	public void init() {
	}

	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(){
		try {
			getFile(fileName);
		}
		catch (IOException e) {
			output = "AppletError: " + e;
		}
	}
}
