3 namespace CoreFramework\Stream\Filesystem;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * An universal class for file input/output streams.
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.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 class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, FileOutputStreamer {
32 * File header indicator
34 const FILE_IO_FILE_HEADER_ID = '@head';
37 * Data block indicator
39 const FILE_IO_DATA_BLOCK_ID = '@data';
44 const FILE_IO_CHUNKER = ':';
49 const FILE_IO_SEPARATOR = '^';
52 * Protected constructor
54 protected function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Create a file IO stream. This is a class for performing all actions
61 * on files like creating, deleting and loading them.
63 * @return $ioInstance An instance of FileIoStream
65 public static final function createFileIoStream () {
66 // Create new instance
67 $ioInstance = new FileIoStream();
69 // Return the instance
74 * Saves data to a given local file and create missing directory structures
76 * @param $fileName The file name for the to be saved file
77 * @param $dataArray The data we shall store to the file
79 * @see FileOutputStreamer
80 * @todo This method needs heavy rewrite
82 public final function saveFile ($fileName, array $dataArray) {
87 for ($idx = 0; $idx < 5; $idx++) {
88 // Get a file output pointer
90 $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_output_class', array($fileName, 'wb'));
91 } catch (FileNotFoundException $e) {
93 ApplicationEntryPoint::app_exit('The application has made a fatal error. Exception: ' . $e->__toString() . ' with message: ' . $e->getMessage());
97 // Write a header information for validation purposes
98 $fileInstance->writeToFile(sprintf('%s%s%s%s%s%s%s%s%s' . PHP_EOL,
99 self::FILE_IO_FILE_HEADER_ID,
100 self::FILE_IO_SEPARATOR,
102 self::FILE_IO_CHUNKER,
104 self::FILE_IO_CHUNKER,
105 strlen($dataArray[1]),
106 self::FILE_IO_CHUNKER,
110 // Encode the (maybe) binary stream with Base64
111 $b64Stream = base64_encode($dataArray[1]);
113 // write the data line by line
114 $line = str_repeat(' ', 50); $idx = 0;
115 while (strlen($line) == 50) {
116 // Get 50 chars or less
117 $line = substr($b64Stream, $idx, 50);
119 // Save it to the stream
120 $fileInstance->writeToFile(sprintf('%s%s%s%s%s' . PHP_EOL,
121 self::FILE_IO_DATA_BLOCK_ID,
122 self::FILE_IO_SEPARATOR,
124 self::FILE_IO_CHUNKER,
128 // Advance to the next 50-chars block
133 unset($fileInstance);
137 * Reads from a local file
139 * @param $fqfn The full-qualified file-name which we shall load
140 * @return $array An array with the element 'header' and 'data'
141 * @see FileInputStreamer
143 public final function loadFileContents ($fqfn) {
144 // Initialize some variables and arrays
149 $readData = ''; // This will contain our read data
151 // Get a file input handler
152 $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', array($fqfn));
154 // Read all it's contents (we very and transparently decompress it below)
155 while ($readRawLine = $fileInstance->readFromFile()) {
156 // Add the read line to the buffer
157 $inputBuffer .= $readRawLine;
159 // Break infinite loop maybe caused by the input handler
160 if ($lastBuffer == $inputBuffer) {
164 // Remember last read line for avoiding possible infinite loops
165 $lastBuffer = $inputBuffer;
168 // Close directory handle
169 unset($fileInstance);
171 // Convert it into an array
172 $inputBuffer = explode(chr(10), $inputBuffer);
174 // Now process the read lines and verify it's content
175 foreach ($inputBuffer as $rawLine) {
176 // Trim it a little but not the leading spaces/tab-stops
177 $rawLine = rtrim($rawLine);
180 if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) {
181 // Header found, so let's extract it
182 $header = explode(self::FILE_IO_SEPARATOR, $rawLine);
183 $header = trim($header[1]);
185 // Now we must convert it again into an array
186 $header = explode(self::FILE_IO_CHUNKER, $header);
188 // Is the header (maybe) valid?
189 if (count($header) != 4) {
190 // Throw an exception
191 throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
193 } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) {
195 $data = explode(self::FILE_IO_SEPARATOR, $rawLine);
198 // First element is the data, second the MD5 checksum
199 $data = explode(self::FILE_IO_CHUNKER, $data);
201 // Validate the read line
202 if (count($data) == 2) {
203 if (md5($data[0]) != $data[1]) {
204 // MD5 hash did not match!
205 throw new InvalidMD5ChecksumException(array($this, md5($data[0]), $data[1]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH);
209 throw new InvalidArrayCountException(array($this, 'data', count($data), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
212 // Add this to the readData string
213 $readData .= $data[0];
215 // Other raw lines than header/data tagged lines and re-add the new-line char
216 $readData .= $rawLine . PHP_EOL;
220 // Was raw lines read and no header/data?
221 if ((!empty($readData)) && (count($header) == 0) && (count($data) == 0)) {
222 // Return raw lines back
226 // Was a header found?
227 if (count($header) != 4) {
228 // Throw an exception
229 throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
232 // Decode all from Base64
233 $readData = @base64_decode($readData);
235 // Does the size match?
236 if (strlen($readData) != $header[2]) {
237 // Size did not match
238 throw new InvalidDataLengthException(array($this, strlen($readData), $header[2]), self::EXCEPTION_UNEXPECTED_STRING_SIZE);
241 // Validate the decoded data with the final MD5 hash
242 if (md5($readData) != $header[3]) {
243 // MD5 hash did not match!
244 throw new InvalidMD5ChecksumException(array($this, md5($readData), $header[3]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH);
247 // Return all in an array
255 * Streams the data and maybe does something to it
257 * @param $data The data (string mostly) to "stream"
258 * @return $data The data (string mostly) to "stream"
259 * @throws UnsupportedOperationException If this method is called
261 public function streamData ($data) {
262 self::createDebugInstance(__CLASS__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
263 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
267 * Determines seek position
269 * @return $seekPosition Current seek position
272 public function determineSeekPosition () {
273 $this->partialStub();
277 * Seek to given offset (default) or other possibilities as fseek() gives.
279 * @param $offset Offset to seek to (or used as "base" for other seeks)
280 * @param $whence Added to offset (default: only use offset to seek to)
281 * @return $status Status of file seek: 0 = success, -1 = failed
283 public function seek ($offset, $whence = SEEK_SET) {
284 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
290 * @return $size Size (in bytes) of file
292 public function size () {
293 $this->partialStub();