Flush remaining data to the underlying stream. The mode argument can be FLUSH_BLOCK or FLUSH_FRAME. Abuse of this method will reduce compression ratio, use it only when necessary. If the program is interrupted afterwards, all data can be recovered. To ensure saving
(self, mode=FLUSH_BLOCK)
| 136 | return length |
| 137 | |
| 138 | def flush(self, mode=FLUSH_BLOCK): |
| 139 | """Flush remaining data to the underlying stream. |
| 140 | |
| 141 | The mode argument can be FLUSH_BLOCK or FLUSH_FRAME. Abuse of this |
| 142 | method will reduce compression ratio, use it only when necessary. |
| 143 | |
| 144 | If the program is interrupted afterwards, all data can be recovered. |
| 145 | To ensure saving to disk, also need to use os.fsync(fd). |
| 146 | |
| 147 | This method does nothing in reading mode. |
| 148 | """ |
| 149 | if self._mode == _MODE_READ: |
| 150 | return |
| 151 | self._check_not_closed() |
| 152 | if mode not in {self.FLUSH_BLOCK, self.FLUSH_FRAME}: |
| 153 | raise ValueError('Invalid mode argument, expected either ' |
| 154 | 'ZstdFile.FLUSH_FRAME or ' |
| 155 | 'ZstdFile.FLUSH_BLOCK') |
| 156 | if self._compressor.last_mode == mode: |
| 157 | return |
| 158 | # Flush zstd block/frame, and write. |
| 159 | data = self._compressor.flush(mode) |
| 160 | self._fp.write(data) |
| 161 | if hasattr(self._fp, 'flush'): |
| 162 | self._fp.flush() |
| 163 | |
| 164 | def read(self, size=-1): |
| 165 | """Read up to size uncompressed bytes from the file. |