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\Traits\Index\IndexableTrait;
use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
}
/**
- * 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();
/*
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
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 function readNextBlock () {
+ // First calculate minimum block length
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
+ $length = $this->getIndexInstance()->calculateMinimumBlockLength();
+
+ // Call protected method
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: Calling parent::readNextBlockByLength(%d) ...', $length));
+ parent::readNextBlockByLength($length);
+
+ // Trace message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
+ }
+
+ /**
+ * Reads the file header
+ *
+ * @return void
+ */
+ public function readFileHeader () {
+ // Call index class' method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: Calling this->indexInstance->readIndexHeader() - CALLED!');
+ $this->getIndexInstance()->readIndexHeader();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: EXIT!');
+ }
+
/**
* Writes given value to the file and returns a hash and gap position for it
*
return $isValid;
}
+ /**
+ * Reads the file header
+ *
+ * @return void
+ * @throws LogicException If both instances are not set
+ */
+ public function readFileHeader () {
+ // Call stacke instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: Calling this->stackInstance->readStackHeader() - CALLED!');
+ $this->getStackInstance()->readStackHeader();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+ }
+
+ /**
+ * 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 function readNextBlock () {
+ // First calculate minimum block length
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: this->seekPosition=%d - CALLED!', $this->getSeekPosition()));
+ $length = $this->getStackInstance()->calculateMinimumBlockLength();
+
+ // Call protected method
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling parent::readNextBlockByLength(%d) ...', $length));
+ parent::readNextBlockByLength($length);
+
+ // Trace message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: EXIT!');
+ }
+
/**
* Writes given value to the file and returns a hash and gap position for it
*
}
// Encode/convert the value into a "binary format"
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling StringUtils::encodeData(value[]=%s) ...', gettype($value)));
$encoded = StringUtils::encodeData($value);
// Get a strong hash for the "encoded" data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling CryptoUtils::hash(%s) ...', $encoded));
$hash = CryptoUtils::hash($encoded);
// Then write it to the next free gap
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: Calling this->stackInstance->writeDataToFreeGap(%s,%s,%s) ...', $stackName, $hash, $encoded));
$data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
// Return info
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: data[]=%s - EXIT!', gettype($data)));
return $data;
}
$data = substr($data, 0, -1);
// And update seek position
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->updateSeekPosition() ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->binaryFileInstance->updateSeekPosition() ...');
$this->getIteratorInstance()->getBinaryFileInstance()->updateSeekPosition();
/*