phpseclib/phpseclib/System/SSH/Common/Traits/ReadBytes.php

44 lines
945 B
PHP
Raw Normal View History

2020-02-02 04:50:28 +00:00
<?php
/**
* ReadBytes trait
*
* PHP version 5
*
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
2022-06-04 15:31:21 +00:00
declare(strict_types=1);
2020-02-02 04:50:28 +00:00
namespace phpseclib3\System\SSH\Common\Traits;
use phpseclib3\Exception\RuntimeException;
2020-02-02 04:50:28 +00:00
/**
* ReadBytes trait
*
* @author Jim Wigginton <terrafrost@php.net>
*/
trait ReadBytes
{
/**
* Read data
*
* @throws RuntimeException on connection errors
2020-02-02 04:50:28 +00:00
*/
2022-07-03 12:33:45 +00:00
public function readBytes(int $length): string
2020-02-02 04:50:28 +00:00
{
$temp = fread($this->fsock, $length);
2022-07-03 12:33:45 +00:00
if ($temp === false) {
throw new RuntimeException('\fread() failed.');
2022-07-03 12:33:45 +00:00
}
if (strlen($temp) !== $length) {
throw new RuntimeException("Expected $length bytes; got " . strlen($temp));
2020-02-02 04:50:28 +00:00
}
return $temp;
}
}