Defuse\Crypto\File::readBytes PHP Method

readBytes() public static method

Read from a stream; prevent partial reads.
public static readBytes ( resource $stream, integer $num_bytes ) : string
$stream resource
$num_bytes integer
return string
    public static function readBytes($stream, $num_bytes)
    {
        if ($num_bytes < 0) {
            throw new Ex\EnvironmentIsBrokenException('Tried to read less than 0 bytes');
        } elseif ($num_bytes === 0) {
            return '';
        }
        $buf = '';
        $remaining = $num_bytes;
        while ($remaining > 0 && !\feof($stream)) {
            $read = \fread($stream, $remaining);
            if ($read === false) {
                throw new Ex\IOException('Could not read from the file');
            }
            $buf .= $read;
            $remaining -= Core::ourStrlen($read);
        }
        if (Core::ourStrlen($buf) !== $num_bytes) {
            throw new Ex\IOException('Tried to read past the end of the file');
        }
        return $buf;
    }