3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
10 use Org\Mxchange\CoreFramework\Index\Indexable;
11 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
12 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
13 use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
16 use \BadMethodCallException;
17 use \InvalidArgumentException;
19 use \OutOfBoundsException;
21 use \UnexpectedValueException;
24 * A general binary file class
26 * @author Roland Haeder <webmaster@ship-simu.org>
28 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
29 * @license GNU GPL 3.0 or any newer version
30 * @link http://www.ship-simu.org
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 abstract class BaseBinaryFile extends BaseAbstractFile implements BinaryFile {
51 * Separator for header data
53 const SEPARATOR_HEADER_DATA = 0x01;
56 * Separator header->entries
58 const SEPARATOR_HEADER_ENTRIES = 0x02;
61 * Separator group->hash
63 const SEPARATOR_GROUP_HASH = 0x03;
66 * Separator hash->value
68 const SEPARATOR_HASH_VALUE = 0x04;
71 * Separator entry->entry
73 const SEPARATOR_ENTRIES = 0x05;
76 * Separator type->position
78 const SEPARATOR_TYPE_POSITION = 0x06;
83 const LENGTH_COUNT = 20;
88 const LENGTH_POSITION = 20;
93 const LENGTH_GROUP = 10;
96 * Maximum length of entry type
98 const LENGTH_TYPE = 20;
100 //***** Array elements for 'gaps' array *****
105 const GAPS_INDEX_START = 'start';
110 const GAPS_INDEX_END = 'end';
113 * Current seek position
115 private $seekPosition = 0;
120 private $headerSize = 0;
125 private $header = [];
128 * Seek positions for gaps ("fragmentation")
133 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
135 private $damagedEntries = [];
140 private $backBuffer = '';
143 * Currently loaded block (will be returned by current())
145 private $currentBlock = '';
148 * Protected constructor
150 * @param $className Name of the class
153 protected function __construct (string $className) {
154 // Call parent constructor
155 parent::__construct($className);
157 // Init counters and gaps array
158 $this->initCountersGapsArray();
162 * Setter for backBuffer field
164 * @param $backBuffer Characters to "store" in back-buffer
167 private function setBackBuffer (string $backBuffer) {
168 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting backBuffer(%d)=%s - CALLED!', strlen($backBuffer), $backBuffer));
169 $this->backBuffer = $backBuffer;
173 * Getter for backBuffer field
175 * @return $backBuffer Characters "stored" in back-buffer
177 private function getBackBuffer () {
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Getting this->backBuffer(%d)=%s - CALLED!', strlen($this->backBuffer), $this->backBuffer));
179 return $this->backBuffer;
183 * Setter for current field
185 * @param $current Characters to set a currently loaded block
188 private function setCurrentBlock (string $currentBlock) {
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting currentBlock(%d)=%s - CALLED!', strlen($currentBlock), $currentBlock));
190 $this->currentBlock = $currentBlock;
194 * Gets currently read data
196 * @return $current Currently read data
198 public function getCurrentBlock () {
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Getting this->currentBlock(%d)=%s - CALLED!', strlen($this->currentBlock), $this->currentBlock));
200 return $this->currentBlock;
204 * Getter for header size
206 * @return $totalEntries Size of file header
208 public final function getHeaderSize () {
209 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Getting this->headerSize=%d - CALLED!', $this->headerSize));
210 return $this->headerSize;
214 * Setter for header size
216 * @param $headerSize Size of file header
219 public final function setHeaderSize (int $headerSize) {
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting headerSize=%d - CALLED!', $headerSize));
221 $this->headerSize = $headerSize;
225 * Getter for header array
227 * @return $totalEntries Size of file header
229 public final function getHeader () {
231 return $this->header;
237 * @param $header Array for a file header
240 public final function setHeader (array $header) {
242 $this->header = $header;
246 * Getter for seek position
248 * @return $seekPosition Current seek position (stored here in object)
250 public final function getSeekPosition () {
251 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Getting this->seekPosition=%d - CALLED!', $this->seekPosition));
252 return $this->seekPosition;
256 * Setter for seek position
258 * @param $seekPosition Current seek position (stored here in object)
261 protected final function setSeekPosition (int $seekPosition) {
262 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting seekPosition=%d - CALLED!', $seekPosition));
263 $this->seekPosition = $seekPosition;
267 * Marks whole file as gaps-only (freshly created file
269 * @param $type Type of file
270 * @param $minimumBlockLength Minimum block length
273 private function markFileGapsOnly (string $type, int $minimumBlockLength) {
274 // Very simple to do ...
275 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s,minimumBlockLength=%d - CALLED!', $type, $minimumBlockLength));
276 for ($idx = 0; $idx < FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); $idx++) {
277 // Mark start and end position as gap
278 $this->addGap($idx * $minimumBlockLength, $idx * $minimumBlockLength + $minimumBlockLength);
282 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
286 * Checks whether the abstracted file only contains gaps by counting all
287 * gaps' bytes together and compare it to total length.
289 * @return $isGapsOnly Whether the abstracted file only contains gaps
291 private function isFileGapsOnly () {
293 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
295 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->gaps()=%d', count($this->gaps)));
296 foreach ($this->gaps as $gap) {
297 // Calculate size of found gap: end-start including both
298 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gap[%s]=%d,ga[%s]=%d', self::GAPS_INDEX_START, $gap[self::GAPS_INDEX_START], self::GAPS_INDEX_END, $gap[self::GAPS_INDEX_END]));
299 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
302 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gapsSize=%d', $gapsSize));
305 // Total gap size + header size must be same as file size
306 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gapsSize=%d,this->fileSize=%d', $gapsSize, $this->getFileSize()));
307 $isGapsOnly = ($gapsSize + 1 == $this->getFileSize());
310 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isGapsOnly=%d - EXIT!', intval($isGapsOnly)));
315 * Adds a gap for given start and end position
317 * @param $startPosition Start seek position
318 * @param $endPosition End seek position
321 private function addGap(int $startPosition, int $endPosition) {
322 // Push to gaps array
323 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: startPosition=%d,endPosition=%d - CALLED!', $startPosition, $endPosition));
324 array_push($this->gaps, [
325 self::GAPS_INDEX_START => $startPosition,
326 self::GAPS_INDEX_END => $endPosition,
330 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
334 * Initializes the back-buffer by setting it to an empty string.
338 private function initBackBuffer () {
339 // Simply call the setter
340 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
341 $this->setBackBuffer('');
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
348 * Seeks to beginning of file, updates seek position in this object and
349 * flushes the header.
351 * @param $flushHeader Wether the file's header should be flushed (default: false)
354 protected function rewindUpdateSeekPosition (bool $flushHeader = false) {
355 // Seek to beginning of file
356 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: flushHeader=%d - CALLED!', intval($flushHeader)));
359 // And update seek position ...
360 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->updateSeekPosition() ...');
361 $this->updateSeekPosition();
365 // ... to write it back into the file
366 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
367 $this->flushFileHeader();
371 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
375 * Seeks to old position
379 protected function seekToOldPosition () {
380 // Seek to currently ("old") saved position
381 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
382 $this->seek($this->determineSeekPosition());
385 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
389 * Initializes this file class
391 * @param $fileInfoInstance An instance of a SplFileInfo class
394 protected function initFile (SplFileInfo $fileInfoInstance) {
395 // Get a file i/o pointer instance
396 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance));
397 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileInfoInstance));
399 // ... and set it here
400 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting pointerInstance=%s ...', $pointerInstance->__toString()));
401 $this->setPointerInstance($pointerInstance);
404 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
408 * Marks the currently loaded block as empty (with length of the block)
410 * @param $length Length of the block
412 * @throws InvalidArgumentException If a parameter is invalid
414 protected function markCurrentBlockAsEmpty (int $length) {
415 // Validate parameter
416 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
418 // Length cannot below one
419 throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
422 // Get current seek position
423 $currentPosition = $this->determineSeekPosition();
425 // Now add it as gap entry
426 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: currentPosition=%d', $currentPosition));
427 $this->addGap(($currentPosition - $length), $currentPosition);
430 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
434 * Initializes counter for valid entries, arrays for damaged entries and
435 * an array for gap seek positions. If you call this method on your own,
436 * please re-analyze the file structure. So you are better to call
437 * analyzeFileStructure() instead of this method.
441 public function initCountersGapsArray () {
442 // Init counter and seek position to header size
443 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
444 $this->setCounter(0);
445 $this->setSeekPosition($this->getHeaderSize());
449 $this->damagedEntries = [];
452 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
456 * Updates seekPosition attribute from file to avoid to much access on file.
460 public function updateSeekPosition () {
461 // Get key (= seek position)
462 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
463 $seekPosition = $this->determineSeekPosition();
466 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
467 $this->setSeekPosition($seekPosition);
470 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
474 * Checks whether the block separator has been found
476 * @param $str String to look in
477 * @return $isFound Whether the block separator has been found
478 * @throws InvalidArgumentException If a parameter is not valid
480 public static function isBlockSeparatorFound (string $str) {
481 // Validate parameter
482 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: str=%s - CALLED!', $str));
485 throw new InvalidArgumentException('Parameter "str" is empty');
489 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
492 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isFound=%d - EXIT!', intval($isFound)));
497 * Writes data at given position
499 * @param $seekPosition Seek position
500 * @param $data Data to be written
501 * @param $flushHeader Whether to flush the header (default: flush)
503 * @throws OutOfBoundsException If the position is not seekable
504 * @throws InvalidArgumentException If a parameter is invalid
506 public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
507 // Validate parameter
508 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%s,data()=%d,flushHeader=%d - CALLED!', $seekPosition, strlen($data), intval($flushHeader)));
509 if ($seekPosition < 0) {
510 // Invalid seek position
511 throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition));
512 } elseif (empty($data)) {
513 // Empty data is invalid, too
514 throw new InvalidArgumentException('Parameter "data" is empty');
517 // Write data at given position
518 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->writeAtPosition(%d,%s) ...', $seekPosition, $data));
519 $this->writeAtPosition($seekPosition, $data);
522 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->incrementCounter() ...');
523 $this->incrementCounter();
525 // Update seek position
526 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->updateSeekPosition() ...');
527 $this->updateSeekPosition();
530 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: flushHeader=%d', intval($flushHeader)));
531 if ($flushHeader === true) {
533 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
534 $this->flushFileHeader();
536 // Seek to old position
537 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->seekToOldPosition() ...');
538 $this->seekToOldPosition();
542 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
546 * Writes at given position by seeking to it.
548 * @param $seekPosition Seek position in file
549 * @param $dataStream Data to be written
550 * @return mixed Number of writes bytes or false on error
551 * @throws OutOfBoundsException If the position is not seekable
552 * @throws InvalidArgumentException If a parameter is not valid
554 public function writeAtPosition (int $seekPosition, string $dataStream) {
555 // Validate parameter
556 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
557 if ($seekPosition < 0) {
558 // Invalid seek position
559 throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition));
560 } elseif (empty($dataStream)) {
562 throw new InvalidArgumentException('Parameter "dataStream" is empty');
565 // Call pointer's method
566 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->pointerInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
567 $status = $this->getPointerInstance()->writeAtPosition($seekPosition, $dataStream);
570 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: status[%s]=%d - EXIT!', gettype($status), $status));
575 * Checks whether the file header is initialized
577 * @return $isInitialized Whether the file header is initialized
579 public function isFileHeaderInitialized () {
580 // Default is not initialized
581 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
582 $isInitialized = false;
584 // Is the file initialized?
585 if ($this->isFileInitialized()) {
586 // Some bytes has been written, so rewind to start of it.
590 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->readFileHeader() ...');
591 $this->readFileHeader();
594 $headerCount = count($this->getHeader());
596 // The above method does already check the header
597 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: headerCount=%d', $headerCount));
598 $isInitialized = ($headerCount > 0);
602 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
603 return $isInitialized;
607 * Checks whether the assigned file has been initialized
609 * @return $isInitialized Whether the file's size is zero
610 * @throws UnexpectedValueException If an unexpected value was returned
612 public function isFileInitialized () {
613 // Get it from iterator which holds the pointer instance. If false is returned
614 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
615 $fileSize = $this->size();
618 * The returned file size should not be false or NULL as this means
619 * that the pointer class does not work correctly.
621 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize[%s]=%d', gettype($fileSize), $fileSize));
622 if (!is_int($fileSize)) {
624 throw new UnexpectedValueException(sprintf('fileSize[]=%s is unexpected', gettype($fileSize)));
627 // Is more than 0 returned?
628 $isInitialized = ($fileSize > 0);
631 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
632 return $isInitialized;
636 * Creates the assigned file
639 * @throws BadMethodCallException If this file's header is already initialized
641 public function createFileHeader () {
642 // The file's header should not be initialized here
643 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
644 if ($this->isFileHeaderInitialized()) {
646 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: this=%s', __METHOD__, __LINE__, print_r($this, TRUE)));
647 throw new BadMethodCallException('File header is already initialized but method called');
650 // Simple flush file header which will create it.
651 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
652 $this->flushFileHeader();
654 // Rewind seek position (to beginning of file) and update/flush file header
655 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->rewindUpdateSeekPosition() ...');
656 $this->rewindUpdateSeekPosition();
659 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
663 * Pre-allocates file (if enabled) with some space for later faster write access.
665 * @param $type Type of the file
667 * @throws InvalidArgumentException If a parameter is empty
669 public function preAllocateFile (string $type) {
671 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s - CALLED!', $type));
674 throw new InvalidArgumentException('Parameter "type" is empty');
675 } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
676 // Don't continue here.
677 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Not pre-allocating file.'));
682 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
684 // Calculate minimum length for one entry and get file size
685 $minimumBlockLength = $this->getIndexInstance()->calculateMinimumBlockLength();
686 $fileSize = $this->getFileSize();
688 // Calulcate seek position
689 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: minimumBlockLength=%d,fileSize=%d', $minimumBlockLength, $fileSize));
690 $seekPosition = $minimumBlockLength * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count') + $fileSize ;
692 // Now simply write a NUL there. This will pre-allocate the file.
693 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->writeAtPosition(%d,NUL) ...', $seekPosition));
694 $this->writeAtPosition($seekPosition, chr(0));
696 // Is the seek position zero?
697 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize=%d', $fileSize));
698 if ($fileSize == 0) {
699 // Mark file as gaps-only
700 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->markGapsOnly(%s,%d) ...', $type, $minimumBlockLength));
701 $this->markFileGapsOnly($type, $minimumBlockLength);
703 // Analyze file structure
704 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->analyzeFileStructure() ...');
705 $this->analyzeFileStructure();
708 // Rewind seek position
709 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->rewind() ...');
713 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
717 * Determines seek position
719 * @return $seekPosition Current seek position
721 public function determineSeekPosition () {
722 // Call pointer instance
723 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
724 $seekPosition = $this->getPointerInstance()->determineSeekPosition();
727 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d - EXIT!', $seekPosition));
728 return $seekPosition;
732 * Seek to given offset (default) or other possibilities as fseek() gives.
734 * @param $offset Offset to seek to (or used as "base" for other seeks)
735 * @param $whence Added to offset (default: only use offset to seek to)
736 * @return $status Status of file seek: 0 = success, -1 = failed
737 * @throws OutOfBoundsException If the position is not seekable
739 public function seek (int $offset, int $whence = SEEK_SET) {
740 // Validate parameter
741 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: offset=%d,whence=%d - CALLED!', $offset, $whence));
743 // No offset is smaller than zero
744 throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
747 // Call pointer instance
748 $status = $this->getPointerInstance()->seek($offset, $whence);
751 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: status[%s]=%d - EXIT!', gettype($status), $status));
756 * Reads given amount of bytes from file.
758 * @param $bytes Amount of bytes to read
759 * @return $data Data read from file
760 * @throws OutOfBoundsException If the position is not seekable
762 public function read (int $bytes = 0) {
763 // Validate parameter
764 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: bytes=%d - CALLED!', $bytes));
767 throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
770 // Call pointer instance
771 $data = $this->getPointerInstance()->read($bytes);
774 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data[%s]=%s - EXIT!', gettype($data), $data));
779 * Rewinds to the beginning of the file
783 public function rewind () {
784 // Call pointer instance
785 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
786 $this->getPointerInstance()->rewind();
789 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
793 * Analyzes entries in index file. This will count all found (and valid)
794 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
795 * only gaps are found, the file is considered as "virgin" (no entries).
798 * @throws BadMethodCallException If this method is called but file is not initialized
800 public function analyzeFileStructure () {
801 // Make sure the file is initialized
802 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
803 if (!$this->isFileInitialized()) {
805 throw new BadMethodCallException('Method called but file is not initialized.');
808 // Init counters and gaps array
809 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->initCounterGapsArrays() ...');
810 $this->initCountersGapsArray();
812 // Output message (as this may take some time)
813 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Analyzing file structure ... (this may take some time)'));
815 // First rewind to the begining
816 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->rewind() ...');
819 // Then try to load all entries
820 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Looping through file');
821 while ($this->isValid()) {
823 $current = $this->getCurrentBlock();
826 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->readNextBlock() ...');
827 $this->readNextBlock();
830 * If the block is empty, maybe the whole file is? This could mean
831 * that the file has been pre-allocated.
833 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: current(%d)[]=%s', strlen($current), gettype($current)));
834 if (empty($current)) {
835 // Then skip this part
836 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: current[]=%s is empty - CONTINUE!', gettype($current)));
840 // Handle current record
841 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: current(%d)[]=%s', strlen($current), gettype($current)));
844 // If the last read block is empty, check gaps
845 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: current()=%d', strlen($current)));
846 if (empty($current)) {
848 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Found a total of %d gaps.', count($this->gaps)));
850 // Check gaps, if the whole file is empty.
851 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->isFileGapsOnly() ...');
852 if ($this->isFileGapsOnly()) {
853 // Only gaps, so don't continue here.
854 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: File is gaps-only - EXIT!');
859 * The above call has calculated a total size of all gaps. If the
860 * percentage of gaps passes a "soft" limit and last
861 * defragmentation is to far in the past, or if a "hard" limit has
862 * reached, run defragmentation.
864 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->isDefragmentationNeeded() ...');
865 if ($this->isDefragmentationNeeded()) {
866 // Run "defragmentation"
867 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->doRunDefragmentation() ...');
868 $this->doRunDefragmentation();
873 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
877 * Reads next "block" of bytes into $currentBlock field
880 * @throws BadMethodCallException If this method was called without prior valid() call
882 public function readNextBlock () {
883 // Is there nothing to read?
884 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
885 if (!$this->isValid()) {
887 throw new BadMethodCallException('next() invoked but no valid current block (EOF?)');
890 // First calculate minimum block length
891 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: this->seekPosition=%d', $this->determineSeekPosition()));
892 $length = $this->getIndexInstance()->calculateMinimumBlockLength();
893 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
895 // Read possibly back-buffered bytes from previous call of next().
896 $data = $this->getBackBuffer();
899 * Read until a entry/block separator has been found. The next read
900 * "block" may not fit, so this loop will continue until the EOB or EOF
901 * has been reached whatever comes first.
903 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
904 while ((!$this->isEndOfFileReached()) && (empty($data) || !self::isBlockSeparatorFound($data))) {
905 // Then read the next possible block
906 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->read(%d) ...', $length));
907 $block = $this->read($length);
910 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: block()=%d,length=%d', strlen($block), $length));
911 if (strlen(trim($block, chr(0))) == 0) {
912 // Mark this block as empty
913 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->markCurrentBlockAsEmpty(%d) ...', strlen($block)));
914 $this->markCurrentBlockAsEmpty(strlen($block));
917 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Empty block found ... - BREAK!');
921 // At this block then
925 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data(%d)=%s', strlen($data), $data));
929 * Init back-buffer which is the data that has been found beyond the
932 $this->initBackBuffer();
935 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data(%d)=%s', strlen($data), $data));
937 // Yes, maybe whole file was ...
938 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Maybe empty file found - EXIT!');
943 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
945 // Left part is the actual block, right one the back-buffer data, if found
946 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray()=%d', count($dataArray)));
947 //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray=%s', print_r($dataArray, true)));
948 $this->setCurrentBlock($dataArray[0]);
950 // Is back buffere data found?
951 if (isset($dataArray[1])) {
953 $this->setBackBuffer($dataArray[1]);
957 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
961 * Checks wether the current entry is valid (not at the end of the file).
962 * This method will return true if an emptied (nulled) entry has been found.
964 * @return $isValid Whether the next entry is valid
965 * @throws UnexpectedValueException If some value is not expected
967 public function isValid () {
968 // First calculate minimum block length
969 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
970 $length = $this->getIndexInstance()->calculateMinimumBlockLength();
972 // Short be more than zero!
973 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
976 throw new UnexpectedValueException(sprintf('length=%d is not expected', $length));
979 // Get current seek position
980 $seekPosition = $this->determineSeekPosition();
982 // Then try to read it
983 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
984 $data = $this->read($length);
986 // If some bytes could be read, all is fine
987 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data[%s]()=%d', gettype($data), strlen($data)));
988 $isValid = ((is_string($data)) && (strlen($data) > 0));
991 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isValid=%d', intval($isValid)));
992 $headerSize = $this->getHeaderSize();
994 // Is the seek position at or beyond the header?
995 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d,headerSize=%d', $seekPosition, $headerSize));
996 if ($seekPosition >= $headerSize) {
997 // Seek back to old position
998 $isValid = ($isValid && $this->seek($seekPosition) === 0);
1000 // Seek directly behind the header
1001 $isValid = ($isValid && $this->seek($headerSize) === 0);
1005 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isValid=%d - EXIT!', intval($isValid)));
1010 * Reads the file header
1013 * @throws LogicException If both instances are not set
1015 public function readFileHeader () {
1016 // Is index set or stack?
1017 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
1018 if ($this->getIndexInstance() instanceof Indexable) {
1019 // Call index instance
1020 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->indexInstance->readIndexHeader() ...');
1021 $this->getIndexInstance()->readIndexHeader();
1022 } elseif ($this->getStackInstance() instanceof StackableFile) {
1023 // Call stacke instance
1024 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->stackInstance->readStackHeader() ...');
1025 $this->getStackInstance()->readStackHeader();
1028 throw new LogicException('Wether indexInstance nor stackInstance are set');
1032 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
1036 * Flushes the file header
1040 public function flushFileHeader () {
1041 // Call block instance
1042 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
1043 $this->getIndexInstance()->flushFileHeader();
1046 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
1050 * Searches for next suitable gap the given length of data can fit in
1051 * including padding bytes.
1053 * @param $length Length of raw data
1054 * @return $seekPosition Found next gap's seek position
1055 * @throws InvalidArgumentException If the parameter is not valid
1057 public function searchNextGap (int $length) {
1058 // If the file is only gaps, no need to seek
1059 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
1062 throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
1063 } elseif ($this->isFileGapsOnly()) {
1065 * The first empty block is the 2nd one right after the header, so
1066 * one byte gap to the header.
1068 $seekPosition = ($this->getHeaderSize() + 2);
1071 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d - EXIT!', $seekPosition));
1072 return $seekPosition;
1076 $this->partialStub('length=' . $length);