Renamed 'seperator' to 'separator'
[core.git] / inc / classes / main / io / class_FileIoStream.php
index 278d6d7d06fd71552af33da7bd650057685e18c8..e1ccd8aa6333984e30e42995bcbfa3bbb87aeecd 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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
@@ -56,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();
 
@@ -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_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])
                ));
 
@@ -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_SEPARATOR,
                                $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_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);
@@ -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]