3 namespace Org\Mxchange\CoreFramework\Compressor\Bzip2;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Compressor\Compressor;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use \InvalidArgumentException;
13 * BZIP2 compression and decompression class
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
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.
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.
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/>.
34 class Bzip2Compressor extends BaseFrameworkSystem implements Compressor {
36 * Protected constructor
40 private function __construct () {
41 // Call parent constructor!
42 parent::__construct(__CLASS__);
46 * Create a new compressor channel based a given compression handler or NULL if the required PHP functions are not found.
48 * @return $compressorInstance An instance of this class or NULL if the required PHP functions are not found.
50 public static final function createBzip2Compressor () {
51 // Routines not found by default
52 $compressorInstance = NULL;
55 if ((function_exists('bzcompress')) && (function_exists('bzdecompress'))) {
56 // Compressor can maybe be used
57 $compressorInstance = new Bzip2Compressor();
60 // Return the compressor instance
61 return $compressorInstance;
65 * BZIP2 compression stream
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
71 public function compressStream (string $streamData) {
73 if (is_object($streamData) || is_resource($streamData)) {
75 throw new InvalidArgumentException(sprintf('streamData[]=%s cannot be compressed/decompressed', gettype($streamData)));
78 // Return the compressed stream
79 return bzcompress($streamData, 1);
83 * BZIP2 decompression stream
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
89 public function decompressStream (string $streamData) {
91 if (is_object($streamData) || is_resource($streamData)) {
93 throw new InvalidArgumentException(sprintf('streamData[]=%s cannot be compressed/decompressed', gettype($streamData)));
97 $streamData = bzdecompress($streamData, true);
99 // Return the decompressed stream
104 * Getter for the file extension of this compressor
106 * @return $string Returns always 'bz2'
108 public final function getCompressorExtension () {