Debian Codegraph @

docs/lz4.block.rst

raw
1 .. default-role:: obj
2
3
4 lz4.block sub-package
5 =====================
6
7 This sub-package provides the capability to compress and decompress data using
8 the `block specification <https://lz4.github.io/lz4/lz4_Block_format.html>`_.
9
10 Because the LZ4 block format doesn't define a container format, the
11 Python bindings will by default insert the original data size as an
12 integer at the start of the compressed payload. However, it is
13 possible to disable this functionality, and you may wish to do so for
14 compatibility with other language bindings, such as the `Java bindings
15 <https://github.com/lz4/lz4-java>`_.
16
17
18
19 Example usage
20 -------------
21 To use the lz4 block format bindings is straightforward:
22
23 .. doctest::
24
25 >>> import lz4.block
26 >>> import os
27 >>> input_data = 20 * 128 * os.urandom(1024) # Read 20 * 128kb
28 >>> compressed_data = lz4.block.compress(input_data)
29 >>> output_data = lz4.block.decompress(compressed_data)
30 >>> input_data == output_data
31 True
32
33 In this simple example, the size of the uncompressed data is stored in
34 the compressed data, and this size is then utilized when uncompressing
35 the data in order to correctly size the buffer. Instead, you may want
36 to not store the size of the uncompressed data to ensure compatibility
37 with the `Java bindings <https://github.com/lz4/lz4-java>`_. The
38 example below demonstrates how to use the block format without storing
39 the size of the uncompressed data.
40
41 .. doctest::
42
43 >>> import lz4.block
44 >>> data = b'0' * 255
45 >>> compressed = lz4.block.compress(data, store_size=False)
46 >>> decompressed = lz4.block.decompress(compressed, uncompressed_size=255)
47 >>> decompressed == data
48 True
49
50 The `uncompressed_size` argument specifies an upper bound on the size
51 of the uncompressed data size rather than an absolute value, such that
52 the following example also works.
53
54 .. doctest::
55
56 >>> import lz4.block
57 >>> data = b'0' * 255
58 >>> compressed = lz4.block.compress(data, store_size=False)
59 >>> decompressed = lz4.block.decompress(compressed, uncompressed_size=2048)
60 >>> decompressed == data
61 True
62
63 A common situation is not knowing the size of the uncompressed data at
64 decompression time. The following example illustrates a strategy that
65 can be used in this case.
66
67 .. doctest::
68
69 >>> import lz4.block
70 >>> data = b'0' * 2048
71 >>> compressed = lz4.block.compress(data, store_size=False)
72 >>> usize = 255
73 >>> max_size = 4096
74 >>> while True:
75 ... try:
76 ... decompressed = lz4.block.decompress(compressed, uncompressed_size=usize)
77 ... break
78 ... except lz4.block.LZ4BlockError:
79 ... usize *= 2
80 ... if usize > max_size:
81 ... print('Error: data too large or corrupt')
82 ... break
83 >>> decompressed == data
84 True
85
86 In this example we are catching the `lz4.block.LZ4BlockError`
87 exception. This exception is raised if the LZ4 library call fails,
88 which can be caused by either the buffer used to store the
89 uncompressed data (as set by `usize`) being too small, or the input
90 compressed data being invalid - it is not possible to distinguish the
91 two cases, and this is why we set an absolute upper bound (`max_size`)
92 on the memory that can be allocated for the uncompressed data. If we
93 did not take this precaution, the code, if ppassed invalid compressed
94 data would continuously try to allocate a larger and larger buffer for
95 decompression until the system ran out of memory.
96
97 Contents
98 ----------------
99
100 .. automodule:: lz4.block
101 :members: compress, decompress
102