OLD | NEW |
1 # Copyright 2011, Google Inc. | 1 # Copyright 2011, Google Inc. |
2 # All rights reserved. | 2 # All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 flushes using the algorithm described in the RFC1979 section 2.1. | 324 flushes using the algorithm described in the RFC1979 section 2.1. |
325 """ | 325 """ |
326 | 326 |
327 def __init__(self, window_bits, no_context_takeover): | 327 def __init__(self, window_bits, no_context_takeover): |
328 self._deflater = None | 328 self._deflater = None |
329 if window_bits is None: | 329 if window_bits is None: |
330 window_bits = zlib.MAX_WBITS | 330 window_bits = zlib.MAX_WBITS |
331 self._window_bits = window_bits | 331 self._window_bits = window_bits |
332 self._no_context_takeover = no_context_takeover | 332 self._no_context_takeover = no_context_takeover |
333 | 333 |
334 def filter(self, bytes, flush=True, bfinal=False): | 334 def filter(self, bytes, end=True, bfinal=False): |
335 if self._deflater is None or (self._no_context_takeover and flush): | 335 if self._deflater is None: |
336 self._deflater = _Deflater(self._window_bits) | 336 self._deflater = _Deflater(self._window_bits) |
337 | 337 |
338 if bfinal: | 338 if bfinal: |
339 result = self._deflater.compress_and_finish(bytes) | 339 result = self._deflater.compress_and_finish(bytes) |
340 # Add a padding block with BFINAL = 0 and BTYPE = 0. | 340 # Add a padding block with BFINAL = 0 and BTYPE = 0. |
341 result = result + chr(0) | 341 result = result + chr(0) |
342 self._deflater = None | 342 self._deflater = None |
343 return result | 343 return result |
344 if flush: | 344 |
| 345 result = self._deflater.compress_and_flush(bytes) |
| 346 if end: |
345 # Strip last 4 octets which is LEN and NLEN field of a | 347 # Strip last 4 octets which is LEN and NLEN field of a |
346 # non-compressed block added for Z_SYNC_FLUSH. | 348 # non-compressed block added for Z_SYNC_FLUSH. |
347 return self._deflater.compress_and_flush(bytes)[:-4] | 349 result = result[:-4] |
348 return self._deflater.compress(bytes) | 350 |
| 351 if self._no_context_takeover and end: |
| 352 self._deflater = None |
| 353 |
| 354 return result |
349 | 355 |
350 | 356 |
351 class _RFC1979Inflater(object): | 357 class _RFC1979Inflater(object): |
352 """A decompressor class for byte sequence compressed and flushed following | 358 """A decompressor class for byte sequence compressed and flushed following |
353 the algorithm described in the RFC1979 section 2.1. | 359 the algorithm described in the RFC1979 section 2.1. |
354 """ | 360 """ |
355 | 361 |
356 def __init__(self, window_bits=zlib.MAX_WBITS): | 362 def __init__(self, window_bits=zlib.MAX_WBITS): |
357 self._inflater = _Inflater(window_bits) | 363 self._inflater = _Inflater(window_bits) |
358 | 364 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 407 |
402 def sendall(self, bytes): | 408 def sendall(self, bytes): |
403 self.send(bytes) | 409 self.send(bytes) |
404 | 410 |
405 def send(self, bytes): | 411 def send(self, bytes): |
406 self._socket.sendall(self._deflater.compress_and_flush(bytes)) | 412 self._socket.sendall(self._deflater.compress_and_flush(bytes)) |
407 return len(bytes) | 413 return len(bytes) |
408 | 414 |
409 | 415 |
410 # vi:sts=4 sw=4 et | 416 # vi:sts=4 sw=4 et |
OLD | NEW |