/** * Creates a text file of keywords * @author Robert J Morton * @version 17 July 2009 */ // Also relative filespecs for visual checking of the index integrity. import java.io.*; class list_index { static Writer kwf; static String KW = ""; static int N, a, b, c, d, e, f, L; static byte INDEX[]; /* CONVERT A 4-BYTE TRAIN TO AN INTEGER. Called from 2 places in getKeyword(), getFS(), kwlist() and 5 places in main(). */ private static int getInt(int i) { int x0 = INDEX[i]; x0 &= 0xff; x0 <<= 24; int x1 = INDEX[i + 1]; // 1. Promote each byte to an int x1 &= 0xff; // 2. Chop off propagated 1s in case high bit set x1 <<= 16; // 3. Shift it into its correct position in the int int x2 = INDEX[i + 2]; x2 &= 0xff; x2 <<= 8; int x3 = INDEX[i + 3]; x3 &= 0xff; return x0 | x1 | x2 | x3; // 'or' them all together } // to form the complete integer /* GET THE Nth KEYWORD IN THE KEYWORD INDEX. Called from only one place in kwlist(). */ private static String getKeyword(int n) { int p = getInt(20 + ( n << 2)), // start pointer of this keyword's text q = getInt(20 + (++n << 2)); // start pointer of next keyword's text String s = ""; while(p < q) s += (char)INDEX[a + p++]; return s; // return the keyword as a string } /* RETRIEVE THE HTML FILE'S FILESPEC FROM THE INDEX Called from only one place in kwlist(). */ private static String getFS(int fsp, int fsi) { int p = fsp + (fsi << 1), // form byte reference of required filespec x2 = INDEX[p++]; x2 &= 0xff; x2 <<= 8; // make the two adjacent bytes form int // a 16-bit integer (short)rn x3 = INDEX[p]; x3 &= 0xff; int rn = x2 | x3, // form the start pointer of the filespec text P = e + getInt(d + ( rn << 2)), // form the 'end' pointer of the filespec text Q = e + getInt(d + (++rn << 2)); String s = ""; while(P < Q) s += (char)INDEX[P++]; return s; } // LIST THE KEYWORDS. Called from only one place in main(). private static void kwlist() throws IOException { kwf = new FileWriter("index.txt"); for(int i = 0; i < N; i++) { // for each keyword in the index KW = getKeyword(i); kwf.write(KW + '\n'); // write the keyword to the index text file int fsp = c + getInt(b + (i << 2)), fse = (c + getInt(b + ((i + 1) << 2)) - fsp) >> 1; for(int j = 0; j < fse; j++) kwf.write('\t' + getFS(fsp, j) + '\n'); } kwf.close(); } public static void main(String args[]) throws IOException { File f = new File("index.dat"); // create a new file object for index.dat InputStream I = new FileInputStream(f); // input file stream for index.dat L = (int)f.length(); INDEX = new byte[L]; // big byte array to hold the whole index int x, i = 0; // while we have not yet reached the end of the file while((x = I.read()) != -1) INDEX[i++] = (byte)x; // copy next byte from file to byte array a = getInt(0); // start pointer of keywords text section N = (a >> 2) - 1; // total number of keywords a += 20; b = getInt( 4) + 20; // start byte of filespec reference pointers c = getInt( 8) + 20; // start byte of filespec reference numbers d = getInt(12) + 20; // start byte of filespec text pointers e = getInt(16) + 20; // start byte of filespec text stream I.close(); // close the URL Connection's input stream System.out.println("Total number of keywords in index........ " + N); System.out.println("Start byte of keywords text section...... " + a); System.out.println("Start byte of filespec reference pointers " + b); System.out.println("Start byte of filespec reference numbers " + c); System.out.println("Start byte of filespec text pointers..... " + d); System.out.println("Start byte of filespec text section...... " + e); System.out.println("End byte of filespec text section........ " + (L - 1)); kwlist(); } }