Rewrote next() to avoid loading whole empty file.
authorRoland Haeder <roland@mxchange.org>
Fri, 30 May 2014 21:26:19 +0000 (23:26 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 30 May 2014 21:26:19 +0000 (23:26 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/main/file_directories/class_BaseFile.php

index c96a4440ee551d14e6833d5b879775d9fc310cea..1d3dac19e1e0016769992519d5088cc532867e72 100644 (file)
@@ -722,7 +722,6 @@ y    * @return      void
         * Advances to next "block" of bytes
         *
         * @return      void
         * Advances to next "block" of bytes
         *
         * @return      void
-        * @todo        This method will load large but empty files in a whole
         */
        public function next () {
                // Is there nothing to read?
         */
        public function next () {
                // Is there nothing to read?
@@ -740,17 +739,37 @@ y  * @return      void
                // Short be more than zero!
                assert($length > 0);
 
                // Short be more than zero!
                assert($length > 0);
 
-               // Wait until a entry/block separator has been found
+               // Read possibly back-buffered bytes from previous call of next().
                $data = $this->getBackBuffer();
                $data = $this->getBackBuffer();
+
+               /*
+                * Read until a entry/block separator has been found. The next read
+                * "block" may not fit, so this loop will continue until the EOB or EOF
+                * has been reached whatever comes first.
+                */
                while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
                while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
-                       // Then read the block
-                       $data .= $this->read($length);
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data));
+                       // Then read the next possible block
+                       $block = $this->read($length);
+
+                       // Is it all empty?
+                       if (strlen(trim($block)) == 0) {
+                               // Mark this block as empty
+                               $this->markCurrentBlockAsEmpty($length);
+
+                               // Skip to next block
+                               continue;
+                       } // END - if
+
+                       // At this block then
+                       $data .= $block;
+
+                       // A debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
                } // END - if
 
                // EOF reached?
                if ($this->isEndOfFileReached()) {
                } // END - if
 
                // EOF reached?
                if ($this->isEndOfFileReached()) {
-                       // Set whole data as current block
+                       // Set whole data as current read block
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
                        $this->setCurrentBlock($data);
 
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
                        $this->setCurrentBlock($data);