resource = $resource; } public function read(int $length): string { if (0 === $length) { return ''; } $data = fread($this->resource, $length); if (false === $data) { throw new RuntimeException('Unable to read the memory'); } if (mb_strlen($data, '8bit') !== $length) { throw new InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d.', $length, mb_strlen($data, '8bit'))); } return $data; } }