X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ffile_directories%2Fio%2Fclass_FrameworkFileInputOutputPointer.php;h=5d23c7bf25bce648c9904f4bb88e7ef0834a2a03;hp=cebb1fad9229640ca38b27f845fe6a8f9cbb6a03;hb=6b19898fa5c1cc332e83100ea41f55073ec20a8a;hpb=79968ab0f673b50f09f35aed9bdc8112558e1553 diff --git a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php index cebb1fad..5d23c7bf 100644 --- a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -57,7 +57,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP } // Try to open a handler - $filePointer = fopen($fileName, 'a+b'); + $filePointer = fopen($fileName, 'c+b'); if ((is_null($filePointer)) || ($filePointer === FALSE)) { // Something bad happend throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID); @@ -75,14 +75,17 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP } /** - * Read data a file pointer + * Validates file pointer and throws exceptions. This method does not return + * anything (not reliable) as this method checks the file pointer and on + * case of an error it throws an exception. If this method does not throw + * any exceptions, the file pointer seems to be fine. * - * @return mixed The result of fread() + * @return void * @throws NullPointerException If the file pointer instance * is not set by setPointer() * @throws InvalidResourceException If there is being set */ - public function readFromFile () { + private function validateFilePointer () { if (is_null($this->getPointer())) { // Pointer not initialized throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); @@ -91,8 +94,20 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); } + // All fine here + } + + /** + * Read 1024 bytes data from a file pointer + * + * @return mixed The result of fread() + */ + public function readFromFile () { + // Validate the pointer + $this->validateFilePointer(); + // Read data from the file pointer and return it - return fread($this->getPointer(), 1024); + return $this->read(1024); } /** @@ -100,19 +115,10 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * * @param $dataStream The data stream we shall write to the file * @return mixed Number of writes bytes or FALSE on error - * @throws NullPointerException If the file pointer instance - * is not set by setPointer() - * @throws InvalidResourceException If there is being set - * an invalid file resource */ public function writeToFile ($dataStream) { - if (is_null($this->getPointer())) { - // Pointer not initialized - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_resource($this->getPointer())) { - // Pointer is not a valid resource! - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } + // Validate the pointer + $this->validateFilePointer(); // Write data to the file pointer and return written bytes return fwrite($this->getPointer(), $dataStream, strlen($dataStream)); @@ -121,11 +127,14 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP /** * Rewinds to the beginning of the file * - * @return void + * @return $status Status of this operation */ public function rewind () { + // Validate the pointer + $this->validateFilePointer(); + // Rewind the pointer - assert(rewind($this->getPointer() === 1); + return rewind($this->getPointer()); } /** @@ -133,11 +142,34 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * * @param $seekPosition Seek position in file * @param $whence "Seek mode" (see http://de.php.net/fseek) - * @return void + * @return $status Status of this operation */ public function seek ($seekPosition, $whence = SEEK_SET) { + // Validate the pointer + $this->validateFilePointer(); + // Move the file pointer - assert(fseek($this->getPointerInstance(), $seekPosition, $whence) === 0); + return fseek($this->getPointer(), $seekPosition, $whence); + } + + /** + * Reads given amount of bytes from file. + * + * @param $bytes Amount of bytes to read + * @return $data Data read from file + */ + public function read ($bytes) { + // Validate the pointer + $this->validateFilePointer(); + + // Try to read given characters + $data = fread($this->getPointer(), $bytes); + + // Was this successfull? + assert(is_string($data)); + + // Then return it + return $data; } }