[Bugfixes, enhancements] Fixed various bugs with loading index from .fbi file, significantly improved error handling. Now the index data is kept as a usual array of FbsEntryPoint records, Vector is not used any more.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2603 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tightvnc/rfbplayer/FbsConnection.java b/java/src/com/tightvnc/rfbplayer/FbsConnection.java
index 96b3c44..009fc66 100644
--- a/java/src/com/tightvnc/rfbplayer/FbsConnection.java
+++ b/java/src/com/tightvnc/rfbplayer/FbsConnection.java
@@ -34,8 +34,8 @@
   URL fbiURL;
   URL fbkURL;
 
-  /** Index data read from the .fbi file. */
-  Vector idx;
+  /** Index data loaded from the .fbi file. */
+  FbsEntryPoint idx[];
 
   FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet)
       throws MalformedURLException {
@@ -69,9 +69,14 @@
     return fbs;
   }
 
+  /**
+   * Load index data from .fbi file to {@link #idx idx}.
+   */
   private void loadIndex() {
     // Loading .fbi makes sense only if both .fbi and .fbk files are available.
     if (fbiURL != null && fbkURL != null) {
+      FbsEntryPoint[] newIndex;
+      int numRecordsRead = 0;
       try {
         // Connect.
         URLConnection connection = fbiURL.openConnection();
@@ -85,32 +90,52 @@
             b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' ||
             b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' ||
             b[10] < '0' || b[10] > '9' || b[11] != '\n') {
-          System.err.println("Warning: bad .fbi file data, not using index");
-          fbiURL = null;
+          System.err.println("Could not load index: bad .fbi file signature");
+          return;
         }
 
+        // Read the record counter and allocate index array.
+        int numRecords = is.readInt();
+        if (numRecords <= 0) {
+          System.err.println("Could not load index: bad .fbi record counter");
+          return;
+        }
+        newIndex = new FbsEntryPoint[numRecords];
+
         // Load index from the .fbi file.
-        Vector newIndex = new Vector();
-        FbsEntryPoint record = new FbsEntryPoint();
         try {
-          while (true) {
-            record.timestamp = is.readInt();
-            record.key_fpos = is.readInt();
-            record.key_size = is.readInt();
-            record.fbs_fpos = is.readInt();
-            record.fbs_skip = is.readInt();
-            newIndex.add(record);
+          for (int i = 0; i < numRecords; i++) {
+            FbsEntryPoint record = new FbsEntryPoint();
+            record.timestamp = (long)is.readInt() & 0xFFFFFFFFL;
+            record.key_fpos = (long)is.readInt() & 0xFFFFFFFFL;
+            record.key_size = (long)is.readInt() & 0xFFFFFFFFL;
+            record.fbs_fpos = (long)is.readInt() & 0xFFFFFFFFL;
+            record.fbs_skip = (long)is.readInt() & 0xFFFFFFFFL;
+            newIndex[i] = record;
+            numRecordsRead++;
           }
         } catch (EOFException e) {
+          System.err.println("Preliminary end of .fbi file");
         } catch (IOException e) {
-          System.err.println("Warning: Index data may be incomplete");
+          System.err.println("Ignored exception: " + e);
         }
-        idx = newIndex;
-        System.err.println("Loaded index data, " + idx.size() + " records");
+        if (numRecordsRead == 0) {
+          System.err.println("Could not load index: failed to read .fbi data");
+          return;
+        } else if (numRecordsRead != numRecords) {
+          System.err.println("Warning: read not as much .fbi data as expected");
+        }
+      } catch (FileNotFoundException e) {
+        System.err.println("Could not load index: .fbi file not found: " +
+                           e.getMessage());
+        return;
       } catch (IOException e) {
-        System.err.println("Warning: I/O exception while loading index: " + e);
-        System.err.println("Warning: failed to load .fbi, not using index");
+        System.err.println(e);
+        System.err.println("Could not load index: failed to load .fbi file");
+        return;
       }
+      idx = newIndex;
+      System.err.println("Loaded index data, " + numRecordsRead + " records");
     }
   }