X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FileIoStream.php;h=e1ccd8aa6333984e30e42995bcbfa3bbb87aeecd;hp=457c51f6d155782c8fddb55320fd2616d1bdf736;hb=f97af6b5b50bc961ec5ad850b73765d64811a5bc;hpb=0cd57c3885f00ad77fc599e53ed2f2d5e7ac267f diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/class_FileIoStream.php index 457c51f6..e1ccd8aa 100644 --- a/inc/classes/main/io/class_FileIoStream.php +++ b/inc/classes/main/io/class_FileIoStream.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -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 + * Separator #1 */ - private $chunker = ':'; + const FILE_IO_CHUNKER = ':'; /** - * Seperator #2 + * Separator #2 */ - private $seperator = '^'; + const FILE_IO_SEPARATOR = '^'; /** * Protected constructor @@ -48,10 +48,6 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); - - // Clean-up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** @@ -60,7 +56,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil * * @return $ioInstance An instance of FileIoStream */ - public final static function createFileIoStream () { + public static final function createFileIoStream () { // Create new instance $ioInstance = new FileIoStream(); @@ -79,12 +75,12 @@ 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 { $fileInstance = FrameworkFileOutputPointer::createFrameworkFileOutputPointer($fileName, 'w'); - } catch (FilePointerNotOpenedException $e) { + } catch (FileIoException $e) { // Create missing directory $dirName = dirname($fileName); for ($idx2 = 0; $idx2 < (2 - $idx); $idx2++) { @@ -98,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_SEPARATOR, $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]) )); @@ -120,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_SEPARATOR, $line, - $this->chunker, + self::FILE_IO_CHUNKER, md5($line) )); @@ -163,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) { @@ -177,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_SEPARATOR, $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_SEPARATOR, $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); @@ -213,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); @@ -236,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( @@ -250,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]