private function calculatePosition($offset, $whence)
    {
        if ($whence === SEEK_CUR) {
            return max(0, $this->cursor + (int) $offset);
        } elseif ($whence === SEEK_SET) {
            return max(0, (int) $offset);
        } elseif ($whence === SEEK_END) {
            throw new \RuntimeException('Cannot set cursor position from the end of a JSON stream');
        }

        throw new \InvalidArgumentException("Invalid cursor relative position '$whence'");
    }