ReadBytes.php
44 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ReadBytes trait |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * @author Jim Wigginton <terrafrost@php.net> |
| 9 | * @copyright 2015 Jim Wigginton |
| 10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 11 | * @link http://phpseclib.sourceforge.net |
| 12 | */ |
| 13 | |
| 14 | declare(strict_types=1); |
| 15 | |
| 16 | namespace phpseclib3\System\SSH\Common\Traits; |
| 17 | |
| 18 | use phpseclib3\Exception\RuntimeException; |
| 19 | |
| 20 | /** |
| 21 | * ReadBytes trait |
| 22 | * |
| 23 | * @author Jim Wigginton <terrafrost@php.net> |
| 24 | */ |
| 25 | trait ReadBytes |
| 26 | { |
| 27 | /** |
| 28 | * Read data |
| 29 | * |
| 30 | * @throws RuntimeException on connection errors |
| 31 | */ |
| 32 | public function readBytes(int $length): string |
| 33 | { |
| 34 | $temp = fread($this->fsock, $length); |
| 35 | if ($temp === false) { |
| 36 | throw new RuntimeException('\fread() failed.'); |
| 37 | } |
| 38 | if (strlen($temp) !== $length) { |
| 39 | throw new RuntimeException("Expected $length bytes; got " . strlen($temp)); |
| 40 | } |
| 41 | return $temp; |
| 42 | } |
| 43 | } |
| 44 |