DecompressorStream¶
- class tinycompress.base.DecompressorStream(stream, decomp)[source]¶
Raw I/O stream that decompresses data from an underlying binary stream.
This class wraps a decompressor and binary input stream to create a readable stream interface that transparently decompresses data as it is read.
- Parameters:
stream (
BinaryIO) – The binary input stream containing compressed data.decomp (
BaseDecompressor) – The decompressor instance to use.
Attributes
closedMethods
Flush and close the IO object.
Return underlying file descriptor if one exists.
Flush write buffers, if applicable.
Return whether this is an 'interactive' stream.
Read and decompress up to size bytes from the stream.
Check if the stream is readable.
Read and decompress all remaining data from the stream.
Read decompressed data into a pre-allocated buffer.
Read and return a line from the stream.
Return a list of lines from the stream.
Change the stream position to the given byte offset.
Check if the stream supports seeking.
Get the current stream position in the decompressed data.
Truncate file to size bytes.
Return whether object was opened for writing.
writeWrite a list of lines to stream.
- __iter__()¶
Implement iter(self).
- __next__()¶
Implement next(self).
- close()¶
Flush and close the IO object.
This method has no effect if the file is already closed.
- fileno()¶
Return underlying file descriptor if one exists.
Raise OSError if the IO object does not use a file descriptor.
- flush()¶
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
- isatty()¶
Return whether this is an ‘interactive’ stream.
Return False if it can’t be determined.
- read(size=-1, /)[source]¶
Read and decompress up to size bytes from the stream.
- Parameters:
size (
int|None) – Maximum number of bytes to read and decompress. If None or negative, read until EOF.- Returns:
The decompressed data as bytes.
- readable()[source]¶
Check if the stream is readable.
- Returns:
True, as DecompressorStream is always readable.
- readall()[source]¶
Read and decompress all remaining data from the stream.
- Returns:
All remaining decompressed data as bytes.
- readinto(buffer)[source]¶
Read decompressed data into a pre-allocated buffer.
- Parameters:
buffer (
bytes|bytearray|memoryview) – A writable buffer to store the decompressed data.- Returns:
Number of bytes read and stored in the buffer.
- readline(size=-1, /)¶
Read and return a line from the stream.
If size is specified, at most size bytes will be read.
The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.
- readlines(hint=-1, /)¶
Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
- seek(offset, whence=0, /)[source]¶
Change the stream position to the given byte offset.
- Parameters:
offset (
int) – The offset to seek to, relative to the position specified by whence.whence (
int) – The reference point for offset. SEEK_SET: Start of stream (default). SEEK_CUR: Current position. SEEK_END: End of stream.
- Returns:
The new absolute position in the decompressed data stream.
- Raises:
ValueError – If whence is invalid.
Note
Seeking in a compressed stream requires: 1. For backwards seeks: Reset to start and decompress up to target 2. For forward seeks: Read and decompress data until target reached
- seekable()[source]¶
Check if the stream supports seeking.
- Returns:
True if the underlying stream is seekable, False otherwise.
- tell()[source]¶
Get the current stream position in the decompressed data.
- Returns:
Current position as number of bytes from start of decompressed data.
- truncate(size=None, /)¶
Truncate file to size bytes.
File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Return the new size.
- writable()¶
Return whether object was opened for writing.
If False, write() will raise OSError.
- writelines(lines, /)¶
Write a list of lines to stream.
Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.