3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Filesystem;
9 * A virtual file system interface
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 interface BinaryFile extends Filesystem {
32 * Separator for header data
34 const SEPARATOR_HEADER_DATA = 0x01;
37 * Separator header->entries
39 const SEPARATOR_HEADER_ENTRIES = 0x02;
42 * Separator group->hash
44 const SEPARATOR_GROUP_HASH = 0x03;
47 * Separator hash->value
49 const SEPARATOR_HASH_VALUE = 0x04;
52 * Separator entry->entry
54 const SEPARATOR_ENTRIES = 0x05;
57 * Separator type->position
59 const SEPARATOR_TYPE_POSITION = 0x06;
64 const LENGTH_COUNT = 20;
69 const LENGTH_POSITION = 20;
74 const LENGTH_GROUP = 10;
77 * Maximum length of entry type
79 const LENGTH_TYPE = 20;
81 //***** Array elements for 'gaps' array *****
86 const GAPS_INDEX_START = 'start';
91 const GAPS_INDEX_END = 'end';
94 const HEADER_NAME_MAGIC = 'magic';
95 const HEADER_NAME_TOTAL_ENTRIES = 'total';
96 const HEADER_NAME_SEEK_POSITION = 'seek';
98 // Header element counts
99 const HEADER_INDEX_ELEMENT_COUNT = 2;
100 const HEADER_STACK_ELEMENT_COUNT = 3;
103 * Reads the file header
107 function readFileHeader ();
110 * Flushes the file header
114 function flushFileHeader ();
117 * Determines whether the EOF has been reached
119 * @return $isEndOfFileReached Whether the EOF has been reached
121 function isEndOfFileReached ();
124 * Getter for header size
126 * @return $totalEntries Size of file header
128 function getHeaderSize ();
131 * Setter for header size
133 * @param $headerSize Size of file header
136 function setHeaderSize (int $headerSize);
139 * Getter for header array
141 * @return $totalEntries Size of file header
143 function getHeader ();
148 * @param $header Array for a file header
151 function setHeader (array $header);
154 * Initializes counter for valid entries, arrays for damaged entries and
155 * an array for gap seek positions. If you call this method on your own,
156 * please re-analyze the file structure. So you are better to call
157 * analyzeFileStructure() instead of this method.
161 function initCountersGapsArray ();
164 * Updates seekPosition attribute from file to avoid to much access on file.
168 function updateSeekPosition ();
171 * Getter for total entries
173 * @return $totalEntries Total entries in this file
175 function getCounter ();
178 * "Getter" for file size
180 * @return $fileSize Size of currently loaded file
182 function getFileSize ();
185 * Checks whether the abstracted file only contains gaps by counting all
186 * gaps' bytes together and compare it to total length.
188 * @return $isGapsOnly Whether the abstracted file only contains gaps
190 function isFileGapsOnly();
193 * Searches for next suitable gap the given length of data can fit in
194 * including padding bytes.
196 * @param $length Length of raw data
197 * @return $seekPosition Found next gap's seek position
199 function searchNextGap (int $length);
202 * Writes given value to the file and returns a hash and gap position for it
204 * @param $groupId Group identifier
205 * @param $value Value to be added to the stack
206 * @return $data Hash and gap position
208 function writeValueToFile (string $groupId, string $rawData);
211 * Writes given raw data to the file and returns a gap position and length
213 * @param $groupId Group identifier
214 * @param $hash Hash from encoded value
215 * @param $encoded Encoded value to be written to the file
216 * @return $data Gap position and length of the raw data
218 function writeDataToFreeGap (string $groupId, string $hash, string $encoded);
221 * Writes data at given position
223 * @param $seekPosition Seek position
224 * @param $data Data to be written
225 * @param $flushHeader Whether to flush the header (default: flush)
228 function writeData (int $seekPosition, string $data, bool $flushHeader = true);
231 * Writes at given position by seeking to it.
233 * @param $seekPosition Seek position in file
234 * @param $dataStream Data to be written
235 * @return mixed Number of writes bytes or false on error
236 * @throws InvalidArgumentException If a parameter is not valid
238 function writeAtPosition (int $seekPosition, string $dataStream);