Adds chunk_size to CompressingFileReader.

Before when iterating using this object, the call to the underlying file object
was made without a chunk_size.  This is bad because this would cause the entire
contents of the file to be read.

Change-Id: I9956e5d2d693a6260252fff331d4f78f70179a6c
This commit is contained in:
Greg Lange 2013-05-01 21:16:18 +00:00
parent d69fa437cd
commit 5fda0c9a77

View File

@ -49,9 +49,11 @@ class CompressingFileReader(object):
:param file_obj: File object to wrap.
:param compresslevel: Compression level, defaults to 9.
:param chunk_size: Size of chunks read when iterating using object,
defaults to 4096.
"""
def __init__(self, file_obj, compresslevel=9):
def __init__(self, file_obj, compresslevel=9, chunk_size=4096):
self._f = file_obj
self._compressor = compressobj(
compresslevel, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL,
@ -60,6 +62,7 @@ class CompressingFileReader(object):
self.first = True
self.crc32 = 0
self.total_size = 0
self.chunk_size = chunk_size
def read(self, *a, **kw):
"""
@ -96,7 +99,7 @@ class CompressingFileReader(object):
return self
def next(self):
chunk = self.read()
chunk = self.read(self.chunk_size)
if chunk:
return chunk
raise StopIteration