]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/iterator/io/class_FileIoIterator.php
Introduced CalculatableBlock + basic implementation for valid().
[core.git] / inc / classes / main / iterator / io / class_FileIoIterator.php
index cfd1297bbf1e920ae603e8611d88eaedc903f79b..12cf82ea461e8f32fc5e3f32e4b11b6627dbf57a 100644 (file)
@@ -35,16 +35,20 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
        /**
         * Creates an instance of this class
         *
-        * @param       $pointerInstance        An instance of a FrameworkFileInputOutputPointer class
+        * @param       $pointerInstance        An instance of a InputOutputPointer class
+        * @param       $blockInstance          An instance of a CalculatableBlock class
         * @return      $iteratorInstance       An instance of a Iterator class
         */
-       public final static function createFileIoIterator (FrameworkFileInputOutputPointer $pointerInstance) {
+       public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
                // Get new instance
                $iteratorInstance = new FileIoIterator();
 
                // Set the instance here
                $iteratorInstance->setPointerInstance($pointerInstance);
 
+               // Set the block instance here
+               $iteratorInstance->setBlockInstance($blockInstance);
+
                // Return the prepared instance
                return $iteratorInstance;
        }
@@ -70,13 +74,8 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @return      $key    Current key in iteration
         */
        public function key () {
-               // Default is null
-               $key = null;
-
-               $this->partialStub('Please implement this method.');
-
                // Return it
-               return $key;
+               return $this->getPointerInstance()->determineSeekPosition();
        }
 
        /**
@@ -91,30 +90,90 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
        /**
         * Rewinds to the beginning of the file
         *
-        * @return      void
+        * @return      $status         Status of this operation
         */
        public function rewind () {
-               $this->partialStub('Please implement this method.');
+               // Call pointer instance
+               return $this->getPointerInstance()->rewind();
+       }
+
+       /**
+        * Checks wether the current entry is valid (not at the end of the file).
+        * This method will return TRUE if an emptied (nulled) entry has been found.
+        *
+        * @return      $isValid        Whether the next entry is valid
+        */
+       public function valid () {
+               // First calculate minimum block length
+               $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
+
+               // Short be more than zero!
+               assert($length > 0);
+
+               // Get current seek position
+               $seekPosition = $this->key();
+
+               // Then try to read it
+               $data = $this->read($length);
+
+               // If some bytes could be read, all is fine
+               $isValid = ((is_string($data)) && (strlen($data) > 0));
+
+               // Seek back to old position
+               $this->seek($seekPosition);
+
+               // Return result
+               return $isValid;
        }
 
        /**
         * Seeks to given position
         *
         * @param       $seekPosition   Seek position in file
-        * @return      void
+        * @return      $status                 Status of this operation
         */
-       public function seek ($seedPosition) {
-               $this->partialStub('Please implement this method. seekPosition=' . $seekPosition);
+       public function seek ($seekPosition) {
+               // Call pointer instance
+               return $this->getPointerInstance()->seek($seekPosition);
        }
 
        /**
-        * Checks wether the current entry is valid (not at the end of the file).
-        * This method will return TRUE if an emptied (nulled) entry has been found.
+        * Writes at given position by seeking to it.
         *
+        * @param       $seekPosition   Seek position in file
+        * @param       $data                   Data to be written
         * @return      void
         */
-       public function valid () {
-               $this->partialStub('Please implement this method.');
+       public function writeAtPosition ($seekPosition, $data) {
+               // First seek to it
+               $this->seek($seekPosition);
+
+               // Then write the data at that position
+               $this->getPointerInstance()->writeToFile($data);
+       }
+
+       /**
+        * Size of file stack
+        *
+        * @return      $size   Size (in bytes) of file
+        */
+       public function size () {
+               // Call the pointer object
+               $size = $this->getPointerInstance()->size();
+
+               // Return result
+               return $size;
+       }
+
+       /**
+        * Reads given amount of bytes from file.
+        *
+        * @param       $bytes  Amount of bytes to read
+        * @return      $data   Data read from file
+        */
+       public function read ($bytes) {
+               // Call pointer instance
+               return $this->getPointerInstance()->read($bytes);
        }
 }