137
138
% (num_bytes, self._content_length))
138
139
# Expand the content if required
139
140
if self._content is None:
141
if self._content_chunks is not None:
142
self._content = ''.join(self._content_chunks)
143
self._content_chunks = None
144
if self._content is None:
140
145
if self._z_content is None:
141
146
raise AssertionError('No content to decompress')
142
147
if self._z_content == '':
273
278
bytes = apply_delta_to_source(self._content, content_start, end)
281
def set_chunked_content(self, content_chunks):
282
"""Set the content of this block to the given chunks."""
283
self._content_length = sum(map(len, content_chunks))
284
self._content_chunks = content_chunks
285
self._z_content = None
276
288
def set_content(self, content):
277
289
"""Set the content of this block."""
278
290
self._content_length = len(content)
279
291
self._content = content
280
292
self._z_content = None
294
def _create_z_content_using_lzma(self):
295
if self._content_chunks is not None:
296
self._content = ''.join(self._content_chunks)
297
self._content_chunks = None
298
if self._content is None:
299
raise AssertionError('Nothing to compress')
300
self._z_content = pylzma.compress(self._content)
301
self._z_content_length = len(self._z_content)
303
def _create_z_content_from_chunks(self):
304
compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION)
305
compressed_chunks = []
306
for chunk in self._content_chunks:
307
z_bytes = compressor.compress(chunk)
309
compressed_chunks.append(z_bytes)
310
compressed_chunks.append(compressor.flush())
311
self._z_content = ''.join(compressed_chunks)
312
self._z_content_length = len(self._z_content)
314
def _create_z_content(self):
315
if self._z_content is not None:
318
self._create_z_content_using_lzma()
320
if self._content_chunks is not None:
321
self._create_z_content_from_chunks()
323
self._z_content = zlib.compress(self._content)
324
self._z_content_length = len(self._z_content)
282
326
def to_bytes(self):
283
327
"""Encode the information into a byte stream."""
284
compress = zlib.compress
286
compress = pylzma.compress
287
if self._z_content is None:
288
if self._content is None:
289
raise AssertionError('Nothing to compress')
290
self._z_content = compress(self._content)
291
self._z_content_length = len(self._z_content)
328
self._create_z_content()
293
330
header = self.GCB_LZ_HEADER