Reads a packet from the socket. Prior to reading from the socket
will check for availability of the connection.

@param integer $length Number of bytes to read.
@return string|boolean Data or `false` on error.
5724
protected function _read($length = null) {
		if (!$this->connected) {
			$message = 'No connection found while reading data from socket.';
			throw new RuntimeException($message);
		}
		if ($length) {
			if (feof($this->_connection)) {
				return false;
			}
			$data = stream_get_contents($this->_connection, $length + 2);
			$meta = stream_get_meta_data($this->_connection);

			if ($meta['timed_out']) {
				$message = 'Connection timed out while reading data from socket.';
				throw new RuntimeException($message);
			}
			$packet = rtrim($data, "\r\n");
		} else {
			$packet = stream_get_line($this->_connection, 16384, "\r\n");
		}
		return $packet;
	}
717713
public function read($length)
    {
        if ($length < 1) {
            return '';
        }

        $buffer = fread($this->stream, $length);

        if ($buffer === false) {
            throw new RuntimeException('Failed to read ' . $length . ' bytes');
        }

        return $buffer;
    }
1170666
public function stream_read($length)
    {
        if ($this->fileHandle === null) {
            return false;
        }
        self::restore();
        $content = fread($this->fileHandle, $length);
        self::register();
        return $content;
    }
1281281
public function receive(int $length = self::DEFAULT_RECEIVE_LENGTH): string
    {
        $buffer = '';
        $metadata['unread_bytes'] = 0;
        $makeBlockingAfterRead = false;

        try {
            do {
                // feof means socket has been closed
                // also, sometimes in long running processes the system seems to kill the underlying socket
                if (!$this->socket || feof($this->socket)) {
                    $this->disconnect();

                    return $buffer;
                }

                $result = fread($this->socket, $length);

                if ($makeBlockingAfterRead) {
                    stream_set_blocking($this->socket, true);
                    $makeBlockingAfterRead = false;
                }

                // fread FALSE means socket has been closed
                if ($result === false) {
                    $this->disconnect();

                    return $buffer;
                }

                $buffer .= $result;

                // feof means socket has been closed
                if (feof($this->socket)) {
                    $this->disconnect();

                    return $buffer;
                }

                $continue = false;

                if (strlen($result) == 1) {
                    // Workaround Chrome behavior (still needed?)
                    $continue = true;
                }

                if (strlen($result) == $length) {
                    $continue = true;
                }

                // Continue if more data to be read
                $metadata = stream_get_meta_data($this->socket);

                if ($metadata && isset($metadata['unread_bytes'])) {
                    if (!$metadata['unread_bytes']) {
                        // stop it, if we read a full message in previous time
                        $continue = false;
                    } else {
                        $continue = true;
                        // it makes sense only if unread_bytes less than DEFAULT_RECEIVE_LENGTH
                        if ($length > $metadata['unread_bytes']) {
                            // http://php.net/manual/en/function.stream-get-meta-data.php
                            // 'unread_bytes' don't describes real length correctly.
                            //$length = $metadata['unread_bytes'];

                            // Socket is a blocking by default. When we do a blocking read from an empty
                            // queue it will block and the server will hang. https://bugs.php.net/bug.php?id=1739
                            stream_set_blocking($this->socket, false);
                            $makeBlockingAfterRead = true;
                        }
                    }
                }
            } while ($continue);

            return $buffer;
        } finally {
            if ($this->socket && !feof($this->socket) && $makeBlockingAfterRead) {
                stream_set_blocking($this->socket, true);
            }
        }
    }
108362
public function readLine(int $length = 8192): string
    {
        return iconv($this->charset, 'UTF-8', $this->inputStream->readLine($length));
    }
838131
public function read($length)
    {
        $buffer = '';
        $total = count($this->streams) - 1;
        $remaining = $length;

        while ($remaining > 0) {
            // Progress to the next stream if needed.
            if ($this->streams[$this->current]->eof()) {
                if ($this->current == $total) {
                    break;
                }
                $this->current++;
            }
            $buffer .= $this->streams[$this->current]->read($remaining);
            $remaining = $length - strlen($buffer);
        }

        $this->pos += strlen($buffer);

        return $buffer;
    }
202814
public function read($length)
    {
        $buffer = '';
        $total = count($this->streams) - 1;
        $remaining = $length;
        $progressToNext = false;

        while ($remaining > 0) {

            // Progress to the next stream if needed.
            if ($progressToNext || $this->streams[$this->current]->eof()) {
                $progressToNext = false;
                if ($this->current === $total) {
                    break;
                }
                $this->current++;
            }

            $result = $this->streams[$this->current]->read($remaining);

            // Using a loose comparison here to match on '', false, and null
            if ($result == null) {
                $progressToNext = true;
                continue;
            }

            $buffer .= $result;
            $remaining = $length - strlen($buffer);
        }

        $this->pos += strlen($buffer);

        return $buffer;
    }
1025942
public function read($length)
    {
        $index = 0;
        $contents = '';
        while ($this->iterator->valid() && $index < $length) {
            if ($this->stringify !== null) {
                $contents .= $this->stringify->call($this, $this->iterator->current());
            } else {
                $contents .= $this->iterator->current();
            }

            $this->iterator->next();
            ++$this->position;
            ++$index;
        }

        return $contents;
    }
402472
public function readline($length=null) {
		$this->assertReadable();
		return $this->format->readline($this->file, $length);
	}
86951
public function readBytes($length)
    {
        if (($length + $this->_needle) > $this->_streamLength) {
            throw new Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length);
        }
        $bytes = $this->_mbStringFunctionsOverloaded ? mb_substr($this->_stream, $this->_needle, $length, '8bit') : substr($this->_stream, $this->_needle, $length);
        $this->_needle+= $length;
        return $bytes;
    }
77498
public function read($length = 4096)
    {
        $data = false;
        $this->_checkFileOpened();
        if ($length > 0) {
            $data = $this->_read($length);
        }

        return $data;
    }
859570
public function read($length)
    {
        $buffer = $this->socket->read($length);

        if ($this->state < self::STATE_OPEN) {
            $this->handshakeBuffer .= $buffer;

            $headerPos = mb_strpos(
                $this->handshakeBuffer,
                "\r\n\r\n",
                0,
                '8bit'
            );

            if ($headerPos !== false) {
                $data = mb_substr(
                    $this->handshakeBuffer,
                    0,
                    $headerPos,
                    '8bit'
                );

                try {
                    $this->handleHandshake($data);
                } catch (Net_Notifier_WebSocket_ProtocolException $e) {
                    $this->state = self::STATE_CLOSED;
                    $this->shutdown();
                }

                $length = mb_strlen($this->handshakeBuffer, '8bit');
                $buffer = mb_substr(
                    $this->handshakeBuffer,
                    $headerPos + 4,
                    $length - $headerPos - 5,
                    '8bit'
                );
            }
        }

        if ($this->state > self::STATE_CONNECTING) {
            $frames = $this->parser->parse($buffer);

            foreach ($frames as $frame) {
                $this->handleFrame($frame);
            }
        }

        return (
               count($this->textMessages) > 0
            || count($this->binaryMessages) > 0
        );
    }
119774
public function read(int $length = 8192): string
    {
        $bytes           = substr($this->buffer, $this->position, $length);
        $this->position += strlen($bytes);
        return $bytes;
    }
173550
public function read(int $length): string
    {
        $data = @fread($this->handle, $length);

        if (false === $data || '' === $data) {
            $message = 'Could not read from the file.';
            if ($error = error_get_last()) {
                $message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
            }
            throw new FileException($message);
        }

        return $data;
    }
892159
public function stream_read($length)
    {
        assert(null !== $this->handle);

        return fread($this->handle, $length);
    }
82976
public function read($bytes)
    {
        $data = substr($this->str, $this->pos, $bytes);

        $this->seekto($this->pos + $bytes);

        return $data;
    }
280440
public function read($length)
    {
        $size = $this->response->getHeader('Content-Length')[0];
        if ($size - $this->tell() < $length) {
            // Don't try to read further than the end of the stream.
            $length = $size - $this->tell();
        }

        return $this->response->getBody()->read($length);
    }