@@ -247,6 +247,27 @@ Compressing and decompressing data in memory
247247 The *mode * argument is a :class: `ZstdCompressor ` attribute, either
248248 :attr: `~.FLUSH_BLOCK `, or :attr: `~.FLUSH_FRAME `.
249249
250+ .. method :: set_pledged_input_size(size)
251+
252+ Specify the amount of uncompressed data *size * that will be provided for
253+ the next frame. *size * will be written into the frame header of the next
254+ frame unless :attr: `CompressionParameter.content_size_flag ` is ``False ``
255+ or ``0 ``. A size of ``0 `` means that the frame is empty. If *size * is
256+ ``None ``, the frame header will omit the frame size. Frames that include
257+ the uncompressed data size require less memory to decompress, especially
258+ at higher compression levels.
259+
260+ If :attr: `last_mode ` is not :attr: `FLUSH_FRAME `, a
261+ :exc: `ValueError ` is raised as the compressor is not at the start of
262+ a frame. If the pledged size does not match the actual size of data
263+ provided to :meth: `.compress `, future calls to :meth: `!compress ` or
264+ :meth: `flush ` may raise :exc: `ZstdError ` and the last chunk of data may
265+ be lost.
266+
267+ After :meth: `flush ` or :meth: `.compress ` are called with mode
268+ :attr: `FLUSH_FRAME `, the next frame will not include the frame size into
269+ the header unless :meth: `!set_pledged_input_size ` is called again.
270+
250271 .. attribute :: CONTINUE
251272
252273 Collect more data for compression, which may or may not generate output
@@ -266,6 +287,13 @@ Compressing and decompressing data in memory
266287 :meth: `~.compress ` will be written into a new frame and
267288 *cannot * reference past data.
268289
290+ .. attribute :: last_mode
291+
292+ The last mode passed to either :meth: `~.compress ` or :meth: `~.flush `.
293+ The value can be one of :attr: `~.CONTINUE `, :attr: `~.FLUSH_BLOCK `, or
294+ :attr: `~.FLUSH_FRAME `. The initial value is :attr: `~.FLUSH_FRAME `,
295+ signifying that the compressor is at the start of a new frame.
296+
269297
270298.. class :: ZstdDecompressor(zstd_dict=None, options=None)
271299
@@ -620,12 +648,17 @@ Advanced parameter control
620648 Write the size of the data to be compressed into the Zstandard frame
621649 header when known prior to compressing.
622650
623- This flag only takes effect under the following two scenarios:
651+ This flag only takes effect under the following scenarios:
624652
625653 * Calling :func: `compress ` for one-shot compression
626654 * Providing all of the data to be compressed in the frame in a single
627655 :meth: `ZstdCompressor.compress ` call, with the
628656 :attr: `ZstdCompressor.FLUSH_FRAME ` mode.
657+ * Calling :meth: `ZstdCompressor.set_pledged_input_size ` with the exact
658+ amount of data that will be provided to the compressor prior to any
659+ calls to :meth: `ZstdCompressor.compress ` for the current frame.
660+ :meth: `!ZstdCompressor.set_pledged_input_size ` must be called for each
661+ new frame.
629662
630663 All other compression calls may not write the size information into the
631664 frame header.
0 commit comments