56e704c3d69bbd64a7e5d343c0860258c15175a8
[shipsimu.git] / inc / classes / main / compressor / class_Bzip2Compressor.php
1 <?php
2 /**
3  * BZIP2 compression and decompression class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 class Bzip2Compressor extends BaseFrameworkSystem implements Compressor {
24         /**
25          * Private constructor
26          *
27          * @return      void
28          */
29         private function __construct () {
30                 // Call parent constructor!
31                 parent::constructor(__CLASS__);
32
33                 // Debug message
34                 if (((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
35                         $this->__toString()
36                 ));
37
38                 // Set description
39                 $this->setPartDescr("BZIP2-Kompressor");
40
41                 // Create an unique ID
42                 $this->createUniqueID();
43         }
44
45         /**
46          * Create a new compressor channel based a given compression handler
47          *
48          * @return      $cInstance      An instance of this class
49          */
50         public final static function createBzip2Compressor () {
51                 // Get new instance
52                 if ((function_exists('bzcompress')) && (function_exists('bzdecompress'))) {
53                         // Compressor can maybe be used
54                         $cInstance = new Bzip2Compressor();
55
56                         // Debug message
57                         if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] BZIP2-Kompression wird verwendet.<br />\n",
58                                 $cInstance->__toString()
59                         ));
60                 } else {
61                         // Routines not found!
62                         $cInstance = null;
63
64                         // Debug message
65                         if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] BZIP2-Kompressionsroutinen <strong>nicht</strong> gefunden.<br />\n",
66                                 $cInstance->__toString()
67                         ));
68                 }
69
70                 // Return the compressor instance
71                 return $cInstance;
72         }
73
74         /**
75          * BZIP2 compression stream
76          *
77          * @param               $streamData                     Mixed non-object stream data
78          * @return      $streamData                     The compressed stream data      
79          * @throws      InvalidObjectException  If the stream is an object
80          */
81         public function compressStream ($streamData) {
82                 if (is_object($streamData)) {
83                         // Throw an exception
84                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
85                 }
86
87                 // Return the compressed stream
88                 return bzcompress($streamData, 1);
89         }
90
91         /**
92          * BZIP2 decompression stream
93          *
94          * @param               $streamData                     Mixed non-object stream data
95          * @return      $streamData                     The decompressed stream data    
96          * @throws      InvalidObjectException  If the stream is an object
97          */
98         public function decompressStream ($streamData) {
99                 if (is_object($streamData)) {
100                         // Throw an exception
101                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
102                 }
103
104                 // Return the decompressed stream
105                 return bzdecompress($streamData);
106         }
107
108         /**
109          * Getter for the file extension of this compressor
110          *
111          * @return      $string Returns always "bz2"
112          */
113         public final function getCompressorExtension () {
114                 if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:] Dateierweiterung angefordert.<br />\n",
115                         $this->__toString()
116                 ));
117                 return "bz2";
118         }
119 }
120
121 // [EOF]
122 ?>