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\FrameworkInterface;
13 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
14 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
15 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
18 use \InvalidArgumentException;
21 use \UnexpectedValueException;
24 * A class for reading files
26 * @author Roland Haeder <webmaster@shipsimu.org>
28 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
29 * @license GNU GPL 3.0 or any newer version
30 * @link http://www.shipsimu.org
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
47 * Protected constructor
51 private function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
57 * Create a file pointer based on the given file. The file will also
60 * @param $fileInstance An instance of a SplFileInfo class
61 * @throws FileIoException If the file is not reachable
62 * @throws FileReadProtectedException If the file is not found or cannot be read
63 * @throws FileNotFoundException If the file does not exist
66 public static final function createFrameworkRawFileInputPointer (SplFileInfo $fileInstance) {
67 // Some pre-sanity checks...
68 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
69 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
70 // File cannot be accessed (due to open_basedir restriction)
71 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
72 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
73 // File does not exist
74 throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND);
75 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
76 // File exists but cannot be read from
77 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
80 // Try to open a handler
81 $fileObject = $fileInstance->openFile('rb');
84 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
85 if (!($fileObject instanceof SplFileObject)) {
86 // Something bad happend
87 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
90 // Create new instance
91 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject.pathname=%s', $fileObject->getPathname()));
92 $pointerInstance = new FrameworkRawFileInputPointer();
94 // Set file pointer and file name
95 $pointerInstance->setFileObject($fileObject);
97 // Return the instance
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
99 return $pointerInstance;
103 * Read data a file pointer
105 * @return mixed The result of fread()
106 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
107 * @throws LogicException If there is no object being set
109 public function readFromFile () {
111 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: CALLED!');
112 if (is_null($this->getFileObject())) {
113 // Pointer not initialized
114 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
115 } elseif (!is_object($this->getFileObject())) {
116 // Pointer is not a valid resource!
117 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
120 // Read data from the file pointer and return it
121 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: Invoking this->read(1024) - EXIT!');
122 return $this->read(1024);
126 * Reads a line, maximum 4096 Bytes from current file pointer
128 * @return $data Read data from file
129 * @throws UnsupportedOperationException If this method is called
131 public function readLine () {
132 // Not supported in binary files ...
133 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
137 * Reads given amount of bytes from file.
139 * @param $bytes Amount of bytes to read
140 * @return $data Data read from file
141 * @throws InvalidArgumentException If a parameter is not valid
142 * @throws UnexpectedValueException If fread() returns a non-string value
144 public function read (int $bytes = 0) {
146 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: bytes=%d - CALLED!', $bytes));
149 throw new InvalidArgumentException(sprintf('bytes=%d is an invalid value, only zero or postive numbers are accepted', $bytes));
152 // Try to read given characters
153 $data = $this->getFileObject()->fread($bytes);
156 if (!is_string($data)) {
158 throw new UnexpectedValueException(sprintf('Returned data[]=%s is not expected.', gettype($data)));
162 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: data()=%d - EXIT!', strlen($data)));
167 * Analyzes entries in index file. This will count all found (and valid)
168 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
169 * only gaps are found, the file is considered as "virgin" (no entries).
172 * @throws UnsupportedOperationException If this method is called
174 public function analyzeFileStructure () {
175 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
179 * Advances to next "block" of bytes
182 * @throws UnsupportedOperationException If this method is called
184 public function next () {
185 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
189 * Checks wether the current entry is valid (not at the end of the file).
190 * This method will return true if an emptied (nulled) entry has been found.
192 * @return $isValid Whether the next entry is valid
193 * @throws UnsupportedOperationException If this method is called
195 public function valid () {
196 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
200 * Gets current seek position ("key").
202 * @return $key Current key in iteration
203 * @throws UnsupportedOperationException If this method is called
205 public function key () {
206 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);