]> 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 819e78a6e63bbe2734b541bc31010e085959c042..04e6da4eef30c6fd093025026cebfce592b9ac75 100644 (file)
@@ -4,13 +4,14 @@ namespace Org\Mxchange\CoreFramework\Filesystem\File;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+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;
 
 /**
@@ -408,15 +409,16 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
        /**
         * Initializes this file class
         *
-        * @param       $infoInstance   An instance of a SplFileInfo class
+        * @param       $fileInfoInstance       An instance of a SplFileInfo class
         * @return      void
         */
-       protected function initFile (SplFileInfo $infoInstance) {
+       protected function initFile (SplFileInfo $fileInfoInstance) {
                // Get a file i/o pointer instance
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: infoInstance=%s - CALLED!', $infoInstance));
-               $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance));
+               /* 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
@@ -430,10 +432,20 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         * @param       $data                   Data to be written
         * @param       $flushHeader    Whether to flush the header (default: flush)
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is invalid
         */
        public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
-               // Write data at given position
+               // 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);
 
                // Increment counter
@@ -460,10 +472,17 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $length         Length of the block
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is invalid
         */
        protected function markCurrentBlockAsEmpty (int $length) {
-               // Get current seek position
+               // 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();
 
                // Now add it as gap entry
@@ -490,12 +509,12 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                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('rewindStatus=%s', $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('Could not rewind().'));
+                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Could not rewind().');
                        }
 
                        // Read file header
@@ -506,7 +525,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                }
 
                // Return result
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized)));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
                return $isInitialized;
        }
 
@@ -516,11 +535,10 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         * @return      $isInitialized          Whether the file's size is zero
         */
        public function isFileInitialized () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-
                // 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('fileSize=%s', $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
@@ -532,7 +550,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                $isInitialized = ($fileSize > 0);
 
                // Return result
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized)));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
                return $isInitialized;
        }
 
@@ -542,9 +560,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         * @return      void
         */
        public function createFileHeader () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-
                // 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.
@@ -553,6 +570,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                // Rewind seek position (to beginning of file) and update/flush file header
                $this->rewindUpdateSeekPosition();
 
+               // Trace message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
        }
 
@@ -561,30 +579,32 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $type   Type of the file
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is empty
         */
        public function preAllocateFile (string $type) {
                // Is it enabled?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
-                       // Not enabled
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Not pre-allocating file.'));
-
+               /* 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;
                }
 
                // Message to user
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Pre-allocating file ...'));
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
 
                // Calculate minimum length for one entry
                $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
 
                // Calulcate seek position
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('minLengthEntry=%s', $minLengthEntry));
+               /* 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('seekPosition=%s', $seekPosition));
+               /* 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
@@ -622,7 +642,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
         */
-       public function read (int $bytes = NULL) {
+       public function read (int $bytes = 0) {
                // Call pointer instance
                return $this->getPointerInstance()->read($bytes);
        }
@@ -869,10 +889,15 @@ abstract 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 (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);
                }