Read from a stream; prevent partial reads

@param int $num
@param bool $skipTests
@return string
@throws CannotPerformOperation
@throws FileAccessDenied
961238
public function readBytes(int $num, bool $skipTests = false): string
    {
        // @codeCoverageIgnoreStart
        if ($num < 0) {
            throw new CannotPerformOperation('num < 0');
        } elseif ($num === 0) {
            return '';
        }
        if (($this->pos + $num) > $this->stat['size']) {
            throw new CannotPerformOperation('Out-of-bounds read');
        }
        // @codeCoverageIgnoreEnd
        $buf = '';
        $remaining = $num;
        do {
            if ($remaining <= 0) {
                // @codeCoverageIgnoreStart
                break;
                // @codeCoverageIgnoreEnd
            }
            /** @var int $bufSize */
            $bufSize = \min($remaining, self::CHUNK);
            /** @var string|bool $read */
            $read = \fread($this->fp, $bufSize);
            if (!\is_string($read)) {
                // @codeCoverageIgnoreStart
                throw new FileAccessDenied(
                    'Could not read from the file'
                );
                // @codeCoverageIgnoreEnd
            }
            $buf .= $read;
            $readSize = Binary::safeStrlen($read);
            $this->pos += $readSize;
            $remaining -= $readSize;
        } while ($remaining > 0);
        return $buf;
    }
556834
public function seeResponseCodeIs($num)
    {
        \PHPUnit\Framework\Assert::assertEquals($num, $this->client->getInternalResponse()->getStatus());
    }
1070509
public function skip(int $n) : int
    {
        if ($n <= 0) {
            return 0;
        }

        $skipped = 0;
        $skipLimit = min(self::MAX_SKIP_BUFFER_SIZE, $n);
        while ($skipped < $n) {
            $buf = $this->read(min($skipLimit, $n - $skipped));
            if ($buf === null) {
                break;
            }
            $skipped += strlen($buf);
        }
        return $skipped;
    }
1141249
protected function skipBOM()
    {
        rewind($this->fileHandle);

        switch ($this->inputEncoding) {
            case 'UTF-8':
                fgets($this->fileHandle, 4) == "\xEF\xBB\xBF" ?
                    fseek($this->fileHandle, 3) : fseek($this->fileHandle, 0);
                break;
            case 'UTF-16LE':
                fgets($this->fileHandle, 3) == "\xFF\xFE" ?
                    fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
                break;
            case 'UTF-16BE':
                fgets($this->fileHandle, 3) == "\xFE\xFF" ?
                    fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
                break;
            case 'UTF-32LE':
                fgets($this->fileHandle, 5) == "\xFF\xFE\x00\x00" ?
                    fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
                break;
            case 'UTF-32BE':
                fgets($this->fileHandle, 5) == "\x00\x00\xFE\xFF" ?
                    fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
                break;
            default:
                break;
        }
    }
939473
static function skipCurvy(array $tokens, &$i) {
    $level = 0;
    for (++$i; ; ++$i) {
      if (')' === $token = $tokens[$i]) {
        if (--$level < 0) {
          ++$i;
          return NULL;
        }
      }
      elseif ('(' === $token) {
        ++$level;
      }
      elseif ('#' === $token) {
        --$i;
        return FALSE;
      }
    }

    throw new \RuntimeException('Unreachable code.');
  }
1099386
protected function skip($srcPath, $skipPath)
    {
        $len = strlen($srcPath);

        for ($i = 0; $i < $len; $i++) {
            if (!isset($skipPath[$i]) || $skipPath[$i] != $srcPath[$i]) {
                break;
            }
        }

        return substr($srcPath, $i);
    }
61130
public function skip($num)
    {
        $num = (integer) $num;
        $this->skip = $num;
        $this->riakCursor->skip($num);
        return $this;
    }
1039683
private function skipToNextStringOrNamespaceSeparator(\ArrayIterator $tokens)
    {
        while ($tokens->valid() && (T_STRING !== $tokens->current()[0]) && (T_NS_SEPARATOR !== $tokens->current()[0])) {
            $tokens->next();
        }
    }
1352619
@Override
    public long skip(long n) throws IOException {
        long toSkip = max >= 0 ? Math.min(n, max - pos) : n;
        long skippedBytes = in.skip(toSkip);
        pos += skippedBytes;
        return skippedBytes;
    }
549437
public boolean skipPreamble()
            throws IOException {
        // First delimiter may be not preceeded with a CRLF.
        System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);
        boundaryLength = boundary.length - 2;
        try {
            // Discard all data up to the delimiter.
            discardBodyData();

            // Read boundary - if succeded, the stream contains an
            // encapsulation.
            return readBoundary();
        } catch (MalformedStreamException e) {
            return false;
        } finally {
            // Restore delimiter.
            System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);
            boundaryLength = boundary.length;
            boundary[0] = CR;
            boundary[1] = LF;
        }
    }
628100
public function skip($num)
    {
        $num = (integer) $num;
        $this->skip = $num;
        $this->mongoCursor->skip($num);
        return $this;
    }
1018471
static function skipCurly(array $tokens, &$i) {
    $iStart = $i;
    $level = 0;
    for (++$i;; ++$i) {
      if ('}' === $token = $tokens[$i]) {
        if (--$level < 0) {
          ++$i;
          return NULL;
        }
      }
      elseif ('{' === $token || T_CURLY_OPEN === $token[0] || T_DOLLAR_OPEN_CURLY_BRACES === $token[0]) {
        ++$level;
      }
      elseif ('#' === $token) {
        $i = $iStart;
        return FALSE;
      }
    }

    throw new \RuntimeException('Unreachable code.');
  }
1316619
public function flush(
        int $bulkNumber = 500,
        bool $skipIfLess = false
    ) {
        if (
            $skipIfLess &&
            count($this->elementsToUpdate) < $bulkNumber
        ) {
            return;
        }

        $offset = 0;

        try {
            while (true) {
                $items = array_slice(
                    $this->elementsToUpdate,
                    $offset,
                    $bulkNumber
                );

                if (empty($items)) {
                    break;
                }

                $this->flushItems($items, []);
                $offset += $bulkNumber;
            }

            $this->flushItems([], $this->elementsToDelete);
        } catch (Exception $exception) {
            /*
             * No matter the exception is thrown, cached elements should be
             * deleted
             */
            $this->resetCachedElements();

            throw $exception;
        }

        $this->resetCachedElements();
    }
1164186
public function send(MessageInterface $message, $skipErrors = true) {

        // check clickatell limit
        if (count($message->getRecipients()) > 600) {
            $this->addError(new SendingError($message->getRecipients(),
                    'You have excedded provider limit of messages to send in one time.'
                    , $message->error));
        }

        $gateway = $this->getClient();

        try {
            $response = $gateway->sendMessage($message->getRecipients(), $message->getText());
            foreach ($response as $message) {
                if ($message->errorCode) {
                    $this->addError(new SendingError($message->destination, $message->errorCode, $message->error));
                }
            }
        }
        catch (Exception $e) {
            $this->addError(new SendingError('', $e->getCode(), $e->getMessage()));
            if (!$skipErrors) {
                throw new \RuntimeException($e->getMessage(), $e->getCode());
            }
        }
        return $this->getErrors()->count() === 0;
    }
346300
public function outputImage($path, array $params = [], $skipValidation = false)
    {
        $this->validateRequest($path, $params, $skipValidation);

        $this->getLeagueGlideServer()->outputImage($path, $params);
    }
644906
public long skip(InputStream src, long n) throws IOException {
        long done;
        int chunk;

        if (n <= 0) {
            return 0;
        }
        done = 0;
        while (done < n) {
            chunk = src.read(buffer, 0, (int) Math.min(Integer.MAX_VALUE, n - done));
            if (chunk == -1) {
                break;
            }
            done += chunk;
        }
        return done;
    }
168951
private long skipInternal(long n) throws IOException {
    assert (stateChangeLock.isLocked());
    waitForAsyncReadComplete();
    if (isEndOfStream()) {
      return 0;
    }
    if (available() >= n) {
      // we can skip from the internal buffers
      int toSkip = (int) n;
      // We need to skip from both active buffer and read ahead buffer
      toSkip -= activeBuffer.remaining();
      assert(toSkip > 0); // skipping from activeBuffer already handled.
      activeBuffer.position(0);
      activeBuffer.flip();
      readAheadBuffer.position(toSkip + readAheadBuffer.position());
      swapBuffers();
      // Trigger async read to emptied read ahead buffer.
      readAsync();
      return n;
    } else {
      int skippedBytes = available();
      long toSkip = n - skippedBytes;
      activeBuffer.position(0);
      activeBuffer.flip();
      readAheadBuffer.position(0);
      readAheadBuffer.flip();
      long skippedFromInputStream = underlyingInputStream.skip(toSkip);
      readAsync();
      return skippedBytes + skippedFromInputStream;
    }
  }