Phalcon\Db\Adapter\MongoDB\GridFS\ReadableStream::downloadNumBytes PHP Method

downloadNumBytes() public method

Note: this method may return a string smaller than the requested length if data is not available to be read.
public downloadNumBytes ( integer $numBytes ) : string
$numBytes integer Number of bytes to read
return string
    public function downloadNumBytes($numBytes)
    {
        if ($this->bufferFresh) {
            rewind($this->buffer);
            $this->bufferFresh = false;
        }
        // TODO: Should we be checking for fread errors here?
        $output = fread($this->buffer, $numBytes);
        if (strlen($output) == $numBytes) {
            return $output;
        }
        $this->initEmptyBuffer();
        $bytesLeft = $numBytes - strlen($output);
        while (strlen($output) < $numBytes && $this->advanceChunks()) {
            $bytesLeft = $numBytes - strlen($output);
            $output .= substr($this->chunksIterator->current()->data->getData(), 0, $bytesLeft);
        }
        if (!$this->iteratorEmpty && $this->file->length > 0 && $bytesLeft < strlen($this->chunksIterator->current()->data->getData())) {
            fwrite($this->buffer, substr($this->chunksIterator->current()->data->getData(), $bytesLeft));
            $this->bufferEmpty = false;
        }
        return $output;
    }