]> 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 8fcaeca9dcba94f78af136793548859d83bcfb26..a7a1dcfd3ff0de9c228b56e122f59dbacebf26af 100644 (file)
@@ -8,8 +8,8 @@ use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
-use Org\Mxchange\CoreFramework\Index\Indexable;
-use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
 use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
 
@@ -26,7 +26,7 @@ use \UnexpectedValueException;
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -48,6 +48,11 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        use StackableTrait;
        use IndexableTrait;
 
+       /**
+        * Configuration cache
+        */
+       private static $configCache = [];
+
        /**
         * Current seek position
         */
@@ -247,9 +252,16 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
         * @return      void
         */
        private function markFileGapsOnly (string $type, int $minimumBlockLength) {
-               // Very simple to do ...
+               // Is config cache there?
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s,minimumBlockLength=%d - CALLED!', $type, $minimumBlockLength));
-               for ($idx = 0; $idx < FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); $idx++) {
+               if (!isset(self::$configCache[$type . '_pre_allocate_count'])) {
+                       // Then set it
+                       self::$configCache[$type . '_pre_allocate_count'] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count');
+               }
+
+               // Very simple to do ...
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: self:configCache[%s_pre_allocate_count]()=%d', count(self::$configCache[$type . '_pre_allocate_count'])));
+               for ($idx = 0; $idx < self::$configCache[$type . '_pre_allocate_count']; $idx++) {
                        // Calculate start/end positions
                        $startPosition = $idx * $minimumBlockLength;
                        $endPosition = $idx * $minimumBlockLength + $minimumBlockLength;
@@ -448,7 +460,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: str=%s - CALLED!', $str));
                if (empty($str)) {
                        // Throw IAE
-                       throw new InvalidArgumentException('Parameter "str" is empty');
+                       throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // Determine it
@@ -477,7 +489,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                        throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition));
                } elseif (empty($data)) {
                        // Empty data is invalid, too
-                       throw new InvalidArgumentException('Parameter "data" is empty');
+                       throw new InvalidArgumentException('Parameter "data" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // Write data at given position
@@ -525,7 +537,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                        throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition));
                } elseif (empty($dataStream)) {
                        // Empty dataStream
-                       throw new InvalidArgumentException('Parameter "dataStream" is empty');
+                       throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // Call pointer's method
@@ -587,7 +599,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize[%s]=%d', gettype($fileSize), $fileSize));
                if (!is_int($fileSize)) {
                        // Bad file?
-                       throw new UnexpectedValueException(sprintf('fileSize[]=%s is unexpected', gettype($fileSize)));
+                       throw new UnexpectedValueException(sprintf('fileSize[]=%s is unexpected', gettype($fileSize)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
                }
 
                // Is more than 0 returned?
@@ -791,19 +803,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        }
 
        /**
-        * Reads next "block" of bytes into $currentBlock field. THis method loads
-        * the whole file into memory when the file is just freshly initialized
-        * (only zeros in it).
+        * Reads next "block" of given bytes into $currentBlock field. THis method
+        * loads the whole file into memory when the file is just freshly
+        * initialized (only zeros in it).
         *
         * @return      void
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       private function readNextBlock () {
-               // First calculate minimum block length
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
-               $length = $this->getIndexInstance()->calculateMinimumBlockLength();
+       protected function readNextBlockByLength (int $length) {
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d - CALLED!', $this->getSeekPosition(), $length));
+               if ($length < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               }
 
                // Read possibly back-buffered bytes from previous call of next().
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d,length=%d', $this->getSeekPosition(), $length));
                $data = $this->getBackBuffer();
 
                /*
@@ -884,11 +899,11 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s,minimumBlockLength=%d - CALLED!', $type, $minimumBlockLength));
                if (empty($type)) {
                        // Empty type
-                       throw new InvalidArgumentException('Parameter "type" is empty');
+                       throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                } elseif ($minimumBlockLength < 1) {
                        // Invalid block length
                        throw new InvalidArgumentException(sprintf('Parameter minimumBlockLength=%d is not valid', $minimumBlockLength));
-               } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
+               } elseif (!FrameworkBootstrap::getConfigurationInstance()->isEnabled($type . '_pre_allocate')) {
                        // Don't continue here.
                        self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Not pre-allocating file.'));
                        return;
@@ -970,31 +985,22 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                return $isValid;
        }
 
+       /**
+        * Reads next "block" of bytes into $currentBlock field. THis method loads
+        * the whole file into memory when the file is just freshly initialized
+        * (only zeros in it).
+        *
+        * @return      void
+        */
+       protected abstract function readNextBlock ();
+
        /**
         * Reads the file header
         *
         * @return      void
         * @throws      LogicException  If both instances are not set
         */
-       public function readFileHeader () {
-               // Is index set or stack?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               if ($this->getIndexInstance() instanceof Indexable) {
-                       // Call index instance
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexInstance->readIndexHeader() ...');
-                       $this->getIndexInstance()->readIndexHeader();
-               } elseif ($this->getStackInstance() instanceof StackableFile) {
-                       // Call stacke instance
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->stackInstance->readStackHeader() ...');
-                       $this->getStackInstance()->readStackHeader();
-               } else {
-                       // Bad logic?
-                       throw new LogicException('Wether indexInstance nor stackInstance are set');
-               }
-
-               // Trace message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
-       }
+       public abstract function readFileHeader ();
 
        /**
         * Searches for next suitable gap the given length of data can fit in
@@ -1023,7 +1029,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                }
 
                // @TODO Unfinished
-               $this->partialStub('length=' . $length);
+               DebugMiddleware::getSelfInstance()->partialStub('length=' . $length);
        }
 
 }