Debian Codegraph @

docs/lz4.stream.rst

raw
1 .. default-role:: obj
2
3
4 lz4.stream sub-package
5 ======================
6
7 .. warning::
8 This module is unmaintained.
9
10 This sub-package is considered experimental. It was submitted by a community
11 member who is not able to continue to maintain the module.
12
13 This module is not built as part of the distributed wheels. If you wish to
14 build and use this module you will need to download and build from source
15 with the environment variable PYLZ4_EXPERIMENTAL set to TRUE.
16
17 The module needs some re-write, and the tests need extensive work, for this
18 to become production ready. If you are interested in working on this, please
19 reach out to the package maintainers.
20
21 This sub-package provides the capability to compress and decompress data using
22 the `stream specification
23 <https://github.com/lz4/lz4/blob/master/examples/streaming_api_basics.md>`_,
24 especially the `stream specification based on a double buffer
25 <https://github.com/lz4/lz4/blob/master/examples/blockStreaming_doubleBuffer.md>`_.
26
27 Because the LZ4 stream format does not define a container format, the
28 Python bindings will by default insert the compressed data size as an
29 integer at the start of the compressed payload. However, it is
30 possible to set the bit depth of this compressed data size.
31
32 So far, only the double-buffer based approach is implemented.
33
34 Example usage
35 -------------
36 To use the lz4 stream format bindings is straightforward:
37
38 .. doctest::
39
40 >>> from lz4.stream import LZ4StreamCompressor, LZ4StreamDecompressor
41 >>> import os
42 >>> block_size_length = 2 # LZ4 compressed block size stored on 2 bytes
43 >>> page_size = 8192 # LZ4 context double buffer page size
44 >>> origin_stream = 10 * 1024 * os.urandom(1024) # 10MiB
45 >>> # LZ4 stream compression of origin_stream into compressed_stream:
46 >>> compressed_stream = bytearray()
47 >>> with LZ4StreamCompressor("double_buffer", page_size, store_comp_size=block_size_length) as proc:
48 ... offset = 0
49 ... while offset < len(origin_stream):
50 ... chunk = origin_stream[offset:offset + page_size]
51 ... block = proc.compress(chunk)
52 ... compressed_stream.extend(block)
53 ... offset += page_size
54 >>> # LZ4 stream decompression of compressed_stream into decompressed_stream:
55 >>> decompressed_stream = bytearray()
56 >>> with LZ4StreamDecompressor("double_buffer", page_size, store_comp_size=block_size_length) as proc:
57 ... offset = 0
58 ... while offset < len(compressed_stream):
59 ... block = proc.get_block(compressed_stream[offset:])
60 ... chunk = proc.decompress(block)
61 ... decompressed_stream.extend(chunk)
62 ... offset += block_size_length + len(block)
63 >>> decompressed_stream == origin_stream
64 True
65
66 Out-of-band block size record example
67 -------------------------------------
68 .. doctest::
69
70 >>> from lz4.stream import LZ4StreamCompressor, LZ4StreamDecompressor
71 >>> import os
72 >>> page_size = 8192 # LZ4 context double buffer page size
73 >>> out_of_band_block_sizes = [] # Store the block sizes
74 >>> origin_stream = 10 * 1024 * os.urandom(1024) # 10MiB
75 >>> # LZ4 stream compression of origin_stream into compressed_stream:
76 >>> compressed_stream = bytearray()
77 >>> with LZ4StreamCompressor("double_buffer", page_size, store_comp_size=0) as proc:
78 ... offset = 0
79 ... while offset < len(origin_stream):
80 ... chunk = origin_stream[offset:offset + page_size]
81 ... block = proc.compress(chunk)
82 ... out_of_band_block_sizes.append(len(block))
83 ... compressed_stream.extend(block)
84 ... offset += page_size
85 >>> # LZ4 stream decompression of compressed_stream into decompressed_stream:
86 >>> decompressed_stream = bytearray()
87 >>> with LZ4StreamDecompressor("double_buffer", page_size, store_comp_size=0) as proc:
88 ... offset = 0
89 ... for block_len in out_of_band_block_sizes:
90 ... # Sanity check:
91 ... if offset >= len(compressed_stream):
92 ... raise LZ4StreamError("Truncated stream")
93 ... block = compressed_stream[offset:offset + block_len]
94 ... chunk = proc.decompress(block)
95 ... decompressed_stream.extend(chunk)
96 ... offset += block_len
97 >>> decompressed_stream == origin_stream
98 True
99
100 Contents
101 ----------------
102
103 .. automodule:: lz4.stream
104 :members: LZ4StreamCompressor, LZ4StreamDecompressor