Code refactoring. Now RfbPlayer maintains FbsInputStream and RfbProto instances separately. Also, RfbProto does not need FbsInputStream any more, it can work with any InputStream.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2589 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tightvnc/rfbplayer/RfbPlayer.java b/java/src/com/tightvnc/rfbplayer/RfbPlayer.java
index 98b1505..8cff6e1 100644
--- a/java/src/com/tightvnc/rfbplayer/RfbPlayer.java
+++ b/java/src/com/tightvnc/rfbplayer/RfbPlayer.java
@@ -52,6 +52,7 @@
 
   String[] mainArgs;
 
+  FbsInputStream fbs;
   RfbProto rfb;
   Thread rfbThread;
 
@@ -134,7 +135,9 @@
       } else {
         url = new URL(sessionURL);
       }
-      rfb = new RfbProto(url, initialTimeOffset);
+      newFbsConnection();
+      fbs.setTimeOffset(initialTimeOffset);
+      rfb = new RfbProto(fbs);
 
       vc = new VncCanvas(this);
       gbc.weightx = 1.0;
@@ -178,19 +181,23 @@
       while (!isQuitting) {
         try {
           setPaused(!autoPlay);
-          rfb.fbs.setSpeed(playbackSpeed);
+          fbs.setSpeed(playbackSpeed);
           vc.processNormalProtocol();
         } catch (EOFException e) {
+          long newTimeOffset;
           if (e.getMessage() != null && e.getMessage().equals("[REWIND]")) {
             // A special type of EOFException allowing us to seek backwards.
-            initialTimeOffset = rfb.fbs.getSeekOffset();
-            autoPlay = !rfb.fbs.isPaused();
+            newTimeOffset = fbs.getSeekOffset();
+            autoPlay = !fbs.isPaused();
           } else {
             // Return to the beginning after the playback is finished.
-            initialTimeOffset = 0;
+            newTimeOffset = 0;
             autoPlay = false;
           }
-          rfb.newSession(url, initialTimeOffset);
+          fbs.close();
+          newFbsConnection();
+          fbs.setTimeOffset(newTimeOffset);
+          rfb.newSession(fbs);
           vc.updateFramebufferSize();
         } catch (NullPointerException e) {
           // catching this causes a hang with 1.4.1 JVM's under Win32 IE
@@ -207,6 +214,17 @@
 
   }
 
+  /**
+   * Open new connection specified by this.url, save new FbsInputStream in
+   * this.fbs.
+   *
+   * @throws java.io.IOException
+   */
+  void newFbsConnection() throws IOException {
+    URLConnection connection = url.openConnection();
+    fbs = new FbsInputStream(connection.getInputStream());
+  }
+
   public void setPausedInt(String paused) {
     // default to true (pause)
     int pause = 1;
@@ -227,9 +245,9 @@
     if (showControls)
       buttonPanel.setPaused(paused);
     if (paused) {
-      rfb.fbs.pausePlayback();
+      fbs.pausePlayback();
     } else {
-      rfb.fbs.resumePlayback();
+      fbs.resumePlayback();
     }
   }
 
@@ -239,27 +257,27 @@
 
   public void setSpeed(double speed) {
     playbackSpeed = speed;
-    rfb.fbs.setSpeed(speed);
+    fbs.setSpeed(speed);
   }
 
   public void jumpTo(long pos) {
-    long diff = Math.abs(pos - rfb.fbs.getTimeOffset());
+    long diff = Math.abs(pos - fbs.getTimeOffset());
 
     // Current threshold is 5 seconds
     if (diff > 5000) {
-      rfb.fbs.pausePlayback();
+      fbs.pausePlayback();
       setPos(pos);
-      rfb.fbs.resumePlayback();
+      fbs.resumePlayback();
     }
   }
 
   public void setPos(long pos) {
-    rfb.fbs.setTimeOffset(pos);
+    fbs.setTimeOffset(pos);
   }
 
   public void updatePos() {
     if (showControls && buttonPanel != null)
-      buttonPanel.setPos(rfb.fbs.getTimeOffset());
+      buttonPanel.setPos(fbs.getTimeOffset());
   }
 
   //
@@ -402,8 +420,12 @@
   public void destroy() {
     isQuitting = true;
     vncContainer.removeAll();
-    if (rfb != null) {
-      rfb.quit();
+    if (fbs != null) {
+      fbs.quit();
+      try {
+        fbs.close();
+      } catch (IOException e) {
+      }
     }
     try {
       rfbThread.join();
diff --git a/java/src/com/tightvnc/rfbplayer/RfbProto.java b/java/src/com/tightvnc/rfbplayer/RfbProto.java
index 4afad71..191173e 100644
--- a/java/src/com/tightvnc/rfbplayer/RfbProto.java
+++ b/java/src/com/tightvnc/rfbplayer/RfbProto.java
@@ -28,7 +28,6 @@
 package com.tightvnc.rfbplayer;
 
 import java.io.*;
-import java.net.*;
 
 class RfbProto {
 
@@ -88,52 +87,36 @@
 
   final static int TightMinToCompress = 12;
 
-  FbsInputStream fbs;
   DataInputStream is;
 
-
-  //
-  // Constructor.
-  //
-  RfbProto(URL url, long timeOffset) throws Exception {
-    fbs = null;
-    newSession(url, timeOffset);
+  /**
+   * Constructor. It calls <code>{@link #newSession(InputStream)}</code> to open
+   * new session.
+   *
+   * @param is the input stream from which RFB data will be read.
+   * @throws java.lang.Exception
+   * @throws java.io.IOException
+   */
+  RfbProto(InputStream is) throws Exception {
+    newSession(is);
   }
 
-  // Force processing to quit
-  public void quit() {
-    fbs.quit();
-    try {
-      fbs.close();
-    } catch (IOException e) {
-      System.out.println("IOException quitting RfbProto: " + e);
-    }
-  }
+  /**
+   * Open new session that can be read from the specified InputStream.
+   *
+   * @param is the input stream.
+   * @throws java.lang.Exception
+   * @throws java.io.IOException
+   */
+  public void newSession(InputStream is) throws Exception {
 
-  //
-  // Open new session URL.
-  //
-  public void newSession(URL url, long timeOffset) throws Exception {
-    if (fbs != null)
-      fbs.close();
-
-    // open the connection, and use caching
-    URLConnection connection = url.openConnection();
-    connection.setUseCaches(true);
-
-    fbs = new FbsInputStream(connection.getInputStream());
-    is = new DataInputStream(fbs);
+    this.is = new DataInputStream(is);
 
     readVersionMsg();
     if (readAuthScheme() != NoAuth) {
       throw new Exception("Wrong authentication type in the session file");
     }
     readServerInit();
-
-    // Go to initial position but make sure not to seek backwards.
-    if (timeOffset > fbs.getTimeOffset()) {
-      fbs.setTimeOffset(timeOffset);
-    }
   }
 
   //
diff --git a/java/src/com/tightvnc/rfbplayer/VncCanvas.java b/java/src/com/tightvnc/rfbplayer/VncCanvas.java
index 8c3313b..7bb8d29 100644
--- a/java/src/com/tightvnc/rfbplayer/VncCanvas.java
+++ b/java/src/com/tightvnc/rfbplayer/VncCanvas.java
@@ -353,7 +353,7 @@
 
     // Tell our FbsInputStream object to notify us when it goes to the
     // `paused' mode.
-    rfb.fbs.addObserver(this);
+    appClass.fbs.addObserver(this);
 
     //
     // main dispatch loop
@@ -1004,7 +1004,7 @@
   // Tell JVM to repaint specified desktop area.
   //
   void scheduleRepaint(int x, int y, int w, int h) {
-    if (rfb.fbs.isSeeking()) {
+    if (appClass.fbs.isSeeking()) {
       // Do nothing, and remember we are seeking.
       seekMode = true;
     } else {
@@ -1315,7 +1315,7 @@
   }
 
   void scheduleCursorRepaint(int x, int y, int w, int h, int n) {
-    if (rfb.fbs.isSeeking()) {
+    if (appClass.fbs.isSeeking()) {
       // Do nothing, and remember we are seeking.
       seekMode = true;
     } else {