Some updates:
[core.git] / framework / main / classes / file_directories / input / raw / class_FrameworkRawFileInputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Input;
4
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;
14
15 // Import SPL stuff
16 use \SplFileInfo;
17
18 /**
19  * A class for reading files
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
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
25 =======
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
30  *
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.
35  *
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.
40  *
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/>.
43  */
44 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Create a file pointer based on the given file. The file will also
57          * be verified here.
58          *
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
63          * @return      void
64          */
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);
76                 }
77
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);
83                 } // END - if
84
85                 // Create new instance
86                 $pointerInstance = new FrameworkRawFileInputPointer();
87
88                 // Set file pointer and file name
89                 $pointerInstance->setFileObject($fileObject);
90
91                 // Return the instance
92                 return $pointerInstance;
93         }
94
95         /**
96          * Read data a file pointer
97          *
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
101          */
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())));
109                 }
110
111                 // Read data from the file pointer and return it
112                 return $this->read(1024);
113         }
114
115         /**
116          * Reads a line, maximum 4096 Bytes from current file pointer
117          *
118          * @return      $data   Read data from file
119          * @throws      UnsupportedOperationException   If this method is called
120          */
121         public function readLine () {
122                 // Not supported in binary files ...
123                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
124         }
125
126         /**
127          * Reads given amount of bytes from file.
128          *
129          * @param       $bytes  Amount of bytes to read
130          * @return      $data   Data read from file
131          */
132         public function read ($bytes = NULL) {
133                 // $bytes shall be integer
134                 assert(is_int($bytes));
135
136                 // Try to read given characters
137                 $data = $this->getFileObject()->fread($bytes);
138
139                 // Then return it
140                 return $data;
141         }
142
143         /**
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).
147          *
148          * @return      void
149          * @throws      UnsupportedOperationException   If this method is called
150          */
151         public function analyzeFile () {
152                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
153         }
154
155         /**
156          * Advances to next "block" of bytes
157          *
158          * @return      void
159          * @throws      UnsupportedOperationException   If this method is called
160          */
161         public function next () {
162                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
163         }
164
165         /**
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.
168          *
169          * @return      $isValid        Whether the next entry is valid
170          * @throws      UnsupportedOperationException   If this method is called
171          */
172         public function valid () {
173                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
174         }
175
176         /**
177          * Gets current seek position ("key").
178          *
179          * @return      $key    Current key in iteration
180          * @throws      UnsupportedOperationException   If this method is called
181          */
182         public function key () {
183                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
184         }
185
186 }