/** * Semi-Generic Loader * @author Robert J Morton YE572246C * @version 04 December 2007 * @copyright Robert J Morton (all rights reserved) */ import java.io.*; // file input/output handling import java.net.*; // for downloading data from the remote server class loader { // TO LOAD AND DISPLAY ROUTE NAMES AND GEOGRAPHIC FEATURES private boolean connectFTT = true, // first-time-through flag loadingFTT = true, // first-time-through flag dataLoaded = false; // hits data not yet loaded private int ls = 0, // load switch [see navstart.java] lp = 0, // 0=idle 1=connecting 2=loading 3=connect error 4=load error l = 0; // number of bytes of the above successfully downloaded int L = 0; // length of the remote item being loaded byte B[]; // gigantic byte array to hold the downloaded index data private String loadFile = "hc.dat", // name of the route names file cb; // URL path to data files private BufferedReader R; // for reading data from files private InputStream I; // input stream for downloading data file // CONSTRUCTOR loader(int ls, String cb) { this.ls = ls; // load switch this.cb = cb; // URL (less file name) from where this applet came lp = 1; // start the route names loading process } // RUN THE AUXILIARY THREAD boolean dataLoaded() { switch(lp) { // THE 2 DOWNLOADING PHASES case 1: fileConnect(); // connect to data resource on server break; case 2: fileLoad(); // manage the downloading of its content } return dataLoaded; } // CONNECT TO APPROPRIATE DATA FILE ON SERVER private void fileConnect() { if(!connectFTT) return; System.out.println("Connecting to server..."); connectFTT = false; try { // set to capture any exceptions locally switch(ls) { case 0: // Create an input stream to load from local file L = (int)(new File(loadFile)).length(); I = new FileInputStream(loadFile); break; case 1: // Create an input stream to load from jar file L = 512; //default maximum content length I = getClass().getResourceAsStream(loadFile); break; case 2: // Create an input stream to load ile from server URLConnection u = new URL(cb + loadFile).openConnection(); I = u.getInputStream(); L = (int)u.getContentLength(); } B = new byte[L]; // create the gigantic buffer for the data l = 0; // number of bytes so far successfully downloaded lp = 2; // advance to the index loading phase } /* If any exception at all occurs, note what kind of exception it was and where it occurred. */ catch(Exception e) { System.out.println("fileConnect() " + e); System.out.println("Couldn't find hits data file."); lp = 4; // reset loader to its idle state } } // DOWNLOAD THE APPROPRIATE DATA FILE private void fileLoad() { if(loadingFTT) { System.out.println("Loading hits data..."); loadingFTT = false; } int k; // content of current byte being read() /* NOTE: Multiple passes of this fileLoad() are only necessary for downloading across the Internet [ls = 2]. Loading from a file in the local directory or from a jar file only need to pass once through this method.*/ try { if(ls == 1) { // L is unknown for jar files /* While the entire file has not yet been down- loaded add each new byte to the big byte array. */ while((k = I.read()) != -1) B[l++] = (byte)k; L = l; // marks the end point of the data } else // L is known for server connections and local files while(l < L && (k = I.read()) != -1) B[l++] = (byte)k; if(l >= L) { I.close(); // close URL Connection [or local file] lp = 3; // go to post-loading phase dataLoaded = true; System.out.println("Hits data loaded."); } } /* Catch any data transmission or file loading error conditions and note the kind of exception and where it occurred then display it in the terminal. */ catch(Exception e) { System.out.println("fileLoad() " + e + L + " " + l); System.out.println("Couldn't load hits data."); lp = 5; // reset loader to its idle state } } }