[Development] Added support for new "Index" parameter which sets URL prefix of .fbi and .fbk index files. Reading .fbi index file, without actually using its data.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2598 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tightvnc/rfbplayer/FbsConnection.java b/java/src/com/tightvnc/rfbplayer/FbsConnection.java
index 34f9576..66e12c7 100644
--- a/java/src/com/tightvnc/rfbplayer/FbsConnection.java
+++ b/java/src/com/tightvnc/rfbplayer/FbsConnection.java
@@ -36,17 +36,24 @@
   FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet)
       throws MalformedURLException {
 
+    // Construct URLs from strings.
     URL base = null;
     if (applet != null) {
       base = applet.getCodeBase();
     }
     fbsURL = new URL(base, fbsLocation);
-    fbiURL = null;
-    fbkURL = null;
+    fbiURL = fbkURL = null;
     if (indexLocationPrefix != null) {
-      fbiURL = new URL(base, indexLocationPrefix + ".fbi");
-      fbkURL = new URL(base, indexLocationPrefix + ".fbk");
+      try {
+        fbiURL = new URL(base, indexLocationPrefix + ".fbi");
+        fbkURL = new URL(base, indexLocationPrefix + ".fbk");
+      } catch (MalformedURLException e) {
+        fbiURL = fbkURL = null;
+      }
     }
+
+    // Try to load the .fbi index file.
+    loadIndex();
   }
 
   FbsInputStream connect(long timeOffset) throws IOException {
@@ -57,4 +64,48 @@
     return fbs;
   }
 
+  private void loadIndex() {
+    // Loading .fbi makes sense only if both .fbi and .fbk files are available.
+    if (fbiURL != null && fbkURL != null) {
+      try {
+        // Connect.
+        URLConnection connection = fbiURL.openConnection();
+        connection.connect();
+        DataInputStream is = new DataInputStream(connection.getInputStream());
+
+        // Check file signature.
+        byte[] b = new byte[12];
+        is.readFully(b);
+        if (b[0] != 'F' || b[1] != 'B' || b[2] != 'I' || b[3] != ' ' ||
+            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;
+        }
+
+        // Load index from the .fbi file.
+        // FIXME: Real loading is not implemented yet.
+        int numRecords = 0;
+        try {
+          while (true) {
+            is.readInt();
+            is.readInt();
+            is.readInt();
+            is.readInt();
+            is.readInt();
+            numRecords++;
+          }
+        } catch (EOFException e) {
+        } catch (IOException e) {
+          System.err.println("Warning: Index data may be incomplete");
+        }
+        System.err.println("Loaded index data, " + numRecords + " records");
+      } 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");
+      }
+    }
+  }
+
 }