1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
<?php
namespace PhpAmqpLib\Wire;
use PhpAmqpLib\Exception\AMQPDataReadException;
class AMQPBufferReader extends AMQPReader
{
/**
* @var string
*/
private $buffer;
/**
* @var int
*/
private $length;
public function __construct(string $buffer)
{
$this->buffer = $buffer;
$this->length = mb_strlen($buffer, 'ASCII');
}
public function close(): void
{
}
/**
* Resets the object from the injected param
*
* Used to not need to create a new AMQPBufferReader instance every time.
* when we can just pass a string and reset the object state.
* NOTE: since we are working with strings we don't need to pass an AbstractIO
* or a timeout.
*
* @param string $str
*/
public function reset(string $str): void
{
$this->buffer = $str;
$this->length = mb_strlen($this->buffer, 'ASCII');
$this->offset = 0;
$this->resetCounters();
}
protected function rawread(int $n): string
{
if ($this->length < $n) {
throw new AMQPDataReadException(sprintf(
'Error reading data. Requested %s bytes while string buffer has only %s',
$n,
$this->length
));
}
$res = mb_substr($this->buffer, 0, $n, 'ASCII');
$this->buffer = mb_substr($this->buffer, $n, null, 'ASCII');
$this->length -= $n;
$this->offset += $n;
return $res;
}
}
|