Implements support for fread(), fgets() etc.

@param   int        $count    maximum number of bytes to read
@return  string
436337
public function stream_read($count)
    {
        if ($this->stream_eof()) {
            return false;
        }
        $s = substr($this->data, $this->pos, $count);
        $this->pos += $count;
        return $s;
    }
214880
public function read(int $length = null): string
    {
        // can not call fgets with null when not specified
        $data = null === $length ? fgets($this->resource) : fgets($this->resource, $length);
        if (false === $data) {
            // fgets() returns false on eof while feof() returned false before
            // but will now return true
            if ($this->eof()) {
                return '';
            }

            if (stream_get_meta_data($this->resource)['timed_out']) {
                throw new Timeout(
                        'Reading of ' . $length . ' bytes failed: timeout of '
                        . $this->timeout . ' seconds exceeded'
                );
            }

            throw new ConnectionFailure(
                    'Reading of ' . $length . ' bytes failed.'
            );
        }

        return $data;
    }
1272088
public static function getValues($count)
    {
        if (!Int::is($count) || $count < 1) {
            throw new \InvalidArgumentException("The \$count parameter must be of type int and greater than zero.");
        }

        $ret = [];
        $flag = 1;
        for ($i = 0; $i < $count; $i++) {
            $ret[] = $flag;
            $flag <<= 1;
        }

        return $ret;
    }
930814
protected function getTokenCharacters($count = 1)
    {
        $result = '';
        for ($i = 0; $i < $count; $i++) {
            $result .= $this->tokenChars[mt_rand(0, count($this->tokenChars) - 1)];
        }
        return $result;
    }
54778
public function read($count)
    {
        // If the archive is complete, we only read the remaining buffer.
        if (!$this->isComplete) {
            if (isset($this->curVideoStream)) {
                if ($this->curVideoStream->eof()) {
                    // Stop streaming the current video.
                    $this->complete_file_stream();

                    $video = next($this->videos);
                    if ($video) {
                        // Start streaming the next video.
                        $this->startVideoStream($video);
                    } else {
                        // No video left.
                        $this->finish();
                        $this->isComplete = true;
                    }
                } else {
                    // Continue streaming the current video.
                    $this->stream_file_part($this->curVideoStream->read($count));
                }
            } else {
                // Start streaming the first video.
                $this->startVideoStream(current($this->videos));
            }
        }

        return fread($this->buffer, $count);
    }
467863
public function readInt()
    {
        $str = $this->_fread(4);

        return  ord($str[0]) << 24 |
                ord($str[1]) << 16 |
                ord($str[2]) << 8  |
                ord($str[3]);
    }
233692
function recv($length)
    {
        $content = '';
        $readn = $length;

        while ($readn > 0)
        {
            if ($readn > 8192)
            {
                $tmp = fread($this->fp, 8192);
            }
            else
            {
                $tmp = fread($this->fp, $readn);
            }
            if (empty($tmp))
            {
                break;
            }
            $content .= $tmp;
            $readn -= strlen($tmp);
        }
        return $content;
    }
1140584
public function takeLast(int $count)
    {
        return $this->wrap(function (Iterator $data) use ($count) {
            // From the end of the collection
            $limit = max(0, $count);
            $chunk = [];

            foreach ($data as $key => $datum) {
                $chunk[] = [$key, $datum];
                if (count($chunk) > $limit) {
                    array_shift($chunk);
                }
            }

            foreach ($chunk as $item) {
                [$key, $datum] = $item;
                yield $key => $datum;
            }
        });
    }
720983
protected function getTitleString($titles, $count)
    {
        $limit      = $this->getLimit();
        $okCount    = count($titles);
        $missing    = $count - $okCount;
        $andCount   = max([0, $okCount - $limit]) + $missing;

        if (!$okCount) {
            list($singular, $plural) = $this->getTypeLabelVariations($count);
            return $this->formatPlural($count, $singular, $plural);
        }
        if ($count < 2) {
            return reset($titles);
        }
        if ($okCount === 2 && !$andCount) {
            return $this->t("@name1 and @name2", array_combine(['@name1', '@name2'], $titles));
        }

        if ($limit < $okCount) {
            $titles = array_slice($titles, 0, $limit);
        }

        $ret = implode(", ", $titles);

        if ($andCount) {
            $ret .= ' ' . $this->formatPlural($andCount, "and @count more", "and @count more");
        }

        return $ret;
    }
165790
public function stream_read($count) {
		if ($this->_lob->eof() || !$count) {
			return '';
		}

		// CLOB to return characters not bytes
		return $this->_lob->read($count / 2); // Fix we expect 2 byte char
	}
425685
public function stream_read($count) {
    assert(isset($this->client));
    return $this->client->read($count);
  }
347420
public function iterNextPage($count = 3)
    {
        if ($this->getPage() == 1 && $this->getPagesCount() >= $count * 2) {
            $count *= 2;
        }
        $pages = [];
        foreach (range(1, $count) as $i) {
            $page = $this->getPage() + $i;
            if ($page <= $this->getPagesCount()) {
                $pages[] = $page;
            }
        }

        return $pages;
    }
301185
private function read()
	{
		// Ignore first byte
		fread($this->fd, 1);

		// There is also masking bit, as MSB, bit it's 0
		$payloadLength = ord(fread($this->fd, 1));

		switch ($payloadLength) {
			case 126:
				$payloadLength = unpack("n", fread($this->fd, 2));
				$payloadLength = $payloadLength[1];
				break;
			case 127:
				//$this->stdout('error', "Next 8 bytes are 64bit uint payload length, not yet implemented, since PHP can't handle 64bit longs!");
				break;
		}

		return fread($this->fd, $payloadLength);
	}
330444
public function getLastCommand($count = 1) {
    $retval = array();
    for($i = 0; $i < $count; $i++) {
      $retval[] = array_pop($this->command_history);
    }
    return $retval;
  }
1250813
public function shift($count = 1)
    {
        $return = array();

        if ($count == 1) {
            return array_shift($this->dirParts);
        }

        for ($i=0; $i<$count; $i++) {
            $value = array_shift($this->dirParts);

            if ($value === null) {
                break;
            }

            $return[] = $value;
        }

        return $return;
    }
765220
function _stream_read($count)
    {
        switch ($this->mode) {
            case 'w':
            case 'a':
            case 'x':
            case 'c':
                return false;
        }

        // commented out because some files - eg. /dev/urandom - will say their size is 0 when in fact it's kinda infinite
        //if ($this->pos >= $this->size) {
        //    $this->eof = true;
        //    return false;
        //}

        $result = $this->sftp->get($this->path, false, $this->pos, $count);
        if (isset($this->notification) && is_callable($this->notification)) {
            if ($result === false) {
                call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
                return 0;
            }
            // seems that PHP calls stream_read in 8k chunks
            call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);
        }

        if (empty($result)) { // ie. false or empty string
            $this->eof = true;
            return false;
        }
        $this->pos+= strlen($result);

        return $result;
    }
917411
public function stream_read($count)
    {
        $ret = substr($this->_data, $this->_pos, $count);
        $this->_pos += strlen($ret);
        return $ret;
    }