]> source.dussan.org Git - tigervnc.git/commitdiff
[Development] Indexed seeking (using HTTP 1.1 Range request headers) is working for...
authorConstantin Kaplinsky <const@tightvnc.com>
Sun, 22 Jun 2008 07:24:11 +0000 (07:24 +0000)
committerConstantin Kaplinsky <const@tightvnc.com>
Sun, 22 Jun 2008 07:24:11 +0000 (07:24 +0000)
git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2612 3789f03b-4d11-0410-bbf8-ca57d06f2519

java/src/com/tightvnc/rfbplayer/FbsConnection.java

index 4e83335cb3c92c3eedc472faa08017a191471899..9b38652ed27f9287d73ea86a4bfd683949a717fe 100644 (file)
@@ -195,36 +195,63 @@ public class FbsConnection {
     // Make sure the protocol is HTTP.
     if (!fbkURL.getProtocol().equalsIgnoreCase("http") ||
         !fbsURL.getProtocol().equalsIgnoreCase("http")) {
+      System.err.println("Indexed access requires HTTP protocol in URLs");
       return null;
     }
 
-    // Prepare URLConnection to the right part of the .fbk file.
+    // Request RFB initialization.
+    // FIXME: Check return value of openHttpByteRange(), it can be null.
     InputStream is =
-        openByteRange(fbkURL, entryPoint.key_fpos, entryPoint.key_size);
+        openHttpByteRange(fbkURL, 12, indexData[0].key_fpos - 12);
     DataInputStream dis = new DataInputStream(is);
 
+    // Read RFB initialization.
+    int initDataSize = dis.readInt();
+    byte[] initData = new byte[initDataSize];
+    dis.readFully(initData);
+    dis.close();
+
+    // Seek to the keyframe.
+    // FIXME: Check return value of openHttpByteRange(), it can be null.
+    is = openHttpByteRange(fbkURL, entryPoint.key_fpos, entryPoint.key_size);
+    dis = new DataInputStream(is);
+
     // Load keyframe data from the .fbk file.
     int keyDataSize = dis.readInt();
     byte[] keyData = new byte[keyDataSize];
     dis.readFully(keyData);
     dis.close();
 
+    // Concatenate init and keyframe data.
+    // FIXME: Get rid of concatenation, read both parts to one array.
+    byte[] allData = new byte[initDataSize + keyDataSize];
+    System.arraycopy(initData, 0, allData, 0, initDataSize);
+    System.arraycopy(keyData, 0, allData, initDataSize, keyDataSize);
+
     // Open the FBS stream.
-    URLConnection fbsConn = fbsURL.openConnection();
-    fbsConn.connect();
-    return new FbsInputStream(fbsConn.getInputStream(), entryPoint.timestamp,
-                              keyData, entryPoint.fbs_skip);
+    // FIXME: Check return value of openHttpByteRange(), it can be null.
+    is = openHttpByteRange(fbsURL, entryPoint.fbs_fpos, -1);
+    return new FbsInputStream(is, entryPoint.timestamp, allData,
+                              entryPoint.fbs_skip);
   }
 
-  private static InputStream openByteRange(URL url, long offset, long length)
+  private static InputStream openHttpByteRange(URL url, long offset, long len)
       throws IOException {
-    URLConnection conn = url.openConnection();
-    long lastByteOffset = offset + length - 1;
-    String rangeSpec = "bytes=" + offset + "-" + lastByteOffset;
-    System.err.println("Range: " + rangeSpec);
+    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+    String rangeSpec = "bytes=" + offset + "-";
+    if (len != -1) {
+      long lastByteOffset = offset + len - 1;
+      rangeSpec += lastByteOffset;
+    }
     conn.setRequestProperty("Range", rangeSpec);
     conn.connect();
-    return conn.getInputStream();
+    InputStream is = conn.getInputStream();
+    if (conn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) {
+      System.err.println("HTTP server does not support Range request headers");
+      is.close();
+      return null;
+    }
+    return is;
   }
 
 }