Normalize provided body and ensure that the result object is Stream.

@param StreamInterface|string|null $body The request body object

@return Stream Normalized body

@throws \InvalidArgumentException If an unsupported argument type is provided.
16567
private function normalizeBody($body = null)
    {
        $body = $body ? $body : new Stream(fopen('php://temp', 'r+'));
        if (is_string($body)) {
            $memoryStream = fopen('php://temp', 'r+');
            fwrite($memoryStream, $body);
            rewind($memoryStream);
            $body = new Stream($memoryStream);
        } elseif (!($body instanceof StreamInterface)) {
            throw new InvalidArgumentException(
                'Body must be a string, null or implement Psr\Http\Message\StreamInterface'
            );
        }

        return $body;
    }
160276
public function setRawBody(string $body) {
        $this->rawBody = $body;
        $this->body = null;
        return $this;
    }
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;
    }
702198
public function autoEmbed($body = null)
    {
        if (!is_string($body)) {
            $this->errors[] = 'For auto-embedding purposes, the input must be a string';
        } elseif ($data = $this->getUrlInfo($body)) {
            $table = array();
            foreach ($data as $url => $service) {
                if (!empty($service['html'])) {
                    $table[$url] = $service['html'];
                }
            }

            // Determine wether the body looks like HTML or just plain text.
            if (strpos($body, '>') !== false) {
                $processor = new \Embera\HtmlProcessor($this->config['ignore_tags'], $table);
                return $processor->process($body);
            }

            return strtr($body, $table);
        }

        return $body;
    }
450710
private function transformResponse($response)
    {
        if (is_null($response)) {
            return;
        }

        if ($body = $response->getBody()) {
            $body = new Stream($body->getStream());
        } else {
            $body = null;
        }

        $transformed = new Response(
            $response->getStatusCode(),
            null,
            $response->getHeaders(),
            $body,
            $response->getProtocolVersion()
        );

        return $transformed;
    }
249555
public static function reader(StreamInterface $stream, $bufferSize = 8192)
	{
		if(!$stream->isReadable())
		{
			throw new \InvalidArgumentException(sprintf('Stream is not readable: %s', get_class($stream)));
		}
		
		while(!$stream->eof())
		{
			yield $stream->read($bufferSize);
		}
	}
1228716
public function withBody(StreamInterface $body)
    {
        if ($this->complete) {
            return $this;
        }

        $new = $this->psrResponse->withBody($body);
        return new self($new);
    }
375439
public static function body($message)
    {
        if (!isset($message['body'])) {
            return null;
        }

        if ($message['body'] instanceof StreamInterface) {
            return (string) $message['body'];
        }

        switch (gettype($message['body'])) {
            case 'string':
                return $message['body'];
            case 'resource':
                return stream_get_contents($message['body']);
            case 'object':
                if ($message['body'] instanceof \Iterator) {
                    return implode('', iterator_to_array($message['body']));
                } elseif (method_exists($message['body'], '__toString')) {
                    return (string) $message['body'];
                }
            default:
                throw new \InvalidArgumentException('Invalid request body: '
                    . self::describeType($message['body']));
        }
    }
1371507
public function withBody($body): self {
        $clone = clone $this;

        if ($body === null) {
            $clone->body = new StringBody("");
        } elseif (\is_scalar($body)) {
            $clone->body = new StringBody((string) $body);
        } elseif ($body instanceof RequestBody) {
            $clone->body = $body;
        } else {
            throw new \TypeError("Invalid body type: " . gettype($body));
        }

        return $clone;
    }
20656
public function send(array $body = null)
    {
        $json = $this->createJsonBody($body);
        $signature = $this->createSignature($json);
        
        $headers = $options = [];
        $headers["User-Agent"] = self::prepareUserAgent();
        
        if ($body) {
            $headers["Content-Type"] = "application/json";
            $headers["Content-Length"] = strlen($json);
            $options["body"] = $json;
        }
        
        $options["headers"] = $this->prepareHeaders($signature, $headers);
        
        try {
            $this->httpClient = new Client(
                [
                    "base_uri" => $this->host,
                    "timeout" => self::TIMEOUT
                ]
            );
            
            return new Result(
                $this->httpClient->request(
                    $this->method, 
                    $this->endpoint, 
                    $options
                )
            );
        } catch (TransferException $e) {
            throw new EndpointException($e->getMessage(), $e->getCode(), $e);
        }
    }
445449
public function body($body, $html = null)
    {
        if ($html !== null) {
            $this->html($html);
        }

        $this->body = $body;

        return $this;
    }
529308
public function setStream($stream)
    {
        if (! is_resource($stream) || ! 'stream' == get_resource_type($stream)) {
            throw new \InvalidArgumentException(sprintf(
                'Expects argument to be a stream resource, got %s',
                is_resource($stream) ? get_resource_type($stream) : gettype($stream)
            ));
        }

        $this->stream = $stream;

        return $this;
    }
1341661
protected function sendBody(Stream $body)
    {
        // I don't trust that this will be at the beginning of the stream, so reset.
        $body->rewind();

        // writing to an arbitrary stream.
        // @todo Use stream operations to make this more robust and allow
        $bytes = 0;
        if ($bytes = $body->getSize() && $bytes < 500) {
            print $body->getContents();

        } else {
            while (!$body->eof()) {
                $data = $body->read(1024);
                print $data;
            }
        }
    }
659000
public function initFromStream($stream)
    {
        if (!$stream instanceof StreamInterface) {
            $stream = new 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 \Intervention\Image\Exception\NotReadableException(
            "Unable to init from given stream"
        );
    }
806625
public static function fromStream(StreamInterface $stream)
    {
        if (! $stream->isReadable() || ! $stream->isSeekable()) {
            throw new InvalidArgumentException('Message stream must be both readable and seekable');
        }

        $stream->rewind();

        list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
        list($headers, $body)                  = self::splitStream($stream);

        return (new Response($body, $status, $headers))
            ->withProtocolVersion($version)
            ->withStatus($status, $reasonPhrase);
    }
1088912
final public function withBody(StreamInterface $body): self
    {
        $message = clone $this;
        $message->body = $body;

        return $message;
    }
463498
public function withBody(StreamInterface $body)
    {
        $request = $this->copy();
        $request->setBody($body);
        
        return $request;
    }