c96a4440ee551d14e6833d5b879775d9fc310cea
[core.git] / inc / classes / main / file_directories / class_BaseFile.php
1 <?php
2 /**
3  * A general file class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseFile extends BaseFrameworkSystem {
25         /**
26          * Separator for header data
27          */
28         const SEPARATOR_HEADER_DATA = 0x01;
29
30         /**
31          * Separator header->entries
32          */
33         const SEPARATOR_HEADER_ENTRIES = 0x02;
34
35         /**
36          * Separator hash->name
37          */
38         const SEPARATOR_HASH_NAME = 0x03;
39
40         /**
41          * Separator entry->entry
42          */
43         const SEPARATOR_ENTRIES = 0x04;
44
45         /**
46          * Separator type->position
47          */
48         const SEPARATOR_TYPE_POSITION = 0x05;
49
50         /**
51          * Length of count
52          */
53         const LENGTH_COUNT = 20;
54
55         /**
56          * Length of position
57          */
58         const LENGTH_POSITION = 20;
59
60         /**
61          * Length of name
62          */
63         const LENGTH_NAME = 10;
64
65         /**
66          * Maximum length of entry type
67          */
68         const LENGTH_TYPE = 20;
69
70         /**
71          * Length of output from hash()
72          */
73         private static $hashLength = NULL;
74
75         /**
76          * Counter for total entries
77          */
78         private $totalEntries = 0;
79
80         /**
81          * Current seek position
82          */
83         private $seekPosition = 0;
84
85         /**
86          * Size of header
87          */
88         private $headerSize = 0;
89
90         /**
91          * File header
92          */
93         private $header = array();
94
95         /**
96          * Seek positions for gaps ("fragmentation")
97          */
98         private $gaps = array();
99
100         /**
101          * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
102          */
103         private $damagedEntries = array();
104
105         /**
106          * The current file we are working in
107          */
108         private $fileName = '';
109
110         /**
111          * Back-buffer
112          */
113         private $backBuffer = '';
114
115         /**
116          * Currently loaded block (will be returned by current())
117          */
118         private $currentBlock = '';
119
120         /**
121          * Protected constructor
122          *
123          * @param       $className      Name of the class
124 y        * @return      void
125          */
126         protected function __construct ($className) {
127                 // Call parent constructor
128                 parent::__construct($className);
129
130                 // Init counters and gaps array
131                 $this->initCountersGapsArray();
132         }
133
134         /**
135          * Destructor for cleaning purposes, etc
136          *
137          * @return      void
138          */
139         public final function __destruct() {
140                 // Try to close a file
141                 $this->closeFile();
142
143                 // Call the parent destructor
144                 parent::__destruct();
145         }
146
147         /**
148          * Initializes counter for valid entries, arrays for damaged entries and
149          * an array for gap seek positions. If you call this method on your own,
150          * please re-analyze the file structure. So you are better to call
151          * analyzeFile() instead of this method.
152          *
153          * @return      void
154          */
155         protected function initCountersGapsArray () {
156                 // Init counter and seek position
157                 $this->setCounter(0);
158                 $this->setSeekPosition(0);
159
160                 // Init arrays
161                 $this->gaps = array();
162                 $this->damagedEntries = array();
163         }
164
165         /**
166          * Getter for total entries
167          *
168          * @return      $totalEntries   Total entries in this file
169          */
170         protected final function getCounter () {
171                 // Get it
172                 return $this->totalEntries;
173         }
174
175         /**
176          * Setter for total entries
177          *
178          * @param       $totalEntries   Total entries in this file
179          * @return      void
180          */
181         protected final function setCounter ($counter) {
182                 // Set it
183                 $this->totalEntries = $counter;
184         }
185
186         /**
187          * Increment counter
188          *
189          * @return      void
190          */
191         protected final function incrementCounter () {
192                 // Get it
193                 $this->totalEntries++;
194         }
195
196         /**
197          * Getter for header size
198          *
199          * @return      $totalEntries   Size of file header
200          */
201         public final function getHeaderSize () {
202                 // Get it
203                 return $this->headerSize;
204         }
205
206         /**
207          * Setter for header size
208          *
209          * @param       $headerSize             Size of file header
210          * @return      void
211          */
212         protected final function setHeaderSize ($headerSize) {
213                 // Set it
214                 $this->headerSize = $headerSize;
215         }
216
217         /**
218          * Getter for header array
219          *
220          * @return      $totalEntries   Size of file header
221          */
222         protected final function getHeade () {
223                 // Get it
224                 return $this->header;
225         }
226
227         /**
228          * Setter for header
229          *
230          * @param       $header         Array for a file header
231          * @return      void
232          */
233         protected final function setHeader (array $header) {
234                 // Set it
235                 $this->header = $header;
236         }
237
238         /**
239          * Getter for seek position
240          *
241          * @return      $seekPosition   Current seek position (stored here in object)
242          */
243         protected final function getSeekPosition () {
244                 // Get it
245                 return $this->seekPosition;
246         }
247
248         /**
249          * Setter for seek position
250          *
251          * @param       $seekPosition   Current seek position (stored here in object)
252          * @return      void
253          */
254         protected final function setSeekPosition ($seekPosition) {
255                 // And set it
256                 $this->seekPosition = $seekPosition;
257         }
258
259         /**
260          * Updates seekPosition attribute from file to avoid to much access on file.
261          *
262          * @return      void
263          */
264         protected function updateSeekPosition () {
265                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
266
267                 // Get key (= seek position)
268                 $seekPosition = $this->getIteratorInstance()->key();
269                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
270
271                 // And set it here
272                 $this->setSeekPosition($seekPosition);
273
274                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
275         }
276
277         /**
278          * Seeks to beginning of file, updates seek position in this object and
279          * flushes the header.
280          *
281          * @return      void
282          */
283         protected function rewineUpdateSeekPosition () {
284                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
285
286                 // flushFileHeader must be callable
287                 assert(is_callable(array($this, 'flushFileHeader')));
288
289                 // Seek to beginning of file
290                 $this->getIteratorInstance()->rewind();
291
292                 // And update seek position ...
293                 $this->updateSeekPosition();
294
295                 // ... to write it back into the file
296                 $this->flushFileHeader();
297
298                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
299         }
300
301         /**
302          * Seeks to old position
303          *
304          * @return      void
305          */
306         protected function seekToOldPosition () {
307                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
308
309                 // Seek to currently ("old") saved position
310                 $this->getIteratorInstance()->seek($this->getSeekPosition());
311
312                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
313         }
314
315         /**
316          * Checks whether the block separator has been found
317          *
318          * @param       $str            String to look in
319          * @return      $isFound        Whether the block separator has been found
320          */
321         public static function isBlockSeparatorFound ($str) {
322                 // Determine it
323                 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
324
325                 // Return result
326                 return $isFound;
327         }
328
329         /**
330          * Getter for the file pointer
331          *
332          * @return      $filePointer    The file pointer which shall be a valid file resource
333          * @throws      UnsupportedOperationException   If this method is called
334          */
335         public final function getPointer () {
336                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
337         }
338
339         /**
340          * Setter for file name
341          *
342          * @param       $fileName       The new file name
343          * @return      void
344          */
345         protected final function setFileName ($fileName) {
346                 $fileName = (string) $fileName;
347                 $this->fileName = $fileName;
348         }
349
350         /**
351          * Getter for file name
352          *
353          * @return      $fileName       The current file name
354          */
355         public final function getFileName () {
356                 return $this->fileName;
357         }
358
359         /**
360          * Initializes the back-buffer by setting it to an empty string.
361          *
362          * @return      void
363          */
364         private function initBackBuffer () {
365                 // Simply call the setter
366                 $this->setBackBuffer('');
367         }
368
369         /**
370          * Setter for backBuffer field
371          *
372          * @param       $backBuffer             Characters to "store" in back-buffer
373          * @return      void
374          */
375         private function setBackBuffer ($backBuffer) {
376                 // Cast to string (so no arrays or objects)
377                 $backBuffer = (string) $backBuffer;
378
379                 // ... and set it
380                 $this->backBuffer = $backBuffer;
381         }
382
383         /**
384          * Getter for backBuffer field
385          *
386          * @return      $backBuffer             Characters "stored" in back-buffer
387          */
388         private function getBackBuffer () {
389                 return $this->backBuffer;
390         }
391
392         /**
393          * Setter for currentBlock field
394          *
395          * @param       $currentBlock           Characters to set a currently loaded block
396          * @return      void
397          */
398         private function setCurrentBlock ($currentBlock) {
399                 // Cast to string (so no arrays or objects)
400                 $currentBlock = (string) $currentBlock;
401
402                 // ... and set it
403                 $this->currentBlock = $currentBlock;
404         }
405
406         /**
407          * Gets currently read data
408          *
409          * @return      $current        Currently read data
410          */
411         public function getCurrentBlock () {
412                 // Return it
413                 return $this->currentBlock;
414         }
415
416         /**
417          * Initializes this file class
418          *
419          * @param       $fileName       Name of this abstract file
420          * @return      void
421          */
422         protected function initFile ($fileName) {
423                 // Get a file i/o pointer instance
424                 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
425
426                 // ... and set it here
427                 $this->setPointerInstance($pointerInstance);
428         }
429
430         /**
431          * Writes data at given position
432          *
433          * @param       $seekPosition   Seek position
434          * @param       $data                   Data to be written
435          * @param       $flushHeader    Whether to flush the header (default: flush)
436          * @return      void
437          */
438         protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
439                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
440
441                 // Write data at given position
442                 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
443
444                 // Update seek position
445                 $this->updateSeekPosition();
446
447                 // Flush the header?
448                 if ($flushHeader === TRUE) {
449                         // Flush header
450                         $this->flushFileHeader();
451
452                         // Seek to old position
453                         $this->seekToOldPosition();
454                 } // END - if
455
456                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
457         }
458
459         /**
460          * Checks whether the file header is initialized
461          *
462          * @return      $isInitialized  Whether the file header is initialized
463          */
464         public function isFileHeaderInitialized () {
465                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
466
467                 // Default is not initialized
468                 $isInitialized = FALSE;
469
470                 // Is the file initialized?
471                 if ($this->isFileInitialized()) {
472                         // Some bytes has been written, so rewind to start of it.
473                         $rewindStatus = $this->rewind();
474                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
475
476                         // Is the rewind() call successfull?
477                         if ($rewindStatus != 1) {
478                                 // Something bad happened
479                                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
480                         } // END - if
481
482                         // Read file header
483                         $this->readFileHeader();
484
485                         // The above method does already check the header
486                         $isInitialized = TRUE;
487                 } // END - if
488
489                 // Return result
490                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
491                 return $isInitialized;
492         }
493
494         /**
495          * Checks whether the assigned file has been initialized
496          *
497          * @return      $isInitialized          Whether the file's size is zero
498          */
499         public function isFileInitialized () {
500                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
501
502                 // Get it from iterator which holds the pointer instance. If FALSE is returned
503                 $fileSize = $this->size();
504                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
505
506                 /*
507                  * The returned file size should not be FALSE or NULL as this means
508                  * that the pointer class does not work correctly.
509                  */
510                 assert(is_int($fileSize));
511
512                 // Is more than 0 returned?
513                 $isInitialized = ($fileSize > 0);
514
515                 // Return result
516                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
517                 return $isInitialized;
518         }
519
520         /**
521          * Creates the assigned file
522          *
523          * @return      void
524          */
525         public function createFileHeader () {
526                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
527
528                 // The file's header should not be initialized here
529                 assert(!$this->isFileHeaderInitialized());
530
531                 // Simple flush file header which will create it.
532                 $this->flushFileHeader();
533
534                 // Rewind seek position (to beginning of file) and update/flush file header
535                 $this->rewineUpdateSeekPosition();
536
537                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
538         }
539
540         /**
541          * Pre-allocates file (if enabled) with some space for later faster write access.
542          *
543          * @param       $type   Type of the file
544          * @return      void
545          */
546         public function preAllocateFile ($type) {
547                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
548
549                 // Is it enabled?
550                 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
551                         // Not enabled
552                         self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
553
554                         // Don't continue here.
555                         return;
556                 } // END - if
557
558                 // Message to user
559                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
560
561                 // Calculate minimum length for one entry
562                 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
563                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
564
565                 // Calulcate seek position
566                 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
567                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
568
569                 // Now simply write a NUL there. This will pre-allocate the file.
570                 $this->writeData($seekPosition, chr(0));
571
572                 // Rewind seek position (to beginning of file) and update/flush file header
573                 $this->rewineUpdateSeekPosition();
574
575                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
576         }
577
578         /**
579          * Close a file source and set it's instance to null and the file name
580          * to empty
581          *
582          * @return      void
583          * @todo        ~10% done?
584          */
585         public function closeFile () {
586                 $this->partialStub('Unfinished method.');
587
588                 // Remove file name
589                 $this->setFileName('');
590         }
591
592         /**
593          * Determines seek position
594          *
595          * @return      $seekPosition   Current seek position
596          */
597         public function determineSeekPosition () {
598                 // Call pointer instance
599                 return $this->getPointerInstance()->determineSeekPosition();
600         }
601
602         /**
603          * Seek to given offset (default) or other possibilities as fseek() gives.
604          *
605          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
606          * @param       $whence         Added to offset (default: only use offset to seek to)
607          * @return      $status         Status of file seek: 0 = success, -1 = failed
608          */
609         public function seek ($offset, $whence = SEEK_SET) {
610                 // Call pointer instance
611                 return $this->getPointerInstance()->seek($offset, $whence);
612         }
613
614         /**
615          * Size of this file
616          *
617          * @return      $size   Size (in bytes) of file
618          * @todo        Handle seekStatus
619          */
620         public function size () {
621                 // Call pointer instance
622                 return $this->getPointerInstance()->size();
623         }
624
625         /**
626          * Read data a file pointer
627          *
628          * @return      mixed   The result of fread()
629          * @throws      NullPointerException    If the file pointer instance
630          *                                                                      is not set by setPointer()
631          * @throws      InvalidResourceException        If there is being set
632          */
633         public function readFromFile () {
634                 // Call pointer instance
635                 return $this->getPointerInstance()->readFromFile();
636         }
637
638         /**
639          * Reads given amount of bytes from file.
640          *
641          * @param       $bytes  Amount of bytes to read
642          * @return      $data   Data read from file
643          */
644         public function read ($bytes) {
645                 // Call pointer instance
646                 return $this->getPointerInstance()->read($bytes);
647         }
648
649         /**
650          * Write data to a file pointer
651          *
652          * @param       $dataStream             The data stream we shall write to the file
653          * @return      mixed                   Number of writes bytes or FALSE on error
654          * @throws      NullPointerException    If the file pointer instance
655          *                                                                      is not set by setPointer()
656          * @throws      InvalidResourceException        If there is being set
657          *                                                                                      an invalid file resource
658          */
659         public function writeToFile ($dataStream) {
660                 // Call pointer instance
661                 return $this->getPointerInstance()->writeToFile($dataStream);
662         }
663
664         /**
665          * Rewinds to the beginning of the file
666          *
667          * @return      $status         Status of this operation
668          */
669         public function rewind () {
670                 // Call pointer instance
671                 return $this->getPointerInstance()->rewind();
672         }
673
674         /**
675          * Determines whether the EOF has been reached
676          *
677          * @return      $isEndOfFileReached             Whether the EOF has been reached
678          */
679         public final function isEndOfFileReached () {
680                 // Call pointer instance
681                 return $this->getPointerInstance()->isEndOfFileReached();
682         }
683
684         /**
685          * Analyzes entries in index file. This will count all found (and valid)
686          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
687          * only gaps are found, the file is considered as "virgin" (no entries).
688          *
689          * @return      void
690          */
691         public function analyzeFile () {
692                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
693
694                 // Make sure the file is initialized
695                 assert($this->isFileInitialized());
696
697                 // Init counters and gaps array
698                 $this->initCountersGapsArray();
699
700                 // Output message (as this may take some time)
701                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
702
703                 // First rewind to the begining
704                 $this->rewind();
705
706                 // Then try to load all entries
707                 while ($this->valid()) {
708                         // Go to next entry
709                         $this->next();
710
711                         // Get current entry
712                         $current = $this->getCurrentBlock();
713
714                         // Debug message
715                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
716                 } // END - while
717
718                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
719         }
720
721         /**
722          * Advances to next "block" of bytes
723          *
724          * @return      void
725          * @todo        This method will load large but empty files in a whole
726          */
727         public function next () {
728                 // Is there nothing to read?
729                 if (!$this->valid()) {
730                         // Nothing to read
731                         return;
732                 } // END - if
733
734                 // Make sure the block instance is set
735                 assert($this->getBlockInstance() instanceof CalculatableBlock);
736
737                 // First calculate minimum block length
738                 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
739
740                 // Short be more than zero!
741                 assert($length > 0);
742
743                 // Wait until a entry/block separator has been found
744                 $data = $this->getBackBuffer();
745                 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
746                         // Then read the block
747                         $data .= $this->read($length);
748                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data));
749                 } // END - if
750
751                 // EOF reached?
752                 if ($this->isEndOfFileReached()) {
753                         // Set whole data as current block
754                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
755                         $this->setCurrentBlock($data);
756
757                         // Then abort here silently
758                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
759                         return;
760                 } // END - if
761
762                 /*
763                  * Init back-buffer which is the data that has been found beyond the
764                  * separator.
765                  */
766                 $this->initBackBuffer();
767
768                 // Separate data
769                 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
770
771                 // This array must contain two elements
772                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
773                 assert(count($dataArray) == 2);
774
775                 // Left part is the actual block, right one the back-buffer data
776                 $this->setCurrentBlock($dataArray[0]);
777                 $this->setBackBuffer($dataArray[1]);
778         }
779
780         /**
781          * Checks wether the current entry is valid (not at the end of the file).
782          * This method will return TRUE if an emptied (nulled) entry has been found.
783          *
784          * @return      $isValid        Whether the next entry is valid
785          */
786         public function valid () {
787                 // Make sure the block instance is set
788                 assert($this->getBlockInstance() instanceof Block);
789
790                 // First calculate minimum block length
791                 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
792
793                 // Short be more than zero!
794                 assert($length > 0);
795
796                 // Get current seek position
797                 $seekPosition = $this->key();
798
799                 // Then try to read it
800                 $data = $this->read($length);
801
802                 // If some bytes could be read, all is fine
803                 $isValid = ((is_string($data)) && (strlen($data) > 0));
804
805                 // Get header size
806                 $headerSize = $this->getBlockInstance()->getHeaderSize();
807
808                 // Is the seek position at or beyond the header?
809                 if ($seekPosition >= $headerSize) {
810                         // Seek back to old position
811                         $this->seek($seekPosition);
812                 } else {
813                         // Seek directly behind the header
814                         $this->seek($headerSize);
815                 }
816
817                 // Return result
818                 return $isValid;
819         }
820
821         /**
822          * Gets current seek position ("key").
823          *
824          * @return      $key    Current key in iteration
825          */
826         public function key () {
827                 // Call pointer instance
828                 return $this->getPointerInstance()->determineSeekPosition();
829         }
830
831         /**
832          * Reads the file header
833          *
834          * @return      void
835          */
836         public function readFileHeader () {
837                 // Make sure the block instance is set
838                 assert($this->getBlockInstance() instanceof Block);
839
840                 // Call block instance
841                 $this->getBlockInstance()->readFileHeader();
842         }
843
844         /**
845          * Flushes the file header
846          *
847          * @return      void
848          */
849         public function flushFileHeader () {
850                 // Make sure the block instance is set
851                 assert($this->getBlockInstance() instanceof Block);
852
853                 // Call block instance
854                 $this->getBlockInstance()->flushFileHeader();
855         }
856 }
857
858 // [EOF]
859 ?>