]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php
Continued:
[core.git] / framework / main / classes / file_directories / io / class_FrameworkFileInputOutputPointer.php
index 2805546ff35208997b53fef82e19eeab0b482ff7..2a03a01110fb7f2ccf87351440f913d11f966416 100644 (file)
@@ -4,10 +4,12 @@ namespace Org\Mxchange\CoreFramework\Filesystem\Pointer;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
-use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
-use Org\Mxchange\CoreFramework\FileSystem\FileReadProtectedException;
-use Org\Mxchange\CoreFramework\FileSystem\FileWriteProtectedException;
+use Org\Mxchange\CoreFramework\Filesystem\BaseFileIo;
+use Org\Mxchange\CoreFramework\Filesystem\FileIoException;
+use Org\Mxchange\CoreFramework\Filesystem\FileReadProtectedException;
+use Org\Mxchange\CoreFramework\Filesystem\FileWriteProtectedException;
 use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
@@ -15,6 +17,8 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 // Import SPL stuff
 use \InvalidArgumentException;
 use \SplFileInfo;
+use \SplFileObject;
+use \OutOfBoundsException;
 use \UnexpectedValueException;
 
 /**
@@ -22,7 +26,7 @@ use \UnexpectedValueException;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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.shipsimu.org
  *
@@ -45,7 +49,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         *
         * @return      void
         */
-       protected function __construct () {
+       private function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
        }
@@ -63,6 +67,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         */
        public static final function createFrameworkFileInputOutputPointer (SplFileInfo $fileInstance) {
                // Some pre-sanity checks...
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance));
                if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
                        // File exists but cannot be read
                        throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
@@ -81,7 +86,8 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $fileObject = $fileInstance->openFile('c+b');
 
                // Is it valid?
-               if ((is_null($fileObject)) || ($fileObject === false)) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
+               if (!($fileObject instanceof SplFileObject)) {
                        // Something bad happend
                        throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID);
                }
@@ -93,6 +99,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $pointerInstance->setFileObject($fileObject);
 
                // Return the instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
                return $pointerInstance;
        }
 
@@ -103,8 +110,12 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         */
        public function readFromFile () {
                // Read data from the file pointer and return it
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
-               return $this->read(1024);
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!');
+               $data = $this->read(1024);
+
+               // Return data
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%d - EXIT!', gettype($data), $data));
+               return $data;
        }
 
        /**
@@ -115,17 +126,21 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         */
        public function writeToFile (string $dataStream) {
                // Validate parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: dataStream(%d)=%s - CALLED!', strlen($dataStream), $dataStream));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: dataStream(%d)=%s - CALLED!', strlen($dataStream), $dataStream));
                if (empty($dataStream)) {
                        // Empty dataStream
-                       throw new InvalidArgumentException('Parameter "dataStream" is empty');
+                       throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
+               // Get length
+               $length = strlen($dataStream);
+
                // Write data to the file pointer and return written bytes
-               $status = $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: Invoking this->fileObject->fwrite(%s,%d) ...', $dataStream, $length));
+               $status = $this->getFileObject()->fwrite($dataStream, $length);
 
                // Return status
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
                return $status;
        }
 
@@ -135,30 +150,29 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @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) {
                // Validate parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: 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');
-               }
-
-               // First seek to it, if file size is larger than zero
-               if (($this->getFileSize() > 0 || $seekPosition > 0) && $this->seek($seekPosition) === -1) {
+                       throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif (($this->getFileSize() > 0 || $seekPosition > 0) && $this->seek($seekPosition) === -1) {
                        // Could not seek
                        throw new InvalidArgumentException(sprintf('Could not seek to seekPosition=%d', $seekPosition));
                }
 
                // Then write the data at that position
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: Invoking this->writeToFile(%s) ...', $dataStream));
                $status = $this->writeToFile($dataStream);
 
                // Return status
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
                return $status;
        }
 
@@ -168,12 +182,12 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @return      void
         */
        public function rewind () {
-               // Rewind the pointer
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
+               /// Rewind the pointer
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!');
                $this->getFileObject()->rewind();
 
                // Trace message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: EXIT!');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: EXIT!');
        }
 
        /**
@@ -182,21 +196,24 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @param       $seekPosition   Seek position in file
         * @param       $whence                 "Seek mode" (see http://de.php.net/fseek)
         * @return      $status                 Status of this operation
-        * @throws      InvalidArgumentException        If a parameter is not valid
+        * @throws      OutOfBoundsException    If the position is not seekable
         */
        public function seek (int $seekPosition, int $whence = SEEK_SET) {
                // Validate parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
                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 ($whence < 0) {
+                       // Invalid seek position
+                       throw new OutOfBoundsException(sprintf('whence=%d is not valid.', $whence));
                }
 
                // Move the file pointer
                $status = $this->getFileObject()->fseek($seekPosition, $whence);
 
                // Return status
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
                return $status;
        }
 
@@ -207,8 +224,12 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         */
        public function readLine () {
                // Read whole line
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
-               return $this->read();
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!');
+               $data = $this->read();
+
+               // Return data
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
+               return $data;
        }
 
        /**
@@ -216,17 +237,17 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         *
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
-        * @throws      InvalidArgumentException        If a parameter is invalid
+        * @throws      OutOfBoundsException    If the position is not seekable
         */
        public function read (int $bytes = 0) {
                // Validatre parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes));
                if ($bytes < 0) {
                        // Bytes cannot be lesser than zero
-                       throw new InvalidArgumentException(sprintf('bytes=%d is not valid', $bytes));
+                       throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
                }
 
-               // Is $bytes set?
+               // Is $bytes bigger than zero?
                if ($bytes > 0) {
                        // Try to read given characters
                        $data = $this->getFileObject()->fread($bytes);
@@ -236,7 +257,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                }
 
                // Then return it
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
                return $data;
        }
 
@@ -248,8 +269,8 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @return      void
         * @throws      UnsupportedOperationException   If this method is called
         */
-       public function analyzeFile () {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       public function analyzeFileStructure () {
+               throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**
@@ -259,7 +280,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @throws      UnsupportedOperationException   If this method is called
         */
        public function next () {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**
@@ -270,7 +291,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @throws      UnsupportedOperationException   If this method is called
         */
        public function valid () {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**
@@ -280,7 +301,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @throws      UnsupportedOperationException   If this method is called
         */
        public function key () {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**
@@ -291,17 +312,17 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         */
        public function getFileSize () {
                // Get file's data
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!');
                $fileData = $this->getFileObject()->fstat();
 
                // Make sure the required array key is there
                if (!isset($fileData['size'])) {
                        // Not valid array
-                       throw new UnexpectedValueException(sprintf('fileData=%s has no element "size"', print_r($fileData, TRUE)));
+                       throw new UnexpectedValueException(sprintf('fileData=%s has no element "size"', print_r($fileData, TRUE)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
                }
 
                // Return size
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: fileData[size]=%d - EXIT!', $fileData['size']));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileData[size]=%d - EXIT!', $fileData['size']));
                return $fileData['size'];
        }