]> 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 35beea36dcb71f80d222b8f2d8558dd4735e679c..440c7f86329c3924c4b92b64bf4ab5430f93d90f 100644 (file)
@@ -8,10 +8,15 @@ 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\Stack\File\StackableFile;
+use Org\Mxchange\CoreFramework\Index\Stack\IndexableStack;
+use Org\Mxchange\CoreFramework\Traits\Index\Stack\IndexableStackTrait;
+use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
 
 // Import SPL stuff
 use \BadMethodCallException;
 use \InvalidArgumentException;
+use \LogicException;
 use \SplFileInfo;
 use \UnexpectedValueException;
 
@@ -37,7 +42,11 @@ use \UnexpectedValueException;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-abstract class BaseBinaryFile extends BaseAbstractFile {
+abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
+       // Load traits
+       use StackableTrait;
+       use IndexableStackTrait;
+
        /**
         * Separator for header data
         */
@@ -135,11 +144,6 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         */
        private $currentBlock = '';
 
-       /**
-        * An instance of a Block class
-        */
-       private $blockInstance = NULL;
-
        /**
         * Protected constructor
         *
@@ -154,25 +158,6 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                $this->initCountersGapsArray();
        }
 
-       /**
-        * Setter for Block instance
-        *
-        * @param       $blockInstance  An instance of an Block class
-        * @return      void
-        */
-       protected final function setBlockInstance (Block $blockInstance) {
-               $this->blockInstance = $blockInstance;
-       }
-
-       /**
-        * Getter for Block instance
-        *
-        * @return      $blockInstance  An instance of an Block class
-        */
-       public final function getBlockInstance () {
-               return $this->blockInstance;
-       }
-
        /**
         * Setter for backBuffer field
         *
@@ -695,7 +680,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
 
                // Calculate minimum length for one entry and get file size
-               $minimumBlockLength = $this->getBlockInstance()->calculateMinimumBlockLength();
+               $minimumBlockLength = $this->getIndexableStackInstance()->calculateMinimumBlockLength();
                $fileSize = $this->getFileSize();
 
                // Calulcate seek position
@@ -902,7 +887,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
 
                // First calculate minimum block length
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d', $this->determineSeekPosition()));
-               $length = $this->getBlockInstance()->calculateMinimumBlockLength();
+               $length = $this->getIndexableStackInstance()->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().
@@ -980,7 +965,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
        public function isValid () {
                // First calculate minimum block length
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               $length = $this->getBlockInstance()->calculateMinimumBlockLength();
+               $length = $this->getIndexableStackInstance()->calculateMinimumBlockLength();
 
                // Short be more than zero!
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
@@ -1023,11 +1008,23 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         * Reads the file header
         *
         * @return      void
+        * @throws      LogicException  If both instances are not set
         */
        public function readFileHeader () {
-               // Call block instance
+               // Is index set or stack?
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               $this->getBlockInstance()->readFileHeader();
+               if ($this->getIndexableStackInstance() instanceof IndexableStack) {
+                       // Call index instance
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexableStackInstance->readFileHeader() ...');
+                       $this->getIndexableStackInstance()->readFileHeader();
+               } 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();
+               } else {
+                       // Bad logic?
+                       throw new LogicException('Wether indexableStackInstance nor stackableFileInstance are set');
+               }
 
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
@@ -1041,7 +1038,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
        public function flushFileHeader () {
                // Call block instance
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
-               $this->getBlockInstance()->flushFileHeader();
+               $this->getIndexableStackInstance()->flushFileHeader();
 
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');