]> source.dussan.org Git - tigervnc.git/commitdiff
[Refactoring] Reading an FBS data block has been separated in a new method with impro...
authorConstantin Kaplinsky <const@tightvnc.com>
Thu, 19 Jun 2008 07:51:42 +0000 (07:51 +0000)
committerConstantin Kaplinsky <const@tightvnc.com>
Thu, 19 Jun 2008 07:51:42 +0000 (07:51 +0000)
git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2593 3789f03b-4d11-0410-bbf8-ca57d06f2519

java/src/com/tightvnc/rfbplayer/FbsInputStream.java

index e12c9f5a167fb56709e2fdcb364bd05e2b271466..0e6ebfa2b3a2ce1129abc7babca582292d18d6dd 100644 (file)
@@ -190,19 +190,7 @@ class FbsInputStream extends InputStream {
     // Just wait unless we are performing playback OR seeking.
     waitWhilePaused();
 
-    bufferSize = (int)readUnsigned32();
-    if (bufferSize >= 0) {
-      int realSize = (bufferSize + 3) & 0xFFFFFFFC;
-      buffer = new byte[realSize];
-      readFully(buffer);
-      bufferPos = 0;
-      timeOffset = (long)(readUnsigned32() / playbackSpeed);
-    }
-
-    if (bufferSize < 0 || timeOffset < 0) {
-      buffer = null;
-      bufferSize = 0;
-      bufferPos = 0;
+    if (!readDataBlock(0)) {
       return false;
     }
 
@@ -230,6 +218,41 @@ class FbsInputStream extends InputStream {
     return true;
   }
 
+  /**
+   * Read FBS data block into the buffer.
+   *
+   * @param numBytesSkip specifies how many bytes should be skipped in the
+   *   beginning of the data block.
+   * @return true on success, false if end of file was reached.
+   * @throws java.io.IOException can be thrown while reading from the
+   *   underlying input stream, or as a result of bad FBS file data.
+   */
+  private boolean readDataBlock(int numBytesSkip) throws IOException {
+    // Read byte counter, check for EOF condition.
+    long readResult = readUnsigned32();
+    if (readResult < 0) {
+      return false;
+    }
+
+    bufferSize = (int)readResult;
+    if (bufferSize >= 0) {
+      int alignedSize = (bufferSize + 3) & 0xFFFFFFFC;
+      buffer = new byte[alignedSize];
+      readFully(buffer);
+      bufferPos = numBytesSkip;
+      timeOffset = (long)(readUnsigned32() / playbackSpeed);
+    }
+
+    if (bufferSize < 0 || timeOffset < 0 || bufferPos >= bufferSize) {
+      buffer = null;
+      bufferSize = 0;
+      bufferPos = 0;
+      throw new IOException("Invalid FBS file data");
+    }
+
+    return true;
+  }
+
   //
   // In paused mode, wait for external notification on this object.
   //