You are here

public function CachingStream::seek in Zircon Profile 8

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

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();
    }

    // Because 0 is the first byte, we seek to size - 1.
    $byte = $size - 1 - $offset;
  }
  else {
    throw new \InvalidArgumentException('Invalid whence');
  }
  $diff = $byte - $this->stream
    ->getSize();
  if ($diff > 0) {

    // If the seek byte is greater the number of read bytes, then read
    // the difference of bytes to cache the bytes and inherently seek.
    $this
      ->read($diff);
  }
  else {

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