Initial import of current development status
[shipsimu.git] / ship-simu / inc / classes / main / compressor / class_GzipCompressor.php
1 <?php
2 /**
3  * GZIP compression and decompression class
4  */
5 class GzipCompressor extends BaseFrameworkSystem implements Compressor {
6         /**
7          * Private constructor
8          *
9          * @return      void
10          */
11         private function __construct () {
12                 // Call parent constructor!
13                 parent::constructor(__CLASS__);
14
15                 // Debug message
16                 if (((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
17                         $this->__toString()
18                 ));
19
20                 // Set description
21                 $this->setPartDescr("GZIP-Kompressor");
22
23                 // Create an unique ID
24                 $this->createUniqueID();
25         }
26
27         /**
28          * Create a new compressor channel based a given compression handler
29          *
30          * @return      $cInstance      An instance of this class
31          */
32         public final static function createGzipCompressor () {
33                 // Get new instance
34                 if ((function_exists('gzcompress')) && (function_exists('gzuncompress'))) {
35                         // Compressor can maybe be used
36                         $cInstance = new GzipCompressor();
37
38                         // Debug message
39                         if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] GZIP-Kompression wird verwendet.<br />\n",
40                                 $cInstance->__toString()
41                         ));
42                 } else {
43                         // Routines not found!
44                         $cInstance = null;
45
46                         // Debug message
47                         if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] GZIP-Kompressionsroutinen <strong>nicht</strong> gefunden.<br />\n",
48                                 $cInstance->__toString()
49                         ));
50                 }
51
52                 // Return the compressor instance
53                 return $cInstance;
54         }
55
56         /**
57          * GZIP compression stream
58          *
59          * @param               $streamData                     Mixed non-object stream data
60          * @return      $streamData                     The compressed stream data      
61          * @throws      InvalidObjectException  If the stream is an object
62          */
63         public function compressStream ($streamData) {
64                 if (is_object($streamData)) {
65                         // Throw an exception
66                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
67                 }
68
69                 // Return the compressed stream
70                 return gzcompress($streamData, 1);
71         }
72
73         /**
74          * GZIP decompression stream
75          *
76          * @param               $streamData                     Mixed non-object stream data
77          * @return      $streamData                     The decompressed stream data    
78          * @throws      InvalidObjectException  If the stream is an object
79          */
80         public function decompressStream ($streamData) {
81                 if (is_object($streamData)) {
82                         // Throw an exception
83                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
84                 }
85
86                 // Return the decompressed stream
87                 return gzuncompress($streamData);
88         }
89
90         /**
91          * Getter for the file extension of this compressor
92          *
93          * @return      $string Returns always "gz"
94          */
95         public final function getCompressorExtension () {
96                 if ((defined('DEBUG_COMPRESSOR')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:] Dateierweiterung angefordert.<br />\n",
97                         $this->__toString()
98                 ));
99                 return "gz";
100         }
101 }
102
103 // [EOF]
104 ?>