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