From: Constantin Kaplinsky Date: Thu, 19 Jun 2008 07:51:42 +0000 (+0000) Subject: [Refactoring] Reading an FBS data block has been separated in a new method with impro... X-Git-Tag: v0.0.90~438 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ab0eaa24c3a964445529a75cfb15a59279300431;p=tigervnc.git [Refactoring] Reading an FBS data block has been separated in a new method with improved error handling. git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2593 3789f03b-4d11-0410-bbf8-ca57d06f2519 --- diff --git a/java/src/com/tightvnc/rfbplayer/FbsInputStream.java b/java/src/com/tightvnc/rfbplayer/FbsInputStream.java index e12c9f5a..0e6ebfa2 100644 --- a/java/src/com/tightvnc/rfbplayer/FbsInputStream.java +++ b/java/src/com/tightvnc/rfbplayer/FbsInputStream.java @@ -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. //