X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FileIoStream.php;h=e1ccd8aa6333984e30e42995bcbfa3bbb87aeecd;hb=f97af6b5b50bc961ec5ad850b73765d64811a5bc;hp=b4a8548062f007f29c69f14ed07c6cbef0ef7861;hpb=3107989f93cfb5808ce9d75f1c7d2b7ee3d83d18;p=core.git diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/class_FileIoStream.php index b4a85480..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 - 2009 Roland Haeder, this is free software + * @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 * @@ -22,16 +22,32 @@ * along with this program. If not, see . */ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, FileOutputStreamer { + /** + * File header indicator + */ + const FILE_IO_FILE_HEADER_ID = '@head'; + + /** + * Data block indicator + */ + const FILE_IO_DATA_BLOCK_ID = '@data'; + + /** + * Separator #1 + */ + const FILE_IO_CHUNKER = ':'; + + /** + * Separator #2 + */ + const FILE_IO_SEPARATOR = '^'; + /** * Protected constructor */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); - - // Clean-up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** @@ -40,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(); @@ -49,36 +65,43 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil } /** - * Saves data to a given local file + * Saves data to a given local file and create missing directory structures * - * @param $fileName The file name for the to be saved file - * @param $dataArray The data we shall store to the file + * @param $fileName The file name for the to be saved file + * @param $dataArray The data we shall store to the file * @return void * @see FileOutputStreamer + * @todo This method needs heavy rewrite */ 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++) { $dirName = dirname($dirName); - } + } // END - for + // Try to create it @mkdir($dirName); } - } + } // END - for // Write a header information for validation purposes - $fileInstance->writeToFile(sprintf("@head^%s:%s:%s:%s\n", + $fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n", + self::FILE_IO_FILE_HEADER_ID, + self::FILE_IO_SEPARATOR, $dataArray[0], + self::FILE_IO_CHUNKER, time(), + self::FILE_IO_CHUNKER, strlen($dataArray[1]), + self::FILE_IO_CHUNKER, md5($dataArray[1]) )); @@ -86,20 +109,23 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $b64Stream = base64_encode($dataArray[1]); // write the data line by line - $line = str_repeat(" ", 50); $idx = 0; + $line = str_repeat(' ', 50); $idx = 0; while (strlen($line) == 50) { // Get 50 chars or less $line = substr($b64Stream, $idx, 50); // Save it to the stream - $fileInstance->writeToFile(sprintf("@data^%s:%s\n", + $fileInstance->writeToFile(sprintf("%s%s%s%s%s\n", + self::FILE_IO_DATA_BLOCK_ID, + self::FILE_IO_SEPARATOR, $line, + self::FILE_IO_CHUNKER, md5($line) )); // Advance to the next 50-chars block $idx += 50; - } + } // END - while // Close the file $fileInstance->closeFile(); @@ -133,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) { @@ -147,33 +173,33 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $rawLine = rtrim($rawLine); // Analyze this line - if (substr($rawLine, 0, 5) == "@head") { + if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) { // Header found, so let's extract it - $header = explode("^", $rawLine); + $header = explode(self::FILE_IO_SEPARATOR, $rawLine); $header = trim($header[1]); // Now we must convert it again into an array - $header = explode(":", $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) == "@data") { + } // END - if + } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) { // Is a data line! - $data = explode("^", $rawLine); + $data = explode(self::FILE_IO_SEPARATOR, $rawLine); $data = $data[1]; // First element is the data, second the MD5 checksum - $data = explode(":", $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); @@ -183,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); @@ -206,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( @@ -220,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]