3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Input;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\BaseFileIo;
8 use Org\Mxchange\CoreFramework\Filesystem\FileIoException;
9 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
10 use Org\Mxchange\CoreFramework\Filesystem\FileReadProtectedException;
11 use Org\Mxchange\CoreFramework\Filesystem\Pointer\InputPointer;
12 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
13 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
14 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
17 use \InvalidArgumentException;
20 use \UnexpectedValueException;
23 * A class for reading files
25 * @author Roland Haeder <webmaster@shipsimu.org>
27 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
28 * @license GNU GPL 3.0 or any newer version
29 * @link http://www.shipsimu.org
31 * This program is free software: you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation, either version 3 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
46 * Protected constructor
50 private function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Create a file pointer based on the given file. The file will also
59 * @param $fileInstance An instance of a SplFileInfo class
60 * @throws FileIoException If the file is not reachable
61 * @throws FileReadProtectedException If the file is not found or cannot be read
62 * @throws FileNotFoundException If the file does not exist
65 public static final function createFrameworkRawFileInputPointer (SplFileInfo $fileInstance) {
66 // Some pre-sanity checks...
67 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
68 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
69 // File cannot be accessed (due to open_basedir restriction)
70 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
71 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
72 // File does not exist
73 throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND);
74 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
75 // File exists but cannot be read from
76 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
79 // Try to open a handler
80 $fileObject = $fileInstance->openFile('rb');
83 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
84 if (!($fileObject instanceof SplFileObject)) {
85 // Something bad happend
86 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
89 // Create new instance
90 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject.pathname=%s', $fileObject->getPathname()));
91 $pointerInstance = new FrameworkRawFileInputPointer();
93 // Set file pointer and file name
94 $pointerInstance->setFileObject($fileObject);
96 // Return the instance
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
98 return $pointerInstance;
102 * Read data a file pointer
104 * @return mixed The result of fread()
105 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
106 * @throws LogicException If there is no object being set
108 public function readFromFile () {
110 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: CALLED!');
111 if (is_null($this->getFileObject())) {
112 // Pointer not initialized
113 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
114 } elseif (!is_object($this->getFileObject())) {
115 // Pointer is not a valid resource!
116 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
119 // Read data from the file pointer and return it
120 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: Invoking this->read(1024) - EXIT!');
121 return $this->read(1024);
125 * Reads a line, maximum 4096 Bytes from current file pointer
127 * @return $data Read data from file
128 * @throws UnsupportedOperationException If this method is called
130 public function readLine () {
131 // Not supported in binary files ...
132 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
136 * Reads given amount of bytes from file.
138 * @param $bytes Amount of bytes to read
139 * @return $data Data read from file
140 * @throws InvalidArgumentException If a parameter is not valid
141 * @throws UnexpectedValueException If fread() returns a non-string value
143 public function read (int $bytes = 0) {
145 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: bytes=%d - CALLED!', $bytes));
148 throw new InvalidArgumentException(sprintf('bytes=%d is an invalid value, only zero or postive numbers are accepted', $bytes));
151 // Try to read given characters
152 $data = $this->getFileObject()->fread($bytes);
155 if (!is_string($data)) {
157 throw new UnexpectedValueException(sprintf('Returned data[]=%s is not expected.', gettype($data)));
161 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: data()=%d - EXIT!', strlen($data)));
166 * Analyzes entries in index file. This will count all found (and valid)
167 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
168 * only gaps are found, the file is considered as "virgin" (no entries).
171 * @throws UnsupportedOperationException If this method is called
173 public function analyzeFileStructure () {
174 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
178 * Advances to next "block" of bytes
181 * @throws UnsupportedOperationException If this method is called
183 public function next () {
184 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
188 * Checks wether the current entry is valid (not at the end of the file).
189 * This method will return true if an emptied (nulled) entry has been found.
191 * @return $isValid Whether the next entry is valid
192 * @throws UnsupportedOperationException If this method is called
194 public function valid () {
195 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
199 * Gets current seek position ("key").
201 * @return $key Current key in iteration
202 * @throws UnsupportedOperationException If this method is called
204 public function key () {
205 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);