]> git.mxchange.org Git - core.git/blob - framework/main/classes/compressor/class_Bzip2Compressor.php
Continued:
[core.git] / framework / main / classes / compressor / class_Bzip2Compressor.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Compressor\Bzip2;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Compressor\Compressor;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8
9 // Load SPL stuff
10 use \InvalidArgumentException;
11
12 /**
13  * BZIP2 compression and decompression class
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
18  * @license             GNU GPL 3.0 or any newer version
19  * @link                http://www.shipsimu.org
20  *
21  * This program is free software: you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation, either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program. If not, see <http://www.gnu.org/licenses/>.
33  */
34 class Bzip2Compressor extends BaseFrameworkSystem implements Compressor {
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         private function __construct () {
41                 // Call parent constructor!
42                 parent::__construct(__CLASS__);
43         }
44
45         /**
46          * Create a new compressor channel based a given compression handler or NULL if the required PHP functions are not found.
47          *
48          * @return      $compressorInstance             An instance of this class or NULL if the required PHP functions are not found.
49          */
50         public static final function createBzip2Compressor () {
51                 // Routines not found by default
52                 $compressorInstance = NULL;
53
54                 // Get new instance
55                 if ((function_exists('bzcompress')) && (function_exists('bzdecompress'))) {
56                         // Compressor can maybe be used
57                         $compressorInstance = new Bzip2Compressor();
58                 }
59
60                 // Return the compressor instance
61                 return $compressorInstance;
62         }
63
64         /**
65          * BZIP2 compression stream
66          *
67          * @param       $streamData             Mixed non-object stream data
68          * @return      $streamData             The compressed stream data
69          * @throws      InvalidArgumentException        If the stream is not compressable or decompressable
70          */
71         public function compressStream (string $streamData) {
72                 // Validate parameter
73                 if (is_object($streamData) || is_resource($streamData)) {
74                         // Throw an exception
75                         throw new InvalidArgumentException(sprintf('streamData[]=%s cannot be compressed/decompressed', gettype($streamData)));
76                 }
77
78                 // Return the compressed stream
79                 return bzcompress($streamData, 1);
80         }
81
82         /**
83          * BZIP2 decompression stream
84          *
85          * @param       $streamData             Mixed non-object stream data
86          * @return      $streamData             The decompressed stream data
87          * @throws      InvalidArgumentException        If the stream is not compressable or decompressable
88          */
89         public function decompressStream (string $streamData) {
90                 // Validate parameter
91                 if (is_object($streamData) || is_resource($streamData)) {
92                         // Throw an exception
93                         throw new InvalidArgumentException(sprintf('streamData[]=%s cannot be compressed/decompressed', gettype($streamData)));
94                 }
95
96                 // Decompress it
97                 $streamData = bzdecompress($streamData, true);
98
99                 // Return the decompressed stream
100                 return $streamData;
101         }
102
103         /**
104          * Getter for the file extension of this compressor
105          *
106          * @return      $string         Returns always 'bz2'
107          */
108         public final function getCompressorExtension () {
109                 return 'bz2';
110         }
111
112 }