*
* @author Roland Haeder <webmaster@ship-simu.org>
* @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
*
/**
* 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
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
-
- // Clean-up a little
- $this->removeNumberFormaters();
- $this->removeSystemArray();
}
/**
*
* @return $ioInstance An instance of FileIoStream
*/
- public final static function createFileIoStream () {
+ public static final function createFileIoStream () {
// Create new instance
$ioInstance = new FileIoStream();
*/
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++) {
// 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])
));
// 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)
));
// 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) {
$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);
$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);
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(
'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]