Init from given stream

@param StreamInterface|resource $stream

@return \Hail\Image\Image
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"
        );
    }
674508
public function parse($stream)
    {
        if ($this->format->isEachLine()) {
            $iterator = new LineStreamIterator(
                $stream,
                [
                    LineStreamIterator::OPTION_ENDING         => "\n",
                    LineStreamIterator::OPTION_IGNORE_BLANK   => $this->format->isIgnoreBlankLines(),
                    LineStreamIterator::OPTION_INCLUDE_ENDING => false,
                ]
            );
            $iterator->setIgnoreBlank($this->format->isIgnoreBlankLines());
            return new MapIterator($iterator, [$this, 'decodeJson']);
        } else {
            $json = $this->decodeJson(stream_get_contents($stream));
            if (is_array($json)) {
                return new ArrayIterator($json);
            } else {
                throw new RuntimeException("Expecting a json array to parse, unknown format detected");
            }
        }
    }
668929
public function createStream($body = null)
    {
        if ($body instanceof StreamInterface)
        {
            return $body;
        }

        if (\is_resource($body))
        {
            return Stream::createFromResource($body);
        }

        return Stream::create($body ?? '');
    }
1358864
private function setStream($stream, $mode = 'r')
    {
        $error    = null;
        $resource = $stream;

        if (is_string($stream)) {
            set_error_handler(function ($e) use (&$error) {
                $error = $e;
            }, E_WARNING);
            $resource = fopen($stream, $mode);
            restore_error_handler();
        }

        if ($error) {
            throw new InvalidArgumentException('Invalid stream reference provided');
        }

        if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) {
            throw new InvalidArgumentException(
                'Invalid stream provided; must be a string stream identifier or stream resource'
            );
        }

        if ($stream !== $resource) {
            $this->stream = $stream;
        }

        $this->resource = $resource;
    }
961172
public function createEncryptingStream(EventStreamInterface $eventStream)
    {
        return new CryptoEventStream(
            $eventStream,
            $this->cryptos,
            $this->encryptionKey,
            CryptoEventStream::ENCRYPT
        );
    }
918132
public function parse(TokenStream $stream)
    {
        while (!$stream->is(Lexer::TOKEN_EOF)) {
            if ($stream->is(Lexer::TOKEN_WTSP)) {
                $stream->next();
                continue;
            }

            switch ($this->status) {
                case self::STAT_NONE:
                    $this->parseClassDefinition($stream);
                    break;
                case self::STAT_CLASS_DEFINED;
                    $this->parseBody($stream);
                    break;
                default:
                    $stream->next();
                    break;
            }
        }
        $this->nodes[] = new Node(self::NODE_EON, null);

        return new NodeStream($this->nodes);
    }
661095
public function create($uid, $stream)
    {
        return new $this->mailClass(
            $uid,
            $stream,
            $this->parseMailHeader($stream, $uid),
            $this->parseHeaders($stream, $uid),
            $this->parseBody($stream, $uid)
        );
    }
625155
protected function sourceToStream($source): StreamInterface
    {
        if (is_string($source)) {
            $source = Psr7\try_fopen($source, 'r+');
        }

        return Psr7\stream_for($source);
    }
185030
protected function castStream($source): StreamInterface
    {
        if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) {
            $source = $source->getStream();
        }

        if ($source instanceof StreamInterface) {
            //This step is important to prevent user errors
            $source->rewind();

            return $source;
        }

        if (is_resource($source)) {
            return stream_for($source);
        }

        if (empty($source)) {
            //Guzzle?
            return stream_for('');
        }

        if ($this->isFilename($source)) {
            //Must never pass user string in here, use Stream
            return stream_for(fopen($source, 'rb'));
        }

        //We do not allow source names in a string form
        throw new BucketException(
            "Source must be a valid resource, stream or filename, invalid value given"
        );
    }
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;
    }
1116197
public static function decorate(StreamInterface $stream, array $methods)
    {
        // If any of the required methods were not provided, then simply
        // proxy to the decorated stream.
        foreach (array_diff(static::SLOTS, array_keys($methods)) as $diff) {
            $methods[$diff] = [$stream, $diff];
        }

        return new self($methods);
    }
1121860
public function setStream($stream)
    {
        parent::setStream($stream);
        foreach ($this->getStreamWriters() as $writer) {
            $writer->setStream($stream);
        }

        return $this;
    }
464323
public function attach($stream, $mode = 'r')
    {
        $this->stream = $stream;

        if (is_resource($stream)) {
            $this->resource = $stream;
        } elseif (is_string($stream)) {
            $this->resource = fopen($stream, $mode);
        } else {
            throw new \InvalidArgumentException('Invalid resource.');
        }

        return $this;
    }
1228716
public function withBody(StreamInterface $body)
    {
        if ($this->complete) {
            return $this;
        }

        $new = $this->psrResponse->withBody($body);
        return new self($new);
    }
876980
public function fromStream(FilesystemStreamInterface $stream)
    {
        $mimetype = null;

        if (static::isSupported())
        {
            if ($path = $stream->getPath()) {
                $mimetype = $this->_getMimetype(strtolower(pathinfo($path, PATHINFO_EXTENSION)));
            }
        }

        return $mimetype;
    }
501530
public function init($data)
    {
        $this->data = $data;

        switch (true) {

            case $this->isGdResource():
                return $this->initFromGdResource($this->data);

            case $this->isImagick():
                return $this->initFromImagick($this->data);

            case $this->isInterventionImage():
                return $this->initFromInterventionImage($this->data);

            case $this->isSplFileInfo():
                return $this->initFromPath($this->data->getRealPath());

            case $this->isBinary():
                return $this->initFromBinary($this->data);

            case $this->isUrl():
                return $this->initFromUrl($this->data);

            case $this->isStream():
                return $this->initFromStream($this->data);

            case $this->isDataUrl():
                return $this->initFromBinary($this->decodeDataUrl($this->data));

            case $this->isFilePath():
                return $this->initFromPath($this->data);

            // isBase64 has to be after isFilePath to prevent false positives
            case $this->isBase64():
                return $this->initFromBinary(base64_decode($this->data));

            default:
                throw new Exception\NotReadableException("Image source not readable");
        }
    }
929081
protected function initializeStream(DuplexStreamInterface $stream, ConnectionInterface $connection)
    {
        try {
            $connection->setData('stream', $stream);
            $write = $this->getWriteStream($connection);
            $connection->setData('write', $write);
            $this->configureStreams($connection, $stream, $write);
            $this->identifyUser($connection, $write);
        } catch (\Exception $e) {
            $this->emitConnectionError($e, $connection);
        }
    }