Returns a resource representing the stream.

@param StreamInterface $stream The stream to get a resource for

@return resource

@throws \InvalidArgumentException if stream is not readable or writable
340026
public static function getResource(StreamInterface $stream)
    {
        self::register();

        if ($stream->isReadable()) {
            $mode = $stream->isWritable() ? 'r+' : 'r';
        } elseif ($stream->isWritable()) {
            $mode = 'w';
        } else {
            throw new \InvalidArgumentException('The stream must be readable, '
                .'writable, or both.');
        }

        return fopen('phmessage://stream', $mode, null, stream_context_create([
            'phmessage' => ['stream' => $stream],
        ]));
    }
785662
public final function withBody(StreamInterface $body): MessageInterface
   {
       if (!$body->getMetadata("uri")) {
           throw new InvalidArgumentException(
               "Can't get instance for body: invalid stream");
       }
       return $this->with("body", $body);
   }
743152
public function createStream($content = ""){
        if(!is_string($content)){
            throw new \InvalidArgumentException();
        }

        $path   = tempnam($this->dir, $this->prefix);

        if($path === false){
            throw new \RuntimeException;
        }

        $stream = $this->createStreamFromFile($path, "r");

        if($content !== ""){
            $stream->write($content);
            $stream->rewind();
        }

        return $stream;
    }
942212
public function getStream()
	{
		$this->validateActive();

		if ($this->stream instanceof StreamInterface) {
			return $this->stream;
		}

		return Factory::streamFromFile($this->file, 'r');
	}
69854
protected static function csvParser(StreamInterface $stream): array
	{
		if ($stream->isSeekable()) {
			$stream->rewind();
		}

		$handle = $stream->detach();
		$data = [];
		while (($row = \fgetcsv($handle)) !== false) {
			$data[] = $row;
		}
		\fclose($handle);

		return ['body' => $data];
	}
789323
public function read(AbstractStream $stream)
    {
        return $stream->getBuffer()->read($this->getSize(), $this->getEndian());
    }
472594
protected function tlsStreamInitializer(ParametersInterface $parameters)
    {
        $resource = $this->tcpStreamInitializer($parameters);
        $metadata = stream_get_meta_data($resource);

        // Detect if crypto mode is already enabled for this stream (PHP >= 7.0.0).
        if (isset($metadata['crypto'])) {
            return $resource;
        }

        if (is_array($parameters->ssl)) {
            $options = $parameters->ssl;
        } else {
            $options = array();
        }

        if (!isset($options['crypto_type'])) {
            $options['crypto_type'] = STREAM_CRYPTO_METHOD_TLS_CLIENT;
        }

        if (!stream_context_set_option($resource, array('ssl' => $options))) {
            $this->onConnectionError('Error while setting SSL context options');
        }

        if (!stream_socket_enable_crypto($resource, true, $options['crypto_type'])) {
            $this->onConnectionError('Error while switching to encrypted communication');
        }

        return $resource;
    }
149676
private function getCharsetDecoratorForStream(StreamInterface $stream)
    {
        $charset = $this->part->getCharset();
        if (!empty($charset)) {
            $stream = $this->streamFactory->newCharsetStream(
                $stream,
                $charset,
                MailMimeParser::DEFAULT_CHARSET
            );
        }
        return $stream;
    }
254046
public function read($length){
        if(!is_int($length)){
            throw new \InvalidArgumentException();
        }

        if(null === $this->resource){
            throw new Exception\StreamUnavailableException(
                static::EXCEPTION_MSG_UNAVAILABLE
            );
        }

        if(!$this->isReadable()){
            throw new Exception\StreamUnreadableException(
                static::EXCEPTION_MSG_UNREADABLE
            );
        }

        if(false === ($contents = fread($this->resource, $length))){
            throw new \RuntimeException("Failed to read stream.");
        }

        return $contents;
    }
528236
private static function getStatusLine(StreamInterface $stream)
    {
        $line = self::getLine($stream);

        if (! preg_match(
            '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#',
            $line,
            $matches
        )) {
            throw new UnexpectedValueException('No status line detected');
        }

        return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];
    }
228742
protected function initStream($stream)
    {
        expect_type(
            $stream,
            ['stream resource', 'string'],
            \TypeError::class,
            "Expected stream resource or uri (string), %s given"
        );

        if (is_string($stream)) {
            $stream = $this->openStream($stream);
        }

        $meta = stream_get_meta_data($stream);

        if ($meta['mode'] === 'r') {
            throw new \InvalidArgumentException("Stream \"{$meta['uri']}\" is not writable");
        }

        return $stream;
    }
699810
static function convertToResource(StreamInterface $stream)
    {
        # register wrapper if not
        $self = new self;
        if(!RegistryOfWrapperStream::isRegistered($self->getLabel()))
            RegistryOfWrapperStream::register($self);

        # make resource
        $mode = new AccessMode();
        (!$stream->isWritable()) ?: $mode->openForWrite();
        (!$stream->isReadable()) ?: $mode->openForRead();

        $label = $self->getLabel();

        $context  = new ContextStreamBase( $label, array('stream' => $stream) );
        $context  = $context->toContext();
        $resource = fopen($label.'://stream'
            , (string) $mode
            , null
            ## set options to wrapper
            , $context
        );

        return $resource;
    }
849306
public function extract(StreamInterface $arguments): MapInterface
    {
        return $this
            ->inputs
            ->reduce(
                new Map('int', Input::class),
                static function(Map $inputs, Input $input): Map {
                    return $inputs->put($inputs->size(), $input); //map value to a position
                }
            )
            ->reduce(
                new Map('string', 'mixed'),
                static function(Map $inputs, int $position, Input $input) use ($arguments): MapInterface {
                    return $input->extract($inputs, $position, $arguments);
                }
            );
    }
576075
private function getStream($filename): StreamInterface
    {
        if ($filename instanceof StreamableInterface) {
            return $filename->getStream();
        }

        if ($filename instanceof StreamInterface) {
            return $filename;
        }

        if (is_resource($filename)) {
            return new Stream($filename, 'r');
        }

        if (!$this->files->isFile($filename)) {
            throw new ResponseException("Unable to allocate response body stream, file does not exist");
        }

        return new Stream(fopen($filename, 'r'));
    }
891468
public function setInputStream($stream)
    {
        $stream = new Stream($stream ?: STDIN);
        $stream = FnStream::decorate($stream, [
            'read' => function ($length) use ($stream) {
                $ret = $stream->read($length);
                $this->onRead($ret);

                return $ret;
            },
        ]);
        $stream = StreamWrapper::getResource($stream);

        parent::setInputStream($stream);
    }
108652
public function withBody(StreamInterface $body)
    {
        $request = clone $this;
        if (method_exists($request, 'setContent')) {
            $request->setContent($body);
        } else {
            $request->content = $body;
        }

        return $request;
    }
510626
public function initFromStream($stream)
    {
        $stream = Factory::stream($stream);

        try {
            $offset = $stream->tell();
        } catch (\RuntimeException $e) {
            $offset = 0;
        }

        $shouldAndCanSeek = $offset !== 0 && $stream->isSeekable();

        if ($shouldAndCanSeek) {
            $stream->rewind();
        }

        try {
            $data = $stream->getContents();
        } catch (\RuntimeException $e) {
            $data = null;
        }

        if ($shouldAndCanSeek) {
            $stream->seek($offset);
        }

        if ($data) {
            return $this->initFromBinary($data);
        }

        throw new Exception\NotReadableException(
            "Unable to init from given stream"
        );
    }