~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-16 19:21:06 UTC
  • mto: (3735.2.156 brisbane-core)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090316192106-iz7ccmx58s0ff3aa
Add tests for the ability to do partial decompression without knowing the final length.

Show diffs side-by-side

added added

removed removed

Lines of Context:
213
213
        """
214
214
        if num_bytes is None:
215
215
            num_bytes = self._content_length
216
 
        assert num_bytes <= self._content_length
 
216
        if self._content_length is not None:
 
217
            assert num_bytes <= self._content_length
217
218
        if self._content is None:
218
219
            assert self._z_content is not None
219
220
            if self._z_content == '':
233
234
                # decompressors 'unconsumed_tail'
234
235
            self._z_content = None
235
236
        # Do we have enough bytes already?
236
 
        if len(self._content) >= num_bytes:
 
237
        if num_bytes is not None and len(self._content) >= num_bytes:
237
238
            return
238
239
        # If we got this far, and don't have a decompressor, something is wrong
239
240
        assert self._z_content_decompressor is not None
240
241
        remaining_decomp = self._z_content_decompressor.unconsumed_tail
241
 
        # If we have nothing left to decomp, we ran out of decomp bytes
242
 
        assert remaining_decomp
243
 
        needed_bytes = num_bytes - len(self._content)
244
 
        # We always set max_size to 32kB over the minimum needed, so that zlib
245
 
        # will give us as much as we really want.
246
 
        # TODO: If this isn't good enough, we could make a loop here, that
247
 
        #       keeps expanding the request until we get enough
248
 
        self._content += self._z_content_decompressor.decompress(
249
 
            remaining_decomp, needed_bytes + _ZLIB_DECOMP_WINDOW)
250
 
        assert len(self._content) >= num_bytes
251
 
        if not self._z_content_decompressor.unconsumed_tail:
252
 
            # The stream is finished
253
 
            self._z_content_decompressor = None
 
242
        if num_bytes is None:
 
243
            if remaining_decomp:
 
244
                # We don't know how much is left, but we'll decompress it all
 
245
                self._content += self._z_content_decompressor.decompress(
 
246
                    remaining_decomp)
 
247
                # Note: There what I consider a bug in zlib.decompressobj
 
248
                #       If you pass back in the entire unconsumed_tail, only
 
249
                #       this time you don't pass a max-size, it doesn't
 
250
                #       change the unconsumed_tail back to None/''.
 
251
                #       However, we know we are done with the whole stream
 
252
                self._z_content_decompressor = None
 
253
            self._content_length = len(self._content)
 
254
        else:
 
255
            # If we have nothing left to decomp, we ran out of decomp bytes
 
256
            assert remaining_decomp
 
257
            needed_bytes = num_bytes - len(self._content)
 
258
            # We always set max_size to 32kB over the minimum needed, so that zlib
 
259
            # will give us as much as we really want.
 
260
            # TODO: If this isn't good enough, we could make a loop here, that
 
261
            #       keeps expanding the request until we get enough
 
262
            self._content += self._z_content_decompressor.decompress(
 
263
                remaining_decomp, needed_bytes + _ZLIB_DECOMP_WINDOW)
 
264
            assert len(self._content) >= num_bytes
 
265
            if not self._z_content_decompressor.unconsumed_tail:
 
266
                # The stream is finished
 
267
                self._z_content_decompressor = None
254
268
 
255
269
    def _parse_bytes(self, bytes):
256
270
        """Read the various lengths from the header.