diff --git a/lazip.py b/lazip.py index 7cd1eda..e7940ec 100644 --- a/lazip.py +++ b/lazip.py @@ -229,6 +229,9 @@ class Filazy(ReadOnlyBinaryIOWrapper): def ensure(self, start: int, end: int) -> None: """Download bytes from start to end inclusively.""" + offset = self.chunk_size - 1 + start = max(0, min(start, end-offset)) + end = min(self.length-1, max(end, start+offset)) with self.stay(): i, j = bisect_left(self.right, start), bisect_right(self.left, end) for start, end in self.merge(start, end, i, j):