source = $stream; $this->capturingSource = new ReadFailureCapturingStream($stream); $inflated = self::inflate($this->capturingSource); $this->stream = $stream->isSeekable() ? $inflated : new NoSeekStream($inflated); } public function eof(): bool { return ($this->capturingSource === null || !$this->capturingSource->hasReadFailure()) && $this->stream->eof(); } public function seek(int $offset, int $whence = SEEK_SET): void { $this->stream->seek($offset, $whence); if ($offset === 0 && $whence === SEEK_SET && $this->capturingSource !== null) { $this->capturingSource->takeReadFailure(); $this->stream = self::inflate($this->capturingSource); } } public function read(int $length): string { if ($length <= 0 || $this->source === null || $this->capturingSource === null) { return $this->stream->read($length); } // A source failure that follows decoded bytes is reported by the next read. $failure = $this->capturingSource->takeReadFailure(); if ($failure === null) { try { $data = $this->stream->read($length); } catch (TimeoutException $e) { throw $e; } catch (\RuntimeException $e) { if (StreamTimeout::isReadTimedOut($this->source)) { throw new TimeoutException('Unable to read from stream: timed out', 0, $e); } throw $e; } if ($data !== '') { return $data; } $failure = $this->capturingSource->takeReadFailure(); } if ($failure instanceof TimeoutException || StreamTimeout::isReadTimedOut($this->source)) { throw new TimeoutException('Unable to read from stream: timed out', 0, $failure); } if ($failure !== null) { throw new \RuntimeException('Unable to read from stream', 0, $failure); } return ''; } public function close(): void { $source = $this->source; $this->source = null; $this->capturingSource = null; $exception = null; try { $this->stream->close(); } catch (\Throwable $e) { $exception = $e; } if ($source !== null) { try { $source->close(); } catch (\Throwable $e) { if ($exception === null) { $exception = $e; } } } if ($exception !== null) { throw $exception; } } public function detach() { $this->source = null; if ($this->capturingSource !== null) { $this->capturingSource->stopCapturing(); $this->capturingSource = null; } return $this->stream->detach(); } private static function inflate(StreamInterface $stream): Stream { $resource = StreamWrapper::getResource($stream); // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" // Default window size is 15. stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 15 + 32]); return new Stream($resource); } }