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