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\FileNotFoundException;
9 use Org\Mxchange\CoreFramework\FileSystem\FileReadProtectedException;
10 use Org\Mxchange\CoreFramework\Filesystem\Pointer\InputPointer;
11 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
12 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
13 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
19 * A class for reading files
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 <<<<<<< HEAD:framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
27 >>>>>>> Some updates::inc/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php
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 protected 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 $infoInstance 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 $infoInstance) {
66 // Some pre-sanity checks...
67 if (!FrameworkBootstrap::isReachableFilePath($infoInstance)) {
68 // File cannot be accessed (due to open_basedir restriction)
69 throw new FileIoException($infoInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
70 } elseif ((!FrameworkBootstrap::isReadableFile($infoInstance)) && (!$infoInstance->isFile())) {
71 // File does not exist
72 throw new FileNotFoundException($infoInstance, self::EXCEPTION_FILE_NOT_FOUND);
73 } elseif ((!FrameworkBootstrap::isReadableFile($infoInstance)) && ($infoInstance->isFile())) {
74 // File exists but cannot be read from
75 throw new FileReadProtectedException($infoInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
78 // Try to open a handler
79 $fileObject = $infoInstance->openFile('rb');
80 if ((is_null($fileObject)) || ($fileObject === false)) {
81 // Something bad happend
82 throw new FileIoException($infoInstance, self::EXCEPTION_FILE_POINTER_INVALID);
85 // Create new instance
86 $pointerInstance = new FrameworkRawFileInputPointer();
88 // Set file pointer and file name
89 $pointerInstance->setFileObject($fileObject);
91 // Return the instance
92 return $pointerInstance;
96 * Read data a file pointer
98 * @return mixed The result of fread()
99 * @throws NullPointerException If the file pointer instance is not set by setPointer()
100 * @throws LogicException If there is no object being set
102 public function readFromFile () {
103 if (is_null($this->getFileObject())) {
104 // Pointer not initialized
105 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
106 } elseif (!is_object($this->getFileObject())) {
107 // Pointer is not a valid resource!
108 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
111 // Read data from the file pointer and return it
112 return $this->read(1024);
116 * Reads a line, maximum 4096 Bytes from current file pointer
118 * @return $data Read data from file
119 * @throws UnsupportedOperationException If this method is called
121 public function readLine () {
122 // Not supported in binary files ...
123 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
127 * Reads given amount of bytes from file.
129 * @param $bytes Amount of bytes to read
130 * @return $data Data read from file
132 public function read ($bytes = NULL) {
133 // $bytes shall be integer
134 assert(is_int($bytes));
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 analyzeFile () {
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);