FileChunkIterator.php
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\Web\File; |
| 12 | |
| 13 | use JetBackup\Exception\IOException; |
| 14 | |
| 15 | defined( '__JETBACKUP__' ) or die( 'Restricted access' ); |
| 16 | |
| 17 | class FileChunkIterator { |
| 18 | |
| 19 | private FileStream $_file; |
| 20 | private int $_chunk_size; |
| 21 | |
| 22 | /** |
| 23 | * @param FileStream $file |
| 24 | * @param int $chunk_size |
| 25 | */ |
| 26 | public function __construct(FileStream $file, int $chunk_size=FileChunk::CHUNK_SIZE) { |
| 27 | $this->_file = $file; |
| 28 | $this->_chunk_size = $chunk_size; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return void |
| 33 | */ |
| 34 | public function rewind():void { |
| 35 | $this->_file->seek(0); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return bool |
| 40 | */ |
| 41 | public function hasNext():bool { |
| 42 | return $this->_file->tell() < $this->_file->getSize(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return FileChunk|null |
| 47 | * @throws IOException |
| 48 | */ |
| 49 | public function next():?FileChunk { |
| 50 | if(!$this->hasNext()) return null; |
| 51 | $chunk = new FileChunk($this->_file, $this->_chunk_size); |
| 52 | if(!$chunk->getSize()) return null; |
| 53 | return $chunk; |
| 54 | } |
| 55 | } |