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;
*
* @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
*
use StackableTrait;
use IndexableTrait;
+ /**
+ * Configuration cache
+ */
+ private static $configCache = [];
+
/**
* Current seek position
*/
* @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;
//* 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
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
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
//* 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?
}
/**
- * 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();
/*
//* 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;
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
}
// @TODO Unfinished
- $this->partialStub('length=' . $length);
+ DebugMiddleware::getSelfInstance()->partialStub('length=' . $length);
}
}