You are here

function copy_to_stream in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/psr7/src/functions.php \GuzzleHttp\Psr7\copy_to_stream()

Copy the contents of a stream into another stream until the given number of bytes have been read.

Parameters

StreamInterface $source Stream to read from:

StreamInterface $dest Stream to write to:

int $maxLen Maximum number of bytes to read. Pass -1: to read the entire stream.

Throws

\RuntimeException on error.

1 call to copy_to_stream()
CachingStream::cacheEntireStream in vendor/guzzlehttp/psr7/src/CachingStream.php

File

vendor/guzzlehttp/psr7/src/functions.php, line 345

Namespace

GuzzleHttp\Psr7

Code

function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) {
  if ($maxLen === -1) {
    while (!$source
      ->eof()) {
      if (!$dest
        ->write($source
        ->read(1048576))) {
        break;
      }
    }
    return;
  }
  $bytes = 0;
  while (!$source
    ->eof()) {
    $buf = $source
      ->read($maxLen - $bytes);
    if (!($len = strlen($buf))) {
      break;
    }
    $bytes += $len;
    $dest
      ->write($buf);
    if ($bytes == $maxLen) {
      break;
    }
  }
}