You are here

public function CachingStream::seek in Lockr 7.3

Seek to a position in the stream.

@link http://www.php.net/manual/en/function.fseek.php

Parameters

int $offset Stream offset:

int $whence Specifies how the cursor position will be calculated: based on the seek offset. Valid values are identical to the built-in PHP $whence values for `fseek()`. SEEK_SET: Set position equal to offset bytes SEEK_CUR: Set position to current location plus offset SEEK_END: Set position to end-of-stream plus offset.

Throws

\RuntimeException on failure.

Overrides StreamDecoratorTrait::seek

1 call to CachingStream::seek()
CachingStream::rewind in vendor/guzzlehttp/psr7/src/CachingStream.php
Seek to the beginning of the stream.

File

vendor/guzzlehttp/psr7/src/CachingStream.php, line 44

Class

CachingStream
Stream decorator that can cache previously read bytes from a sequentially read stream.

Namespace

GuzzleHttp\Psr7

Code

public function seek($offset, $whence = SEEK_SET) {
  if ($whence == SEEK_SET) {
    $byte = $offset;
  }
  elseif ($whence == SEEK_CUR) {
    $byte = $offset + $this
      ->tell();
  }
  elseif ($whence == SEEK_END) {
    $size = $this->remoteStream
      ->getSize();
    if ($size === null) {
      $size = $this
        ->cacheEntireStream();
    }
    $byte = $size + $offset;
  }
  else {
    throw new \InvalidArgumentException('Invalid whence');
  }
  $diff = $byte - $this->stream
    ->getSize();
  if ($diff > 0) {

    // Read the remoteStream until we have read in at least the amount
    // of bytes requested, or we reach the end of the file.
    while ($diff > 0 && !$this->remoteStream
      ->eof()) {
      $this
        ->read($diff);
      $diff = $byte - $this->stream
        ->getSize();
    }
  }
  else {

    // We can just do a normal seek since we've already seen this byte.
    $this->stream
      ->seek($byte);
  }
}