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;
21 * A class for reading files
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
44 * Protected constructor
48 private function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
54 * Create a file pointer based on the given file. The file will also
57 * @param $fileInstance An instance of a SplFileInfo class
58 * @throws FileIoException If the file is not reachable
59 * @throws FileReadProtectedException If the file is not found or cannot be read
60 * @throws FileNotFoundException If the file does not exist
63 public static final function createFrameworkRawFileInputPointer (SplFileInfo $fileInstance) {
64 // Some pre-sanity checks...
65 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
66 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
67 // File cannot be accessed (due to open_basedir restriction)
68 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
69 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
70 // File does not exist
71 throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND);
72 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
73 // File exists but cannot be read from
74 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
77 // Try to open a handler
78 $fileObject = $fileInstance->openFile('rb');
81 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
82 if (!($fileObject instanceof SplFileObject)) {
83 // Something bad happend
84 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
87 // Create new instance
88 $pointerInstance = new FrameworkRawFileInputPointer();
90 // Set file pointer and file name
91 $pointerInstance->setFileObject($fileObject);
93 // Return the instance
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
95 return $pointerInstance;
99 * Read data a file pointer
101 * @return mixed The result of fread()
102 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
103 * @throws LogicException If there is no object being set
105 public function readFromFile () {
106 if (is_null($this->getFileObject())) {
107 // Pointer not initialized
108 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
109 } elseif (!is_object($this->getFileObject())) {
110 // Pointer is not a valid resource!
111 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
114 // Read data from the file pointer and return it
115 return $this->read(1024);
119 * Reads a line, maximum 4096 Bytes from current file pointer
121 * @return $data Read data from file
122 * @throws UnsupportedOperationException If this method is called
124 public function readLine () {
125 // Not supported in binary files ...
126 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
130 * Reads given amount of bytes from file.
132 * @param $bytes Amount of bytes to read
133 * @return $data Data read from file
135 public function read (int $bytes = 0) {
136 // Try to read given characters
137 $data = $this->getFileObject()->fread($bytes);
144 * Analyzes entries in index file. This will count all found (and valid)
145 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
146 * only gaps are found, the file is considered as "virgin" (no entries).
149 * @throws UnsupportedOperationException If this method is called
151 public function analyzeFileStructure () {
152 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
156 * Advances to next "block" of bytes
159 * @throws UnsupportedOperationException If this method is called
161 public function next () {
162 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
166 * Checks wether the current entry is valid (not at the end of the file).
167 * This method will return true if an emptied (nulled) entry has been found.
169 * @return $isValid Whether the next entry is valid
170 * @throws UnsupportedOperationException If this method is called
172 public function valid () {
173 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
177 * Gets current seek position ("key").
179 * @return $key Current key in iteration
180 * @throws UnsupportedOperationException If this method is called
182 public function key () {
183 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);