3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Stack\FileStacker;
7 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
10 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
13 use \InvalidArgumentException;
19 * @author Roland Haeder <webmaster@ship-simu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class StackFile extends BaseBinaryFile implements FileStacker {
40 * Protected constructor
44 private function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of this File class and prepares it for usage
52 * @param $infoInstance An instance of a SplFileInfo class
53 * @param $stackInstance An instance of a StackableFile class
54 * @return $fileInstance An instance of this File class
56 public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
58 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
59 $fileInstance = new StackFile();
61 // Set stack instance here for callbacks
62 $fileInstance->setStackInstance($stackInstance);
64 // Init this abstract file
65 $fileInstance->initFile($infoInstance);
67 // Return the prepared instance
68 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: fileInstance=%s - EXIT!', $fileInstance->__toString()));
73 * Writes given value to the file and returns a hash and gap position for it
75 * @param $stackName Group identifier
76 * @param $value Value to be added to the stack
77 * @return $data Hash and gap position
78 * @throws InvalidArgumentException If a parameter is not valid
80 public function writeValueToFile (string $stackName, $value) {
82 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
83 if (empty($stackName)) {
85 throw new InvalidArgumentException('Parameter "stackName" is empty');
86 } elseif (is_object($value) || is_resource($value)) {
88 throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
91 // Encode/convert the value into a "binary format"
92 $encoded = StringUtils::encodeData($value);
94 // Get a strong hash for the "encoded" data
95 $hash = self::hash($encoded);
97 // Then write it to the next free gap
98 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
105 * Writes given raw data to the file and returns a gap position and length
107 * @param $stackName Group identifier
108 * @param $hash Hash from encoded value
109 * @param $encoded Encoded value to be written to the file
110 * @return $data Gap position and length of the raw data
111 * @throws UnsupportedOperationException If this method is called
113 public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
114 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
115 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);