Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: LimitStream.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use Psr\Http\Message\StreamInterface;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Decorator used to return only a subset of a stream.
[7] Fix | Delete
*
[8] Fix | Delete
* @final
[9] Fix | Delete
*/
[10] Fix | Delete
class LimitStream implements StreamInterface
[11] Fix | Delete
{
[12] Fix | Delete
use StreamDecoratorTrait;
[13] Fix | Delete
[14] Fix | Delete
/** @var int Offset to start reading from */
[15] Fix | Delete
private $offset;
[16] Fix | Delete
[17] Fix | Delete
/** @var int Limit the number of bytes that can be read */
[18] Fix | Delete
private $limit;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* @param StreamInterface $stream Stream to wrap
[22] Fix | Delete
* @param int $limit Total number of bytes to allow to be read
[23] Fix | Delete
* from the stream. Pass -1 for no limit.
[24] Fix | Delete
* @param int $offset Position to seek to before reading (only
[25] Fix | Delete
* works on seekable streams).
[26] Fix | Delete
*/
[27] Fix | Delete
public function __construct(
[28] Fix | Delete
StreamInterface $stream,
[29] Fix | Delete
$limit = -1,
[30] Fix | Delete
$offset = 0
[31] Fix | Delete
) {
[32] Fix | Delete
$this->stream = $stream;
[33] Fix | Delete
$this->setLimit($limit);
[34] Fix | Delete
$this->setOffset($offset);
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
public function eof()
[38] Fix | Delete
{
[39] Fix | Delete
// Always return true if the underlying stream is EOF
[40] Fix | Delete
if ($this->stream->eof()) {
[41] Fix | Delete
return true;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
// No limit and the underlying stream is not at EOF
[45] Fix | Delete
if ($this->limit == -1) {
[46] Fix | Delete
return false;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
return $this->stream->tell() >= $this->offset + $this->limit;
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Returns the size of the limited subset of data
[54] Fix | Delete
* {@inheritdoc}
[55] Fix | Delete
*/
[56] Fix | Delete
public function getSize()
[57] Fix | Delete
{
[58] Fix | Delete
if (null === ($length = $this->stream->getSize())) {
[59] Fix | Delete
return null;
[60] Fix | Delete
} elseif ($this->limit == -1) {
[61] Fix | Delete
return $length - $this->offset;
[62] Fix | Delete
} else {
[63] Fix | Delete
return min($this->limit, $length - $this->offset);
[64] Fix | Delete
}
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Allow for a bounded seek on the read limited stream
[69] Fix | Delete
* {@inheritdoc}
[70] Fix | Delete
*/
[71] Fix | Delete
public function seek($offset, $whence = SEEK_SET)
[72] Fix | Delete
{
[73] Fix | Delete
if ($whence !== SEEK_SET || $offset < 0) {
[74] Fix | Delete
throw new \RuntimeException(sprintf(
[75] Fix | Delete
'Cannot seek to offset %s with whence %s',
[76] Fix | Delete
$offset,
[77] Fix | Delete
$whence
[78] Fix | Delete
));
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
$offset += $this->offset;
[82] Fix | Delete
[83] Fix | Delete
if ($this->limit !== -1) {
[84] Fix | Delete
if ($offset > $this->offset + $this->limit) {
[85] Fix | Delete
$offset = $this->offset + $this->limit;
[86] Fix | Delete
}
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
$this->stream->seek($offset);
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Give a relative tell()
[94] Fix | Delete
* {@inheritdoc}
[95] Fix | Delete
*/
[96] Fix | Delete
public function tell()
[97] Fix | Delete
{
[98] Fix | Delete
return $this->stream->tell() - $this->offset;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
/**
[102] Fix | Delete
* Set the offset to start limiting from
[103] Fix | Delete
*
[104] Fix | Delete
* @param int $offset Offset to seek to and begin byte limiting from
[105] Fix | Delete
*
[106] Fix | Delete
* @throws \RuntimeException if the stream cannot be seeked.
[107] Fix | Delete
*/
[108] Fix | Delete
public function setOffset($offset)
[109] Fix | Delete
{
[110] Fix | Delete
$current = $this->stream->tell();
[111] Fix | Delete
[112] Fix | Delete
if ($current !== $offset) {
[113] Fix | Delete
// If the stream cannot seek to the offset position, then read to it
[114] Fix | Delete
if ($this->stream->isSeekable()) {
[115] Fix | Delete
$this->stream->seek($offset);
[116] Fix | Delete
} elseif ($current > $offset) {
[117] Fix | Delete
throw new \RuntimeException("Could not seek to stream offset $offset");
[118] Fix | Delete
} else {
[119] Fix | Delete
$this->stream->read($offset - $current);
[120] Fix | Delete
}
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
$this->offset = $offset;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Set the limit of bytes that the decorator allows to be read from the
[128] Fix | Delete
* stream.
[129] Fix | Delete
*
[130] Fix | Delete
* @param int $limit Number of bytes to allow to be read from the stream.
[131] Fix | Delete
* Use -1 for no limit.
[132] Fix | Delete
*/
[133] Fix | Delete
public function setLimit($limit)
[134] Fix | Delete
{
[135] Fix | Delete
$this->limit = $limit;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
public function read($length)
[139] Fix | Delete
{
[140] Fix | Delete
if ($this->limit == -1) {
[141] Fix | Delete
return $this->stream->read($length);
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
// Check if the current position is less than the total allowed
[145] Fix | Delete
// bytes + original offset
[146] Fix | Delete
$remaining = ($this->offset + $this->limit) - $this->stream->tell();
[147] Fix | Delete
if ($remaining > 0) {
[148] Fix | Delete
// Only return the amount of requested data, ensuring that the byte
[149] Fix | Delete
// limit is not exceeded
[150] Fix | Delete
return $this->stream->read(min($remaining, $length));
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
return '';
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function