X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FileIoStream.php;h=98af0428497b72caa043638f1b383d200d14ff0b;hb=fc5010729854b1f9c9bc60249460d092ea6694b6;hp=45f1021ec52d35e183c66135a6f1614b71344f7c;hpb=4b88c118b615335d06bd74e444173d21aef4406c;p=core.git diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/class_FileIoStream.php index 45f1021e..98af0428 100644 --- a/inc/classes/main/io/class_FileIoStream.php +++ b/inc/classes/main/io/class_FileIoStream.php @@ -25,22 +25,22 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil /** * File header indicator */ - private $fileHeader = '@head'; + const FILE_IO_FILE_HEADER_ID = '@head'; /** * Data block indicator */ - private $dataBlock = '@data'; + const FILE_IO_DATA_BLOCK_ID = '@data'; /** * Seperator #1 */ - private $chunker = ':'; + const FILE_IO_CHUNKER = ':'; /** * Seperator #2 */ - private $seperator = '^'; + const FILE_IO_SEPERATOR = '^'; /** * Protected constructor @@ -75,7 +75,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil */ public final function saveFile ($fileName, $dataArray) { // Try it five times - $dirName = ''; $fileInstance = null; + $dirName = ''; $fileInstance = NULL; for ($idx = 0; $idx < 5; $idx++) { // Get a file output pointer try { @@ -94,14 +94,14 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil // Write a header information for validation purposes $fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n", - $this->fileHeader, - $this->seperator, + self::FILE_IO_FILE_HEADER_ID, + self::FILE_IO_SEPERATOR, $dataArray[0], - $this->chunker, + self::FILE_IO_CHUNKER, time(), - $this->chunker, + self::FILE_IO_CHUNKER, strlen($dataArray[1]), - $this->chunker, + self::FILE_IO_CHUNKER, md5($dataArray[1]) )); @@ -116,10 +116,10 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil // Save it to the stream $fileInstance->writeToFile(sprintf("%s%s%s%s%s\n", - $this->dataBlock, - $this->seperator, + self::FILE_IO_DATA_BLOCK_ID, + self::FILE_IO_SEPERATOR, $line, - $this->chunker, + self::FILE_IO_CHUNKER, md5($line) )); @@ -159,13 +159,13 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil // Remember last read line for avoiding possible infinite loops $lastBuffer = $inputBuffer; - } + } // END - while // Close directory handle $fileInstance->closeFile(); // Convert it into an array - $inputBuffer = explode("\n", $inputBuffer); + $inputBuffer = explode(chr(10), $inputBuffer); // Now process the read lines and verify it's content foreach ($inputBuffer as $rawLine) { @@ -173,33 +173,33 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $rawLine = rtrim($rawLine); // Analyze this line - if (substr($rawLine, 0, 5) == $this->fileHeader) { + if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) { // Header found, so let's extract it - $header = explode($this->seperator, $rawLine); + $header = explode(self::FILE_IO_SEPERATOR, $rawLine); $header = trim($header[1]); // Now we must convert it again into an array - $header = explode($this->chunker, $header); + $header = explode(self::FILE_IO_CHUNKER, $header); // Is the header (maybe) valid? if (count($header) != 4) { // Throw an exception throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT); - } - } elseif (substr($rawLine, 0, 5) == $this->dataBlock) { + } // END - if + } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) { // Is a data line! - $data = explode($this->seperator, $rawLine); + $data = explode(self::FILE_IO_SEPERATOR, $rawLine); $data = $data[1]; // First element is the data, second the MD5 checksum - $data = explode($this->chunker, $data); + $data = explode(self::FILE_IO_CHUNKER, $data); // Validate the read line if (count($data) == 2) { if (md5($data[0]) != $data[1]) { // MD5 hash did not match! throw new InvalidMD5ChecksumException(array($this, md5($data[0]), $data[1]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH); - } + } // END - if } else { // Invalid count! throw new InvalidArrayCountException(array($this, "data", count($data), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT); @@ -209,21 +209,21 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $readData .= $data[0]; } else { // Other raw lines than header/data tagged lines and re-add the new-line char - $readData .= $rawLine . "\n"; + $readData .= $rawLine . chr(10); } - } + } // END - foreach // Was raw lines read and no header/data? if ((!empty($readData)) && (count($header) == 0) && (count($data) == 0)) { // Return raw lines back return $readData; - } + } // END - if // Was a header found? if (count($header) != 4) { // Throw an exception throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT); - } + } // END - if // Decode all from Base64 $readData = @base64_decode($readData); @@ -232,13 +232,13 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil if (strlen($readData) != $header[2]) { // Size did not match throw new InvalidDataLengthException(array($this, strlen($readData), $header[2]), self::EXCEPTION_UNEXPECTED_STRING_SIZE); - } + } // END - if // Validate the decoded data with the final MD5 hash if (md5($readData) != $header[3]) { // MD5 hash did not match! throw new InvalidMD5ChecksumException(array($this, md5($readData), $header[3]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH); - } + } // END - if // Return all in an array return array( @@ -246,6 +246,18 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil 'data' => $readData ); } + + /** + * Streams the data and maybe does something to it + * + * @param $data The data (string mostly) to "stream" + * @return $data The data (string mostly) to "stream" + * @throws UnsupportedOperationException If this method is called + */ + public function streamData ($data) { + $this->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.'); + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } } // [EOF]