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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.ship-simu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this class
46 * @param $pointerInstance An instance of a Block class
47 * @return $iteratorInstance An instance of a Iterator class
49 public final static function createFileIterator (Block $blockInstance) {
51 $iteratorInstance = new FileIterator();
53 // Set the instance here
54 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
55 $iteratorInstance->setBlockInstance($blockInstance);
57 // Return the prepared instance
58 return $iteratorInstance;
62 * Gets currently read data
64 * @return $current Currently read data
66 public function current () {
67 // Call block instance
68 return $this->getBlockInstance()->current();
72 * Gets current seek position ("key").
74 * @return $key Current key in iteration
76 public function key () {
78 return $this->getBlockInstance()->determineSeekPosition();
82 * Advances to next "block" of bytes
86 public function next () {
87 // Call block instance
88 $this->getBlockInstance()->next();
92 * Rewinds to the beginning of the file
94 * @return $status Status of this operation
96 public function rewind () {
97 // Call block instance
98 return $this->getBlockInstance()->rewind();
102 * Checks wether the current entry is valid (not at the end of the file).
103 * This method will return true if an emptied (nulled) entry has been found.
105 * @return $isValid Whether the next entry is valid
107 public function valid () {
108 // Call block instance
109 return $this->getBlockInstance()->valid();
113 * Seeks to given position
115 * @param $seekPosition Seek position in file
116 * @return $status Status of this operation
118 public function seek ($seekPosition) {
119 // Call block instance
120 return $this->getBlockInstance()->seek($seekPosition);
126 * @return $size Size (in bytes) of file
128 public function size () {
129 // Call the block object
130 $size = $this->getBlockInstance()->size();
137 * Reads given amount of bytes from file.
139 * @param $bytes Amount of bytes to read
140 * @return $data Data read from file
142 public function read ($bytes = NULL) {
143 // Call block instance
144 return $this->getBlockInstance()->read($bytes);
148 * Analyzes entries in index file. This will count all found (and valid)
149 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
150 * only gaps are found, the file is considered as "virgin" (no entries).
154 public function analyzeFile () {
155 // Just call the block instance
156 $this->getBlockInstance()->analyzeFile();
160 * Checks whether the file header is initialized
162 * @return $isInitialized Whether the file header is initialized
164 public function isFileHeaderInitialized () {
165 // Just call the block instance
166 return $this->getBlockInstance()->isFileHeaderInitialized();
170 * Creates the assigned file
174 public function createFileHeader () {
175 // Just call the block instance
176 $this->getBlockInstance()->createFileHeader();
180 * Pre-allocates file (if enabled) with some space for later faster write access.
182 * @param $type Type of the file
185 public function preAllocateFile ($type) {
186 // Just call the block instance
187 $this->getBlockInstance()->preAllocateFile($type);
191 * Initializes counter for valid entries, arrays for damaged entries and
192 * an array for gap seek positions. If you call this method on your own,
193 * please re-analyze the file structure. So you are better to call
194 * analyzeFile() instead of this method.
198 public function initCountersGapsArray () {
199 // Call block instance
200 $this->getBlockInstance()->initCountersGapsArray();
204 * Getter for header size
206 * @return $totalEntries Size of file header
208 public final function getHeaderSize () {
209 // Call block instance
210 return $this->getBlockInstance()->getHeaderSize();
214 * Setter for header size
216 * @param $headerSize Size of file header
219 public final function setHeaderSize ($headerSize) {
220 // Call block instance
221 $this->getBlockInstance()->setHeaderSize($headerSize);
225 * Getter for header array
227 * @return $totalEntries Size of file header
229 public final function getHeader () {
230 // Call block instance
231 return $this->getBlockInstance()->getHeader();
237 * @param $header Array for a file header
240 public final function setHeader (array $header) {
241 // Call block instance
242 $this->getBlockInstance()->setHeader($header);
246 * Updates seekPosition attribute from file to avoid to much access on file.
250 public function updateSeekPosition () {
251 // Call block instance
252 $this->getBlockInstance()->updateSeekPosition();
256 * Getter for total entries
258 * @return $totalEntries Total entries in this file
260 public final function getCounter () {
261 // Call block instance
262 return $this->getBlockInstance()->getCounter();
266 * "Getter" for file size
268 * @return $fileSize Size of currently loaded file
270 public function getFileSize () {
271 // Call block instance
272 return $this->getBlockInstance()->getFileSize();
276 * Writes data at given position
278 * @param $seekPosition Seek position
279 * @param $data Data to be written
280 * @param $flushHeader Whether to flush the header (default: flush)
283 public function writeData ($seekPosition, $data, $flushHeader = true) {
284 // Call block instance
285 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
289 * Getter for seek position
291 * @return $seekPosition Current seek position (stored here in object)
293 public function getSeekPosition () {
294 // Call block instance
295 return $this->getBlockInstance()->getSeekPosition();
299 * Writes given value to the file and returns a hash and gap position for it
301 * @param $groupId Group identifier
302 * @param $value Value to be added to the stack
303 * @return $data Hash and gap position
305 public function writeValueToFile ($groupId, $value) {
306 // Call block instance
307 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
311 * Writes given raw data to the file and returns a gap position and length
313 * @param $groupId Group identifier
314 * @param $hash Hash from encoded value
315 * @param $encoded Encoded value to be written to the file
316 * @return $data Gap position and length of the raw data
318 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
319 // Call block instance
320 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
324 * Searches for next suitable gap the given length of data can fit in
325 * including padding bytes.
327 * @param $length Length of raw data
328 * @return $seekPosition Found next gap's seek position
330 public function searchNextGap ($length) {
331 // Call block instance
332 print $this->getBlockInstance()->__toString() . PHP_EOL;
333 return $this->getBlockInstance()->searchNextGap($length);