]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/binary/class_BaseBinaryFile.php
Refacuring / possible WIP:
[core.git] / framework / main / classes / file_directories / binary / class_BaseBinaryFile.php
index fb96ae762645c10d670eb39d45048aeaceaa0aba..95c642eb066f72b4330566218d486794b351fdea 100644 (file)
@@ -6,18 +6,17 @@ namespace Org\Mxchange\CoreFramework\Filesystem\File;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
 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;
+use Org\Mxchange\CoreFramework\Index\Indexable;
 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
-use Org\Mxchange\CoreFramework\Index\Stack\IndexableStack;
-use Org\Mxchange\CoreFramework\Traits\Index\Stack\IndexableStackTrait;
+use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
 use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
 
 // Import SPL stuff
 use \BadMethodCallException;
 use \InvalidArgumentException;
 use \LogicException;
+use \OutOfBoundsException;
 use \SplFileInfo;
 use \UnexpectedValueException;
 
@@ -46,7 +45,7 @@ use \UnexpectedValueException;
 abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        // Load traits
        use StackableTrait;
-       use IndexableStackTrait;
+       use IndexableTrait;
 
        /**
         * Separator for header data
@@ -501,6 +500,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
         * @param       $data                   Data to be written
         * @param       $flushHeader    Whether to flush the header (default: flush)
         * @return      void
+        * @throws      OutOfBoundsException    If the position is not seekable
         * @throws      InvalidArgumentException        If a parameter is invalid
         */
        public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
@@ -508,7 +508,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                /* 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));
+                       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');
@@ -548,6 +548,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
         * @param       $seekPosition   Seek position in file
         * @param       $dataStream             Data to be written
         * @return      mixed                   Number of writes bytes or false on error
+        * @throws      OutOfBoundsException    If the position is not seekable
         * @throws      InvalidArgumentException        If a parameter is not valid
         */
        public function writeAtPosition (int $seekPosition, string $dataStream) {
@@ -555,7 +556,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
                if ($seekPosition < 0) {
                        // Invalid seek position
-                       throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+                       throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition));
                } elseif (empty($dataStream)) {
                        // Empty dataStream
                        throw new InvalidArgumentException('Parameter "dataStream" is empty');
@@ -681,7 +682,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
 
                // Calculate minimum length for one entry and get file size
-               $minimumBlockLength = $this->getIndexableStackInstance()->calculateMinimumBlockLength();
+               $minimumBlockLength = $this->getIndexInstance()->calculateMinimumBlockLength();
                $fileSize = $this->getFileSize();
 
                // Calulcate seek position
@@ -733,14 +734,14 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
         * @param       $offset         Offset to seek to (or used as "base" for other seeks)
         * @param       $whence         Added to offset (default: only use offset to seek to)
         * @return      $status         Status of file seek: 0 = success, -1 = failed
-        * @throws      InvalidArgumentException         If a parameter is not valid
+        * @throws      OutOfBoundsException    If the position is not seekable
         */
        public function seek (int $offset, int $whence = SEEK_SET) {
                // Validate parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: offset=%d,whence=%d - CALLED!', $offset, $whence));
                if ($offset < 0) {
                        // No offset is smaller than zero
-                       throw new InvalidArgumentException(sprintf('offset=%d is not valid', $offset));
+                       throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
                }
 
                // Call pointer instance
@@ -756,14 +757,14 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
         *
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
-        * @throws      InvalidArgumentException         If a parameter is not valid
+        * @throws      OutOfBoundsException    If the position is not seekable
         */
        public function read (int $bytes = 0) {
                // Validate parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: bytes=%d - CALLED!', $bytes));
                if ($bytes < 0) {
-                       // Throw IAE
-                       throw new InvalidArgumentException(sprintf('bytes=%d is not valid', $bytes));
+                       // Throw exception
+                       throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
                }
 
                // Call pointer instance
@@ -888,7 +889,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
 
                // First calculate minimum block length
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d', $this->determineSeekPosition()));
-               $length = $this->getIndexableStackInstance()->calculateMinimumBlockLength();
+               $length = $this->getIndexInstance()->calculateMinimumBlockLength();
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
 
                // Read possibly back-buffered bytes from previous call of next().
@@ -966,7 +967,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        public function isValid () {
                // First calculate minimum block length
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               $length = $this->getIndexableStackInstance()->calculateMinimumBlockLength();
+               $length = $this->getIndexInstance()->calculateMinimumBlockLength();
 
                // Short be more than zero!
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
@@ -1014,17 +1015,17 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        public function readFileHeader () {
                // Is index set or stack?
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               if ($this->getIndexableStackInstance() instanceof IndexableStack) {
+               if ($this->getIndexInstance() instanceof Indexable) {
                        // Call index instance
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexableStackInstance->readFileHeader() ...');
-                       $this->getIndexableStackInstance()->readFileHeader();
+                       /* 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->readFileHeader() ...');
-                       $this->getStackInstance()->readFileHeader();
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->stackInstance->readStackHeader() ...');
+                       $this->getStackInstance()->readStackHeader();
                } else {
                        // Bad logic?
-                       throw new LogicException('Wether indexableStackInstance nor stackableFileInstance are set');
+                       throw new LogicException('Wether indexInstance nor stackInstance are set');
                }
 
                // Trace message
@@ -1039,7 +1040,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
        public function flushFileHeader () {
                // Call block instance
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               $this->getIndexableStackInstance()->flushFileHeader();
+               $this->getIndexInstance()->flushFileHeader();
 
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');