3 * A class for handling incoming (encoded) raw data with start and end markers.
4 * The "stream" is being verified by its length (if modulo 4 of it is always
5 * zero) and if the "stream" contains all valid characters (the BASE64
8 * Since the latest refacturing this class works "handler-less".
10 * @author Roland Haeder <webmaster@shipsimu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Developer Team
13 * @license GNU GPL 3.0 or any newer version
14 * @link http://www.shipsimu.org
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 class RawDataInputStream extends BaseStream implements InputStream {
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this node class
43 * @return $streamInstance An instance of this node class
45 public final static function createRawDataInputStream () {
47 $streamInstance = new RawDataInputStream();
49 // Return the instance
50 return $streamInstance;
54 * Streams the data and maybe does something to it
56 * @param $data The data (string mostly) to "stream"
57 * @return $data The data (string mostly) to "stream"
58 * @todo Do we need to do something more here?
59 * @throws Base64EncodingModuloException If the data's length modulo 4 is not zero
60 * @throws Base64EncodingBadException If the data contains characters which are not in the "alphabet" of BASE64 messages.
61 * @throws MultipleMessageSentException If the sender has sent two messages and both end up here
63 public function streamData ($data) {
65 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RAW-INPUT-STREAM[' . __METHOD__ . ':' . __LINE__ . ': data(' . strlen($data) . ')=' . $data);
67 // Do we have start and end marker again?
68 assert($this->ifStartEndMarkersSet($data));
70 // Count of start and end markers must be the same
71 assert(substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) == substr_count($data, BaseRawDataHandler::STREAM_END_MARKER));
73 // Check if more than two start markers exist and if so, split it.
74 if (substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) > 1) {
75 // Please do it outside this method
76 throw new MultipleMessageSentException(array($this, $data), BaseHubSystem::EXCEPTION_MULTIPLE_MESSAGE_SENT);
80 $data = substr($data, strlen(BaseRawDataHandler::STREAM_START_MARKER), -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER));
82 // Can it be validated?
83 if ((strlen($data) % 4) != 0) {
84 // Length modulo 4 must be zero, else it is an invalid Base64 message
85 throw new Base64EncodingModuloException(array($this, $data), BaseHubSystem::EXCEPTION_BASE64_ENCODING_NOT_MODULO_4);
86 } elseif (!$this->isBase64Encoded($data)) {
87 // Is not a valid Base64-encoded message
88 throw new Base64EncodingBadException(array($this, $data), BaseHubSystem::EXCEPTION_BASE64_BAD_ENCODING);
90 // Decode the data with BASE64-encoding
91 $data = base64_decode($data);
95 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RAW-INPUT-STREAM: Length of data is now ' . strlen($data) . ' Bytes.');