]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/binary/class_BaseBinaryFile.php
Continued:
[core.git] / framework / main / classes / file_directories / binary / class_BaseBinaryFile.php
index fc4140cdb3796689b172bc2df7c5a3b7fb8b82fc..04e6da4eef30c6fd093025026cebfce592b9ac75 100644 (file)
@@ -1,18 +1,25 @@
 <?php
 // Own namespace
-namespace CoreFramework\Filesystem\File;
+namespace Org\Mxchange\CoreFramework\Filesystem\File;
 
 // Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Filesystem\Block;
-use CoreFramework\Filesystem\Block\CalculatableBlock;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Filesystem\Block;
+use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock;
+use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
+
+// Import SPL stuff
+use \BadMethodCallException;
+use \InvalidArgumentException;
+use \SplFileInfo;
 
 /**
  * A general binary file class
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -29,7 +36,7 @@ use CoreFramework\Filesystem\Block\CalculatableBlock;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseBinaryFile extends BaseAbstractFile {
+abstract class BaseBinaryFile extends BaseAbstractFile {
        /**
         * Separator for header data
         */
@@ -105,17 +112,17 @@ class BaseBinaryFile extends BaseAbstractFile {
        /**
         * File header
         */
-       private $header = array();
+       private $header = [];
 
        /**
         * Seek positions for gaps ("fragmentation")
         */
-       private $gaps = array();
+       private $gaps = [];
 
        /**
         * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
         */
-       private $damagedEntries = array();
+       private $damagedEntries = [];
 
        /**
         * Back-buffer
@@ -127,13 +134,18 @@ class BaseBinaryFile extends BaseAbstractFile {
         */
        private $currentBlock = '';
 
+       /**
+        * An instance of a Block class
+        */
+       private $blockInstance = NULL;
+
        /**
         * Protected constructor
         *
         * @param       $className      Name of the class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
                parent::__construct($className);
 
@@ -141,6 +153,25 @@ class BaseBinaryFile extends BaseAbstractFile {
                $this->initCountersGapsArray();
        }
 
+       /**
+        * Setter for Block instance
+        *
+        * @param       $blockInstance  An instance of an Block class
+        * @return      void
+        */
+       protected final function setBlockInstance (Block $blockInstance) {
+               $this->blockInstance = $blockInstance;
+       }
+
+       /**
+        * Getter for Block instance
+        *
+        * @return      $blockInstance  An instance of an Block class
+        */
+       public final function getBlockInstance () {
+               return $this->blockInstance;
+       }
+
        /**
         * Checks whether the abstracted file only contains gaps by counting all
         * gaps' bytes together and compare it to total length.
@@ -152,8 +183,8 @@ class BaseBinaryFile extends BaseAbstractFile {
                /* Only for debugging
                if (isset($this->gaps[0])) {
                        // Output first and last gap
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', __METHOD__, __LINE__, print_r($this->gaps[0], TRUE), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], TRUE)));
-               } // END - if
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', print_r($this->gaps[0], true), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], true)));
+               }
                */
 
                // Now count every gap
@@ -161,10 +192,10 @@ class BaseBinaryFile extends BaseAbstractFile {
                foreach ($this->gaps as $gap) {
                        // Calculate size of found gap: end-start including both
                        $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
-               } // END - if
+               }
 
                // Debug output
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', $gapsSize, $this->getHeaderSize()));
 
                // Total gap size + header size must be same as file size
                $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
@@ -187,8 +218,8 @@ class BaseBinaryFile extends BaseAbstractFile {
                $this->setSeekPosition(0);
 
                // Init arrays
-               $this->gaps = array();
-               $this->damagedEntries = array();
+               $this->gaps = [];
+               $this->damagedEntries = [];
        }
 
        /**
@@ -207,7 +238,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $headerSize             Size of file header
         * @return      void
         */
-       public final function setHeaderSize ($headerSize) {
+       public final function setHeaderSize (int $headerSize) {
                // Set it
                $this->headerSize = $headerSize;
        }
@@ -249,7 +280,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $seekPosition   Current seek position (stored here in object)
         * @return      void
         */
-       protected final function setSeekPosition ($seekPosition) {
+       protected final function setSeekPosition (int $seekPosition) {
                // And set it
                $this->seekPosition = $seekPosition;
        }
@@ -260,16 +291,16 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        public function updateSeekPosition () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
 
                // Get key (= seek position)
                $seekPosition = $this->key();
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', $seekPosition));
 
                // And set it here
                $this->setSeekPosition($seekPosition);
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
        }
 
        /**
@@ -279,7 +310,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        protected function rewindUpdateSeekPosition () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
 
                // flushFileHeader must be callable
                assert(is_callable(array($this, 'flushFileHeader')));
@@ -293,7 +324,7 @@ class BaseBinaryFile extends BaseAbstractFile {
                // ... to write it back into the file
                $this->flushFileHeader();
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
        }
 
        /**
@@ -302,12 +333,12 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        protected function seekToOldPosition () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
 
                // Seek to currently ("old") saved position
                $this->seek($this->getSeekPosition());
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
        }
 
        /**
@@ -318,7 +349,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         */
        public static function isBlockSeparatorFound ($str) {
                // Determine it
-               $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
+               $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
 
                // Return result
                return $isFound;
@@ -340,10 +371,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $backBuffer             Characters to "store" in back-buffer
         * @return      void
         */
-       private function setBackBuffer ($backBuffer) {
-               // Cast to string (so no arrays or objects)
-               $backBuffer = (string) $backBuffer;
-
+       private function setBackBuffer (string $backBuffer) {
                // ... and set it
                $this->backBuffer = $backBuffer;
        }
@@ -363,10 +391,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $currentBlock           Characters to set a currently loaded block
         * @return      void
         */
-       private function setCurrentBlock ($currentBlock) {
-               // Cast to string (so no arrays or objects)
-               $currentBlock = (string) $currentBlock;
-
+       private function setCurrentBlock (string $currentBlock) {
                // ... and set it
                $this->currentBlock = $currentBlock;
        }
@@ -384,15 +409,20 @@ class BaseBinaryFile extends BaseAbstractFile {
        /**
         * Initializes this file class
         *
-        * @param       $fileName       Name of this abstract file
+        * @param       $fileInfoInstance       An instance of a SplFileInfo class
         * @return      void
         */
-       protected function initFile ($fileName) {
+       protected function initFile (SplFileInfo $fileInfoInstance) {
                // Get a file i/o pointer instance
-               $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance));
+               $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileInfoInstance));
 
                // ... and set it here
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting pointerInstance=%s ...', $pointerInstance->__toString()));
                $this->setPointerInstance($pointerInstance);
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
@@ -402,9 +432,18 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $data                   Data to be written
         * @param       $flushHeader    Whether to flush the header (default: flush)
         * @return      void
-        */
-       public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
+        * @throws      InvalidArgumentException        If a parameter is invalid
+        */
+       public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
+               // Validate parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%s,data()=%d,flushHeader=%d - CALLED!', $seekPosition, strlen($data), intval($flushHeader)));
+               if ($seekPosition < 0) {
+                       // Invalid seek position
+                       throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
+               } elseif (empty($data)) {
+                       // Empty data is invalid, too
+                       throw new InvalidArgumentException('Parameter "data" is empty');
+               }
 
                // Write data at given position
                $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
@@ -416,15 +455,16 @@ class BaseBinaryFile extends BaseAbstractFile {
                $this->updateSeekPosition();
 
                // Flush the header?
-               if ($flushHeader === TRUE) {
+               if ($flushHeader === true) {
                        // Flush header
                        $this->flushFileHeader();
 
                        // Seek to old position
                        $this->seekToOldPosition();
-               } // END - if
+               }
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
@@ -432,8 +472,16 @@ class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $length         Length of the block
         * @return      void
-        */
-       protected function markCurrentBlockAsEmpty ($length) {
+        * @throws      InvalidArgumentException        If a parameter is invalid
+        */
+       protected function markCurrentBlockAsEmpty (int $length) {
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
+               if ($length < 1) {
+                       // Length cannot below one
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               }
+
                // Get current seek position
                $currentPosition = $this->key();
 
@@ -442,6 +490,9 @@ class BaseBinaryFile extends BaseAbstractFile {
                        self::GAPS_INDEX_START  => ($currentPosition - $length),
                        self::GAPS_INDEX_END    => $currentPosition,
                ));
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
@@ -450,32 +501,31 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      $isInitialized  Whether the file header is initialized
         */
        public function isFileHeaderInitialized () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
                // Default is not initialized
-               $isInitialized = FALSE;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+               $isInitialized = false;
 
                // Is the file initialized?
                if ($this->isFileInitialized()) {
                        // Some bytes has been written, so rewind to start of it.
                        $rewindStatus = $this->rewind();
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
 
                        // Is the rewind() call successfull?
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: rewindStatus=%d', $rewindStatus));
                        if ($rewindStatus != 1) {
                                // Something bad happened
-                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
-                       } // END - if
+                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Could not rewind().');
+                       }
 
                        // Read file header
                        $this->readFileHeader();
 
                        // The above method does already check the header
-                       $isInitialized = TRUE;
-               } // END - if
+                       $isInitialized = true;
+               }
 
                // Return result
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
                return $isInitialized;
        }
 
@@ -485,14 +535,13 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      $isInitialized          Whether the file's size is zero
         */
        public function isFileInitialized () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
-               // Get it from iterator which holds the pointer instance. If FALSE is returned
+               // Get it from iterator which holds the pointer instance. If false is returned
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
                $fileSize = $this->size();
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize=%s', $fileSize));
 
                /*
-                * The returned file size should not be FALSE or NULL as this means
+                * The returned file size should not be false or NULL as this means
                 * that the pointer class does not work correctly.
                 */
                assert(is_int($fileSize));
@@ -501,7 +550,7 @@ class BaseBinaryFile extends BaseAbstractFile {
                $isInitialized = ($fileSize > 0);
 
                // Return result
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
                return $isInitialized;
        }
 
@@ -511,9 +560,8 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        public function createFileHeader () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
                // The file's header should not be initialized here
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
                assert(!$this->isFileHeaderInitialized());
 
                // Simple flush file header which will create it.
@@ -522,7 +570,8 @@ class BaseBinaryFile extends BaseAbstractFile {
                // Rewind seek position (to beginning of file) and update/flush file header
                $this->rewindUpdateSeekPosition();
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
@@ -530,37 +579,39 @@ class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $type   Type of the file
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is empty
         */
-       public function preAllocateFile ($type) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
+       public function preAllocateFile (string $type) {
                // Is it enabled?
-               if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
-                       // Not enabled
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
-
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s - CALLED!', $type));
+               if (empty($type)) {
+                       // Empty type
+                       throw new InvalidArgumentException('Parameter "type" is empty');
+               } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
                        // Don't continue here.
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Not pre-allocating file.'));
                        return;
-               } // END - if
+               }
 
                // Message to user
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
 
                // Calculate minimum length for one entry
                $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
 
                // Calulcate seek position
-               $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: minLengthEntry=%s', $minLengthEntry));
+               $seekPosition = $minLengthEntry * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count');
 
                // Now simply write a NUL there. This will pre-allocate the file.
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
                $this->writeData($seekPosition, chr(0));
 
                // Rewind seek position (to beginning of file) and update/flush file header
                $this->rewindUpdateSeekPosition();
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
@@ -580,7 +631,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $whence         Added to offset (default: only use offset to seek to)
         * @return      $status         Status of file seek: 0 = success, -1 = failed
         */
-       public function seek ($offset, $whence = SEEK_SET) {
+       public function seek (int $offset, $whence = SEEK_SET) {
                // Call pointer instance
                return $this->getPointerInstance()->seek($offset, $whence);
        }
@@ -591,10 +642,7 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
         */
-       public function read ($bytes = NULL) {
-               // $bytes shall be integer
-               assert(is_int($bytes));
-
+       public function read (int $bytes = 0) {
                // Call pointer instance
                return $this->getPointerInstance()->read($bytes);
        }
@@ -615,18 +663,21 @@ class BaseBinaryFile extends BaseAbstractFile {
         * only gaps are found, the file is considered as "virgin" (no entries).
         *
         * @return      void
+        * @throws      BadMethodCallException  If this method is called but file is not initialized
         */
        public function analyzeFile () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
                // Make sure the file is initialized
-               assert($this->isFileInitialized());
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+               if (!$this->isFileInitialized()) {
+                       // Bad method call
+                       throw new BadMethodCallException('Method called but file is not initialized.');
+               }
 
                // Init counters and gaps array
                $this->initCountersGapsArray();
 
                // Output message (as this may take some time)
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Analyzing file structure ... (this may take some time)'));
 
                // First rewind to the begining
                $this->rewind();
@@ -646,22 +697,22 @@ class BaseBinaryFile extends BaseAbstractFile {
                        if (empty($current)) {
                                // Then skip this part
                                continue;
-                       } // END - if
+                       }
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
-               } // END - while
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('current()=%d', strlen($current)));
+               }
 
                // If the last read block is empty, check gaps
                if (empty($current)) {
                        // Output message
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Found a total of %s gaps.', count($this->gaps)));
 
                        // Check gaps, if the whole file is empty.
                        if ($this->isFileOnlyGaps()) {
                                // Only gaps, so don't continue here.
                                return;
-                       } // END - if
+                       }
 
                        /*
                         * The above call has calculated a total size of all gaps. If the
@@ -672,35 +723,29 @@ class BaseBinaryFile extends BaseAbstractFile {
                        if ($this->isDefragmentationNeeded()) {
                                // Run "defragmentation"
                                $this->doRunDefragmentation();
-                       } // END - if
-               } // END - if
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+                       }
+               }
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
         * Advances to next "block" of bytes
         *
         * @return      void
+        * @throws      UnexpectedValueException        If some unexpected value was found
         */
        public function next () {
                // Is there nothing to read?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
                if (!$this->valid()) {
                        // Nothing to read
                        return;
-               } // END - if
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
-
-               // Make sure the block instance is set
-               assert($this->getBlockInstance() instanceof CalculatableBlock);
+               }
 
                // First calculate minimum block length
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: key()=%d', $this->key()));
                $length = $this->getBlockInstance()->calculateMinimumBlockLength();
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
-
-               // Short be more than zero!
-               assert($length > 0);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%s', $length));
 
                // Read possibly back-buffered bytes from previous call of next().
                $data = $this->getBackBuffer();
@@ -710,39 +755,40 @@ class BaseBinaryFile extends BaseAbstractFile {
                 * "block" may not fit, so this loop will continue until the EOB or EOF
                 * has been reached whatever comes first.
                 */
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
                while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
                        // Then read the next possible block
                        $block = $this->read($length);
 
-                       // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
-
                        // Is it all empty?
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: block()=%d,length=%s', strlen($block), $length));
                        if (strlen(trim($block)) == 0) {
                                // Mark this block as empty
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling markCurrentBlockAsEmpty(%d) ...', strlen($block)));
                                $this->markCurrentBlockAsEmpty(strlen($block));
 
                                // Skip to next block
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CONTINUE!');
                                continue;
-                       } // END - if
+                       }
 
                        // At this block then
                        $data .= $block;
 
                        // A debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
-               } // END - while
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
+               }
 
                // EOF reached?
                if ($this->isEndOfFileReached()) {
                        // Set whole data as current read block
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling setCurrentBlock(%d) ...', strlen($data)));
                        $this->setCurrentBlock($data);
 
                        // Then abort here silently
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EOF reached - EXIT!');
                        return;
-               } // END - if
+               }
 
                /*
                 * Init back-buffer which is the data that has been found beyond the
@@ -754,25 +800,29 @@ class BaseBinaryFile extends BaseAbstractFile {
                $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
 
                // This array must contain two elements
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
-               assert(count($dataArray) == 2);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: dataArray=' . print_r($dataArray, true));
+               if (count($dataArray) != 2) {
+                       // Bad count
+                       throw new UnexpectedValueException(sprintf('dataArray()=%d is not expected, want 2', count($dataArray)));
+               }
 
                // Left part is the actual block, right one the back-buffer data
                $this->setCurrentBlock($dataArray[0]);
                $this->setBackBuffer($dataArray[1]);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
        /**
         * Checks wether the current entry is valid (not at the end of the file).
-        * This method will return TRUE if an emptied (nulled) entry has been found.
+        * This method will return true if an emptied (nulled) entry has been found.
         *
         * @return      $isValid        Whether the next entry is valid
         */
        public function valid () {
-               // Make sure the block instance is set
-               assert($this->getBlockInstance() instanceof Block);
-
                // First calculate minimum block length
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
                $length = $this->getBlockInstance()->calculateMinimumBlockLength();
 
                // Short be more than zero!
@@ -819,9 +869,6 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        public function readFileHeader () {
-               // Make sure the block instance is set
-               assert($this->getBlockInstance() instanceof Block);
-
                // Call block instance
                $this->getBlockInstance()->readFileHeader();
        }
@@ -832,9 +879,6 @@ class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        public function flushFileHeader () {
-               // Make sure the block instance is set
-               assert($this->getBlockInstance() instanceof Block);
-
                // Call block instance
                $this->getBlockInstance()->flushFileHeader();
        }
@@ -845,13 +889,18 @@ class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $length                 Length of raw data
         * @return      $seekPosition   Found next gap's seek position
+        * @throws      InvalidArgumentException        If the parameter is not valid
         */
-       public function searchNextGap ($length) {
+       public function searchNextGap (int $length) {
                // If the file is only gaps, no need to seek
-               if ($this->isFileOnlyGaps()) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
+               if ($length <= 0) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               } elseif ($this->isFileOnlyGaps()) {
                        // The first empty block is the first one right after the header
                        return ($this->getHeaderSize() + 1);
-               } // END - if
+               }
 
                // @TODO Unfinished
                $this->partialStub('length=' . $length);