use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
// Import SPL stuff
+use \InvalidArgumentException;
use \SplFileInfo;
+use \UnexpectedValueException;
/**
* A class for reading files
if ((is_null($fileObject)) || ($fileObject === false)) {
// Something bad happend
throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID);
- } // END - if
+ }
// Create new instance
$pointerInstance = new FrameworkFileInputOutputPointer();
return $pointerInstance;
}
- /**
- * 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 void
- * @throws NullPointerException If the file pointer instance
- * is not set by setFileObject()
- * @todo Add more checks
- */
- private function validateFilePointer () {
- if (is_null($this->getFileObject())) {
- // Pointer not initialized
- throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
- } // END - if
-
- // 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
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
return $this->read(1024);
}
* @return mixed Number of writes bytes or false on error
*/
public function writeToFile (string $dataStream) {
- // Validate the pointer
- $this->validateFilePointer();
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: dataStream(%d)=%s - CALLED!', strlen($dataStream), $dataStream));
+ if (empty($dataStream)) {
+ // Empty dataStream
+ throw new InvalidArgumentException('Parameter "dataStream" is empty');
+ }
// Write data to the file pointer and return written bytes
- return $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
+ $status = $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status));
+ return $status;
}
/**
* Writes at given position by seeking to it.
*
* @param $seekPosition Seek position in file
- * @param $data Data to be written
+ * @param $dataStream Data to be written
* @return mixed Number of writes bytes or false on error
+ * @throws InvalidArgumentException If a parameter is not valid
*/
- public function writeAtPosition (int $seekPosition, string $data) {
+ public function writeAtPosition (int $seekPosition, string $dataStream) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,dataStream()=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+ if ($seekPosition < 0) {
+ // Invalid seek position
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+ } elseif (empty($dataStream)) {
+ // Empty dataStream
+ throw new InvalidArgumentException('Parameter "dataStream" is empty');
+ }
+
// First seek to it
- $this->seek($seekPosition);
+ if (!$this->seek($seekPosition)) {
+ // Could not seek
+ throw new InvalidArgumentException(sprintf('Could not seek to seekPosition=%d', $seekPosition));
+ }
// Then write the data at that position
- return $this->writeToFile($data);
+ $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));
+ return $status;
}
/**
* @return $status Status of this operation
*/
public function rewind () {
- // Validate the pointer
- $this->validateFilePointer();
-
// Rewind the pointer
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
return $this->getFileObject()->rewind();
}
* @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
*/
- public function seek ($seekPosition, $whence = SEEK_SET) {
- // Validate the pointer
- $this->validateFilePointer();
+ 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));
+ if ($seekPosition < 0) {
+ // Invalid seek position
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+ }
// Move the file pointer
- return $this->getFileObject()->fseek($seekPosition, $whence);
+ $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));
+ return $status;
}
/**
*/
public function readLine () {
// Read whole line
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
return $this->read();
}
*
* @param $bytes Amount of bytes to read
* @return $data Data read from file
+ * @throws InvalidArgumentException If a parameter is invalid
*/
- public function read (int $bytes = NULL) {
- // Validate the pointer
- $this->validateFilePointer();
+ public function read (int $bytes = 0) {
+ // Validatre parameter
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(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));
+ }
// Is $bytes set?
- if (is_int($bytes)) {
+ if ($bytes > 0) {
// Try to read given characters
$data = $this->getFileObject()->fread($bytes);
} else {
}
// Then return it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
return $data;
}
* "Getter" for file size
*
* @return $fileSize Size of currently loaded file
+ * @throws UnexpectedValueException If $fileData does not contain "size"
*/
public function getFileSize () {
- // Check if the pointer is still valid
- $this->validateFilePointer();
-
// Get file's data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
$fileData = $this->getFileObject()->fstat();
// Make sure the required array key is there
- assert(isset($fileData['size']));
+ if (!isset($fileData['size'])) {
+ // Not valid array
+ throw new UnexpectedValueException(sprintf('fileData=%s has no element "size"', print_r($fileData, TRUE)));
+ }
// Return size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: fileData[size]=%d - EXIT!', $fileData['size']);
return $fileData['size'];
}