public function chunk(int $length): Generator
    {
        if ($length < 1) {
            throw new Exception(sprintf('%s() expects the length to be a positive integer %d given', __METHOD__, $length));
        }

        $input_bom = $this->getInputBOM();
        $this->document->rewind();
        $this->document->fseek(strlen($input_bom));
        foreach (str_split($this->output_bom.$this->document->fread($length), $length) as $chunk) {
            yield $chunk;
        }

        while ($this->document->valid()) {
            yield $this->document->fread($length);
        }
    }