Skip to content

Commit

Permalink
Buffer class: when buffer overflow, then extend buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Aug 3, 2020
1 parent 280ea93 commit 7b8070a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions py7zr/helpers.py
Expand Up @@ -375,8 +375,11 @@ def __init__(self, size: int = 16):
def add(self, data: Union[bytes, bytearray, memoryview]):
length = len(data)
if length + self._buflen > self._size:
raise BufferOverflow() # pragma: no-cover
self._buf[self._buflen:self._buflen + length] = data
# extend buffer
self._buf = self._buf[:self._buflen] + data
self._size = self._buflen + length
else:
self._buf[self._buflen:self._buflen + length] = data
self._buflen += length
self.view = memoryview(self._buf[0:self._buflen])

Expand All @@ -387,8 +390,11 @@ def reset(self) -> None:
def set(self, data: Union[bytes, bytearray, memoryview]) -> None:
length = len(data)
if length > self._size:
raise BufferOverflow() # pragma: no-cover
self._buf[0:length] = data
# extend buffer
self._buf = bytearray(data)
self._size = length
else:
self._buf[0:length] = data
self._buflen = length
self.view = memoryview(self._buf[0:length])

Expand Down

0 comments on commit 7b8070a

Please sign in to comment.