/** * Applet to generate a graph of my 'as paid' and my inflation-corrected income. * @author Robert J Morton * @version 12 July 2000, 27 April 2012 * @copyright July 2000 Robert J Morton (all rights reserved) */ // DATA LOADER import java.io.*; // for stream handling for the above import java.net.*; // for downloading data from the remote server public class loader { private InputStream I; // input stream for downloading data files private byte B[]; // gigantic byte array to hold the downloaded data private int lp = 0, // indicates current download phase LP = 0, // indicates previous download phase L = 0, // length of the remote item being loaded l = 0; // number of bytes of the above successfully downloaded so far private boolean jarred = true; private String FN, // filename of current data file cb; // code base URL - where this applet's class file came from private graphs ap; // instance reference to the mail applet private trace TR; // instance reference to the trace panel loader(graphs ap, trace TR, boolean jarred) { this.ap = ap; this.TR = TR; this.jarred = jarred; // URL (less file name) from where this applet came cb = ap.getCodeBase().toString(); /* Workaround re Hotjava re Microsoft intranet machine names where getCodeBase() wrongly seems to return the document base (cira Nov 1999). */ if(!cb.endsWith("/")) { int x = cb.lastIndexOf('/'); if(x != -1) cb = cb.substring(0, x + 1); } } void runLoop() { switch(lp) { // THE 3 DOWNLOADING PHASES // load phase 1: connect to index resource on server case 1: fileConnect(); break; // load phase 2: manage the downloading of its content case 2: fileLoad(); break; /* load phase 3: if loading of data has finished, latch the current load state and paint all the widgets as well as the graph. */ case 3: if(lp != LP) { LP = lp; ap.repaint(); } } } void fileConnect() { // CONNECT TO THE FINANCIAL DATA FILE ON THE SERVER ap.showStatus("Connecting to server..."); try { /* If loading from the applet's '.jar' file, create stream through which to load the file, use a fixed known length for the file because we cannot find the current file length easily */ if(jarred) { I = getClass().getResourceAsStream(FN + ".dat"); L = 4096; } /* Else we're loading directly from the remote server, so form the URL of the account balance data file, create an input stream object to access the file and get length of the remote file. */ else { URL url = new URL(cb + FN + ".dat"); URLConnection u = url.openConnection(); I = u.getInputStream(); L = u.getContentLength(); } l = 0; // number of bytes so far successfully downloaded B = new byte[L]; // create the gigantic buffer for the data DataInputStream D = new DataInputStream(new ByteArrayInputStream(B)); TR.setD(D); // inform the trace panel lp = 2; // advance to the file loading phase } /* Set unrecoverable error status and note the type of exception and where it occurred. */ catch(Exception e) { lp = 0; ap.showStatus("fileConnect() " + e); } } void fileLoad() { // DOWNLOAD THE DATA ap.showStatus("Loading data..."); int k; // current byte being read() try { // try to load the index file if(jarred) // If loading from the '.jar' file: while( // then while we have (k = I.read()) != -1 // not yet hit current end of input stream ) B[l++] = (byte)k; // add new byte to big byte array. else // else we are loading directly from the remote server while( // So, while: l < L && // the entire file not yet downloaded, and (k = I.read()) != -1 // not yet hit current end of input stream ) B[l++] = (byte)k; // add its new byte to the big byte array. if(jarred || l >= L) { // If whole index has now been downloaded, I.close(); // close the URL Connection's input stream lp = 3; // and indicate that index file has been ap.showStatus("Graph data loaded."); // loaded successfully. } } /* If an exception occurs during download, set error condition, note the type of exception and where it occurred: it occurred during file loading, 'e' contains the kind of error (exception) that occurred, show the length of the file (in bytes) and how many bytes had been loaded when the error occurred. */ catch(Exception e) { lp = 0; ap.showStatus("fileLoad() " + e + L + " " + l); } } void getData(String FN) { this.FN = FN; // get the name of the required data file lp = 1; // initiate the loading sequence LP = 0; // prime the loading sequence latch } }