import java.applet.*; // contains class Applet import java.net.*; // contains URL import java.awt.*; // contains TextArea, Button, FlowLayout import java.awt.event.*; // contains WindowListener, ActionEvent import java.io.*; // contains InputStream, OutputStream public class TA extends Applet { // The following two functions convert a TextArea into an InputStream and // an OutputStream respectively. They can be used to produce such streams // for a (suitably parameterised) command-line application that requires // an InputStream (System.in, stdin) & a PrintStream (System.out, stdout) // (and System.err, stderr) when the application is being run as an Applet. public InputStream textArea2InputStream(final TextArea ta) { return new InputStream() { String s = ta.getText(); int inPtr=0; public int read() //minimum implementation of an InputStream { if( inPtr >= s.length() ) return -1; else { inPtr++; return s.charAt(inPtr-1); } }//read };//InputStream }//textArea2InputStream public OutputStream textArea2OutputStream(final TextArea t) { return new OutputStream() { TextArea ta = t; public void write(int b) //minimum implementation of an OutputStream { byte[] bs = new byte[1]; bs[0] = (byte) b; ta.append(new String(bs)); }//write };//OutputStream }//textArea2OutputStream // // NB. There is a constructor PrintStream(OutputStream). // // OutputStream // |-FilterOutputStream // |-PrintStream public URL relativeURL(URL base, String relPath) throws MalformedURLException // Basically base/filename, but may chop a last filename off base first, // e.g., see class Applet, getDocumentBase() etc. { String db = base.toString(); int pathEnd = db.lastIndexOf(File.separatorChar); // i.e. \ or / return new URL( db.substring(0, 1+pathEnd) + relPath ); }//relativeURL(,) public String urlContents(URL url) throws IOException // Read a file at a URL and return its contents as a String { InputStream ins = url.openStream(); StringBuffer sb = new StringBuffer(); int ch; while( (ch = ins.read()) >= 0 ) sb.append( (char)ch ); return sb.toString(); }//urlContents // ---------------------------------------------------------------------------- // Following is a rudimentary test that TextArea can be made into // InputStream and OutputStream public void init() // Applet requires init { this.setLayout(new FlowLayout(FlowLayout.LEFT)); final TextArea tIp = new TextArea("initial input\n", 5, 20); final TextArea tOp = new TextArea(5, 20); final PrintStream opSt = new PrintStream(textArea2OutputStream(tOp)); // * Button b1 = new Button("ip->op"); // appends input to output b1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int ch; InputStream ipSt = textArea2InputStream( tIp ); // * try{ // in the spec., read(), write() could throw IOException while( (ch = ipSt.read()) > 0 ) // read opSt.print( (char)ch ); // print }catch(Exception _) { } } } ); final Button b2 = new Button("clear"); // clears the output area b2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { tOp.setText(""); } } ); this.add("ip", tIp); this.add("b1", b1); this.add("op", tOp); this.add("b2", b2); final String inFileName = getParameter("INPUT"); //maybe a file to load if( inFileName != null ) //into the input TextArea try{ tIp.setText(urlContents(relativeURL(getDocumentBase(), inFileName))); } catch(Exception e) { opSt.print( "Error in loading [" + inFileName + "]\n" + e.toString() ); } // ---------------------------------------------------------------------------- try{ final Button home = new Button("home"); final URL homeURL = new URL("http://www.allisons.org/ll/"); home.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent _) { getAppletContext().showDocument( homeURL, "_top" ); } } ); this.add("home", home); } catch(MalformedURLException _) { } }//init of the Applet }//TA class // Lloyd Allison, 19/9/2007, licence of this applet is GNU GPL "copyleft".