ByteTransformer
6 days ago
ReadStream
6 days ago
WriteStream
6 days ago
LICENSE.md
6 days ago
README.md
6 days ago
class-bytepipe.php
6 days ago
class-bytestreamexception.php
6 days ago
class-filereadwritestream.php
6 days ago
class-memorypipe.php
6 days ago
class-notenoughdataexception.php
6 days ago
composer.json
6 days ago
class-memorypipe.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream; |
| 4 | |
| 5 | use WordPress\ByteStream\ReadStream\BaseByteReadStream; |
| 6 | use WordPress\ByteStream\WriteStream\ByteWriteStream; |
| 7 | |
| 8 | class MemoryPipe extends BaseByteReadStream implements ByteWriteStream { |
| 9 | |
| 10 | protected $is_writing_closed; |
| 11 | |
| 12 | public function __construct( ?string $bytes = null, $expected_length = null ) { |
| 13 | if ( null !== $bytes && strlen( $bytes ) > 0 && null !== $expected_length ) { |
| 14 | throw new ByteStreamException( 'A MemoryPipe accepts either a non-empty string representing the entire data, or an expected length when the data is not available yet. It does not accept both arguments.' ); |
| 15 | } |
| 16 | if ( null !== $bytes ) { |
| 17 | $this->buffer = $bytes; |
| 18 | $this->expected_length = strlen( $bytes ); |
| 19 | // If we have a full buffer, it's already in memory and we don't need |
| 20 | // to clean up old data as we stream it. |
| 21 | // If we did clean up old data, we would lose the ability to seek() to. |
| 22 | // the beginning of the buffer. |
| 23 | $this->max_lookbehind_bytes = PHP_INT_MAX; |
| 24 | } elseif ( null !== $expected_length ) { |
| 25 | $this->expected_length = $expected_length; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public function close_writing(): void { |
| 30 | $this->is_writing_closed = true; |
| 31 | $this->expected_length = $this->bytes_already_forgotten + strlen( $this->buffer ); |
| 32 | } |
| 33 | |
| 34 | public function append_bytes( string $new_bytes ): void { |
| 35 | if ( $this->is_writing_closed ) { |
| 36 | throw new ByteStreamException( 'Cannot append bytes to a closed stream.' ); |
| 37 | } |
| 38 | if ( null !== $this->length() && $this->tell() + strlen( $new_bytes ) > $this->length() ) { |
| 39 | throw new ByteStreamException( 'Appending bytes to the stream would exceed the expected length.' ); |
| 40 | } |
| 41 | $this->buffer .= $new_bytes; |
| 42 | } |
| 43 | |
| 44 | protected function pull_no_more_than( $n ): int { |
| 45 | if ( $this->count_consumable_bytes() > 0 ) { |
| 46 | return min( $n, $this->count_consumable_bytes() ); |
| 47 | } |
| 48 | throw new NotEnoughDataException( 'Cannot pull bytes after exhausting the buffer from a MemoryPipe. You are likely missing a $pipe->reached_end_of_data() check before the pull() call.' ); |
| 49 | } |
| 50 | |
| 51 | protected function pull_exactly( $n ): int { |
| 52 | if ( $this->count_consumable_bytes() >= $n ) { |
| 53 | return min( $n, $this->count_consumable_bytes() ); |
| 54 | } |
| 55 | throw new NotEnoughDataException( 'Cannot pull bytes from a MemoryPipe.' ); |
| 56 | } |
| 57 | |
| 58 | protected function internal_pull( $n ): string { |
| 59 | throw new NotEnoughDataException( 'Cannot pull bytes from a MemoryPipe.' ); |
| 60 | } |
| 61 | |
| 62 | protected function seek_outside_of_buffer( int $target_offset ): void { |
| 63 | throw new NotEnoughDataException( 'Cannot seek past the available data. Call append_bytes() first.' ); |
| 64 | } |
| 65 | |
| 66 | public function length(): ?int { |
| 67 | return $this->expected_length; |
| 68 | } |
| 69 | } |
| 70 |