class-basebytereadstream.php
6 days ago
class-deflatereadstream.php
6 days ago
class-filereadstream.php
6 days ago
class-inflatereadstream.php
6 days ago
class-limitedbytereadstream.php
6 days ago
class-transformedreadstream.php
6 days ago
interface-bytereadstream.php
6 days ago
class-deflatereadstream.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\ReadStream; |
| 4 | |
| 5 | use Exception; |
| 6 | |
| 7 | class DeflateReadStream extends BaseByteReadStream { |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * The offset of the underlying reader at the time of the first read |
| 12 | * from the DeflateReader. |
| 13 | * |
| 14 | * @var int|null |
| 15 | */ |
| 16 | protected $delegate_offset_0; |
| 17 | protected $deflate_context; |
| 18 | |
| 19 | protected $deflate_encoding; |
| 20 | protected $upstream; |
| 21 | |
| 22 | public function __construct( ByteReadStream $upstream, $encoding = ZLIB_ENCODING_DEFLATE ) { |
| 23 | $this->deflate_encoding = $encoding; |
| 24 | $this->deflate_init(); |
| 25 | $this->upstream = $upstream; |
| 26 | } |
| 27 | |
| 28 | protected function internal_pull( $n ): string { |
| 29 | if ( null === $this->delegate_offset_0 ) { |
| 30 | $this->delegate_offset_0 = $this->upstream->tell(); |
| 31 | } |
| 32 | |
| 33 | if ( ! $this->deflate_context ) { |
| 34 | return ''; |
| 35 | } |
| 36 | |
| 37 | if ( $this->upstream->reached_end_of_data() ) { |
| 38 | $bytes = deflate_add( $this->deflate_context, '', ZLIB_FINISH ); |
| 39 | $this->deflate_context = null; |
| 40 | |
| 41 | return $bytes; |
| 42 | } |
| 43 | |
| 44 | $inflated = $this->upstream->peek( $n ); |
| 45 | if ( ! strlen( $inflated ) ) { |
| 46 | $this->upstream->pull( $n ); |
| 47 | $inflated = $this->upstream->peek( $n ); |
| 48 | } |
| 49 | $this->upstream->consume( strlen( $inflated ) ); |
| 50 | |
| 51 | return deflate_add( $this->deflate_context, $inflated ); |
| 52 | } |
| 53 | |
| 54 | public function length(): ?int { |
| 55 | // The length of the deflated stream is unknown until the stream is closed. |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | protected function internal_close_reading(): void { |
| 60 | $this->deflate_context = null; |
| 61 | } |
| 62 | |
| 63 | protected function internal_reached_end_of_data(): bool { |
| 64 | return null === $this->deflate_context && $this->upstream->reached_end_of_data(); |
| 65 | } |
| 66 | |
| 67 | protected function seek_outside_of_buffer( $target_offset ): void { |
| 68 | if ( $target_offset < $this->tell() ) { |
| 69 | $this->buffer = ''; |
| 70 | $this->bytes_already_forgotten = 0; |
| 71 | $this->offset_in_current_buffer = 0; |
| 72 | |
| 73 | $this->deflate_init(); |
| 74 | $this->upstream->seek( $this->delegate_offset_0 ?? 0 ); |
| 75 | } |
| 76 | |
| 77 | while ( $this->tell() < $target_offset ) { |
| 78 | $remaining_bytes = $target_offset - $this->tell(); |
| 79 | $next_chunk_size = min( 50 * 1024, $remaining_bytes ); |
| 80 | $pulled = $this->pull( $next_chunk_size ); |
| 81 | // Keep skipping bytes until we've consumed enough. |
| 82 | $this->consume( min( $remaining_bytes, $pulled ) ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private function deflate_init() { |
| 87 | $this->deflate_context = deflate_init( $this->deflate_encoding ); |
| 88 | if ( ! $this->deflate_context ) { |
| 89 | throw new Exception( 'Failed to initialize deflate context' ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 |