/** * 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 = "ts.dat", // name of the route names file cb; // URL path to data files private BufferedReader R; // for reading route & waypoint lists from files private InputStream I; // input stream for downloading file 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 } boolean dataLoaded() { // RUN THE AUXILIARY THREAD switch(lp) { // THE 2 DOWNLOADING PHASES case 1: fileConnect(); // connect to index resource on server break; case 2: fileLoad(); // manage the downloading of its content } return dataLoaded; } private void fileConnect() { // CONNECT TO APPROPRIATE DATA FILE ON SERVER if(!connectFTT) return; System.out.println("Connecting to server..."); connectFTT = false; try { // set to capture any exceptions locally switch(ls) { case 0: L = (int)(new File(loadFile)).length(); I = new FileInputStream(loadFile); break; case 1: // Create an input stream to load ile 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 } } private void fileLoad() { // DOWNLOAD THE APPROPRIATE DATA FILE 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((k = I.read()) != -1) // While the entire file has not yet B[l++] = (byte)k; // been downloaded,add each new byte // to the big byte array. L = l; // marks end point of data } else // L is known for local files and server connections 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(Exception e) { // catch any data transmission or // file loading error conditions System.out.println( "fileLoad() " // note where the exception occurred + e // and what type of exception it was + L + " " // total length of required content + l // how much of it was downloaded before exception ); System.out.println("Couldn't load hits data."); lp = 5; // reset loader to its idle state } } }