]> git.mxchange.org Git - hub.git/blob - application/hub/main/streams/raw_data/input/class_RawDataInputStream.php
a708c984e523dad38e92ee591a0162665618f1fa
[hub.git] / application / hub / main / streams / raw_data / input / class_RawDataInputStream.php
1 <?php
2 /**
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
6  * "alphabet").
7  *
8  * Since the latest refacturing this class works "handler-less".
9  *
10  * @author              Roland Haeder <webmaster@ship-simu.org>
11  * @version             0.0.0
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.ship-simu.org
15  *
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.
20  *
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.
25  *
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/>.
28  */
29 class RawDataInputStream extends BaseStream implements InputStreamable {
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this node class
42          *
43          * @return      $streamInstance         An instance of this node class
44          */
45         public final static function createRawDataInputStream () {
46                 // Get a new instance
47                 $streamInstance = new RawDataInputStream();
48
49                 // Return the instance
50                 return $streamInstance;
51         }
52
53         /**
54          * Streams the data and maybe does something to it
55          *
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          */
62         public function streamData ($data) {
63                 // Do we have start and end marker again?
64                 assert($this->ifStartEndMarkersSet($data));
65
66                 // Remove both
67                 $data = substr($data, strlen(BaseRawDataHandler::STREAM_START_MARKER), -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER));
68
69                 // Can it be validated?
70                 if ((strlen($data) % 4) != 0) {
71                         // Length modulo 4 must be zero, else it is an invalid Base64 message
72                         throw new Base64EncodingModuloException(array($this, $data), self::EXCEPTION_BASE64_ENCODING_NOT_MODULO_4);
73                 } elseif (!$this->isBase64Encoded($data)) {
74                         // Is not a valid Base64-encoded message
75                         throw new Base64EncodingBadException(array($this, $data), self::EXCEPTION_BASE64_BAD_ENCODING);
76                 } else {
77                         // Decode the data with BASE64-encoding
78                         $data = base64_decode($data);
79                 }
80
81                 // Debug message
82                 //* NOISY-DEBUG: */ $this->debugOutput('RAW-INPUT-STREAM: Length of data is now ' . strlen($data) . ' Bytes.');
83
84                 // Return it
85                 return $data;
86         }
87 }
88
89 // [EOF]
90 ?>