function SessionStreamWrapper::stream_read

Same name and namespace in other branches
  1. 8.x-1.x stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php \Drupal\stream_wrapper_example\StreamWrapper\SessionStreamWrapper::stream_read()
  2. 4.0.x modules/stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php \Drupal\stream_wrapper_example\StreamWrapper\SessionStreamWrapper::stream_read()

Read from stream.

This method is called in response to fread() and fgets().

Note, remember to update the read/write position of the stream (by the number of bytes that were successfully read).

Note, PhpStreamWrapperInterface::stream_eof() is called directly after calling PhpStreamWrapperInterface::stream_read() to check if EOF has been reached. If not implemented, EOF is assumed.

Warning, when reading the whole file (e.g., with file_get_contents()), PHP will call PhpStreamWrapperInterface::stream_read() followed by PhpStreamWrapperInterface::stream_eof() in a loop but as long as PhpStreamWrapperInterface::stream_read() returns a non-empty string, the return value of PhpStreamWrapperInterface::stream_eof() is ignored.

Parameters

int $count: How many bytes of data from the current position should be returned.

Return value

string|false If there are less than $count bytes available, return as many as are available. If no more data is available, return either FALSE or an empty string.

Overrides PhpStreamWrapperInterface::stream_read

File

modules/stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php, line 452

Class

SessionStreamWrapper
Example stream wrapper class to handle session:// streams.

Namespace

Drupal\stream_wrapper_example\StreamWrapper

Code

public function stream_read($count) {
    // @codingStandardsIgnoreEnd
    if (is_string($this->sessionContent)) {
        $remaining_chars = strlen($this->sessionContent) - $this->streamPointer;
        $number_to_read = min($count, $remaining_chars);
        if ($remaining_chars > 0) {
            $buffer = substr($this->sessionContent, $this->streamPointer, $number_to_read);
            $this->streamPointer += $number_to_read;
            return $buffer;
        }
    }
    return FALSE;
}