-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: zip stream repeats initial bytes
- Loading branch information
Showing
2 changed files
with
31 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from io import RawIOBase | ||
|
||
|
||
class MemoryBuffer(RawIOBase): | ||
""" | ||
A buffer class that supports reading and writing binary data. | ||
The buffer automatically resets upon reading to make sure all data is read only once. | ||
""" | ||
|
||
def __init__(self): | ||
self._buffer = b'' | ||
|
||
def write(self, data): | ||
if self.closed: | ||
raise ValueError('write to closed file') | ||
self._buffer += data | ||
return len(data) | ||
|
||
def read(self, size=-1): | ||
if self.closed: | ||
raise ValueError('read from closed file') | ||
if size < 0: | ||
data = self._buffer | ||
self._buffer = b'' | ||
else: | ||
data = self._buffer[:size] | ||
self._buffer = self._buffer[size:] | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters