3 namespace Org\Mxchange\CoreFramework\Iterator\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Block;
7 use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
8 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
13 * @author Roland Haeder <webmaster@ship-simu.org>
15 <<<<<<< HEAD:framework/main/classes/iterator/file/class_FileIterator.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/iterator/file/class_FileIterator.php
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.ship-simu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this class
50 * @param $pointerInstance An instance of a Block class
51 * @return $iteratorInstance An instance of a Iterator class
53 public final static function createFileIterator (Block $blockInstance) {
55 $iteratorInstance = new FileIterator();
57 // Set the instance here
58 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
59 $iteratorInstance->setBlockInstance($blockInstance);
61 // Return the prepared instance
62 return $iteratorInstance;
66 * Gets currently read data
68 * @return $current Currently read data
70 public function current () {
71 // Call block instance
72 return $this->getBlockInstance()->current();
76 * Gets current seek position ("key").
78 * @return $key Current key in iteration
80 public function key () {
82 return $this->getBlockInstance()->determineSeekPosition();
86 * Advances to next "block" of bytes
90 public function next () {
91 // Call block instance
92 $this->getBlockInstance()->next();
96 * Rewinds to the beginning of the file
98 * @return $status Status of this operation
100 public function rewind () {
101 // Call block instance
102 return $this->getBlockInstance()->rewind();
106 * Checks wether the current entry is valid (not at the end of the file).
107 * This method will return true if an emptied (nulled) entry has been found.
109 * @return $isValid Whether the next entry is valid
111 public function valid () {
112 // Call block instance
113 return $this->getBlockInstance()->valid();
117 * Seeks to given position
119 * @param $seekPosition Seek position in file
120 * @return $status Status of this operation
122 public function seek ($seekPosition) {
123 // Call block instance
124 return $this->getBlockInstance()->seek($seekPosition);
130 * @return $size Size (in bytes) of file
132 public function size () {
133 // Call the block object
134 $size = $this->getBlockInstance()->size();
141 * Reads given amount of bytes from file.
143 * @param $bytes Amount of bytes to read
144 * @return $data Data read from file
146 public function read ($bytes = NULL) {
147 // Call block instance
148 return $this->getBlockInstance()->read($bytes);
152 * Analyzes entries in index file. This will count all found (and valid)
153 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
154 * only gaps are found, the file is considered as "virgin" (no entries).
158 public function analyzeFile () {
159 // Just call the block instance
160 $this->getBlockInstance()->analyzeFile();
164 * Checks whether the file header is initialized
166 * @return $isInitialized Whether the file header is initialized
168 public function isFileHeaderInitialized () {
169 // Just call the block instance
170 return $this->getBlockInstance()->isFileHeaderInitialized();
174 * Creates the assigned file
178 public function createFileHeader () {
179 // Just call the block instance
180 $this->getBlockInstance()->createFileHeader();
184 * Pre-allocates file (if enabled) with some space for later faster write access.
186 * @param $type Type of the file
189 public function preAllocateFile ($type) {
190 // Just call the block instance
191 $this->getBlockInstance()->preAllocateFile($type);
195 * Initializes counter for valid entries, arrays for damaged entries and
196 * an array for gap seek positions. If you call this method on your own,
197 * please re-analyze the file structure. So you are better to call
198 * analyzeFile() instead of this method.
202 public function initCountersGapsArray () {
203 // Call block instance
204 $this->getBlockInstance()->initCountersGapsArray();
208 * Getter for header size
210 * @return $totalEntries Size of file header
212 public final function getHeaderSize () {
213 // Call block instance
214 return $this->getBlockInstance()->getHeaderSize();
218 * Setter for header size
220 * @param $headerSize Size of file header
223 public final function setHeaderSize ($headerSize) {
224 // Call block instance
225 $this->getBlockInstance()->setHeaderSize($headerSize);
229 * Getter for header array
231 * @return $totalEntries Size of file header
233 public final function getHeader () {
234 // Call block instance
235 return $this->getBlockInstance()->getHeader();
241 * @param $header Array for a file header
244 public final function setHeader (array $header) {
245 // Call block instance
246 $this->getBlockInstance()->setHeader($header);
250 * Updates seekPosition attribute from file to avoid to much access on file.
254 public function updateSeekPosition () {
255 // Call block instance
256 $this->getBlockInstance()->updateSeekPosition();
260 * Getter for total entries
262 * @return $totalEntries Total entries in this file
264 public final function getCounter () {
265 // Call block instance
266 return $this->getBlockInstance()->getCounter();
270 * "Getter" for file size
272 * @return $fileSize Size of currently loaded file
274 public function getFileSize () {
275 // Call block instance
276 return $this->getBlockInstance()->getFileSize();
280 * Writes data at given position
282 * @param $seekPosition Seek position
283 * @param $data Data to be written
284 * @param $flushHeader Whether to flush the header (default: flush)
287 public function writeData ($seekPosition, $data, $flushHeader = true) {
288 // Call block instance
289 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
293 * Getter for seek position
295 * @return $seekPosition Current seek position (stored here in object)
297 public function getSeekPosition () {
298 // Call block instance
299 return $this->getBlockInstance()->getSeekPosition();
303 * Writes given value to the file and returns a hash and gap position for it
305 * @param $groupId Group identifier
306 * @param $value Value to be added to the stack
307 * @return $data Hash and gap position
309 public function writeValueToFile ($groupId, $value) {
310 // Call block instance
311 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
315 * Writes given raw data to the file and returns a gap position and length
317 * @param $groupId Group identifier
318 * @param $hash Hash from encoded value
319 * @param $encoded Encoded value to be written to the file
320 * @return $data Gap position and length of the raw data
322 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
323 // Call block instance
324 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
328 * Searches for next suitable gap the given length of data can fit in
329 * including padding bytes.
331 * @param $length Length of raw data
332 * @return $seekPosition Found next gap's seek position
334 public function searchNextGap ($length) {
335 // Call block instance
336 print $this->getBlockInstance()->__toString() . PHP_EOL;
337 return $this->getBlockInstance()->searchNextGap($length);