mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-30 04:19:23 +01:00
Implement GzipOutputStream, closes #5
This commit is contained in:
parent
adc2de34c0
commit
f89c6a1161
68
lib/GzipOutputStream.php
Normal file
68
lib/GzipOutputStream.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\ByteStream;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
class GzipOutputStream implements OutputStream {
|
||||
private $destination;
|
||||
private $resource;
|
||||
|
||||
public function __construct(OutputStream $destination) {
|
||||
$this->destination = $destination;
|
||||
$this->resource = \deflate_init(\ZLIB_ENCODING_GZIP);
|
||||
|
||||
if ($this->resource === false) {
|
||||
throw new StreamException("Failed initializing deflate context");
|
||||
}
|
||||
}
|
||||
|
||||
public function write(string $data): Promise {
|
||||
if ($this->resource === null) {
|
||||
throw new ClosedException("The stream has already been closed");
|
||||
}
|
||||
|
||||
$compressed = \deflate_add($this->resource, $data, \ZLIB_SYNC_FLUSH);
|
||||
|
||||
if ($compressed === false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
|
||||
$promise = $this->destination->write($compressed);
|
||||
$promise->onResolve(function ($error) {
|
||||
if ($error) {
|
||||
$this->close();
|
||||
}
|
||||
});
|
||||
|
||||
return $promise;
|
||||
}
|
||||
|
||||
public function end(string $finalData = ""): Promise {
|
||||
if ($this->resource === null) {
|
||||
throw new ClosedException("The stream has already been closed");
|
||||
}
|
||||
|
||||
$compressed = \deflate_add($this->resource, $finalData, \ZLIB_FINISH);
|
||||
|
||||
if ($compressed === false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
|
||||
$promise = $this->destination->write($compressed);
|
||||
$promise->onResolve(function ($error) {
|
||||
if ($error) {
|
||||
$this->close();
|
||||
}
|
||||
});
|
||||
|
||||
return $promise;
|
||||
}
|
||||
|
||||
public function close() {
|
||||
$this->resource = null;
|
||||
$this->destination = null;
|
||||
|
||||
$this->destination->close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user