1 #ifndef MSP_IO_ZLIBCOMPRESSED_H_
2 #define MSP_IO_ZLIBCOMPRESSED_H_
6 #include <sigc++/trackable.h>
7 #include <msp/core/mspcore_api.h>
13 class MSPCORE_API zlib_error: public std::runtime_error
19 zlib_error(const std::string &, int);
21 int code() const noexcept { return m_code; }
25 Compresses or decompresses data with zlib. This class is a filter that
26 operates on top of another I/O object.
28 To ensure proper termination of the compressed data stream, the ZlibCompressed
29 object must be destroyed before the underlying object is closed.
31 class MSPCORE_API ZlibCompressed: public Base, public sigc::trackable
37 std::size_t buffer_size = 1024;
38 unsigned char *in_buffer = nullptr;
39 unsigned char *out_buffer = nullptr;
40 bool stream_end = false;
41 Private *priv = nullptr;
44 /** Creates a zlib de/compression object. The underlying object must be
45 open for reading or writing, not both. The level parameter determines
46 compression quality, ranging from 1 (fastest) to 9 (best compression). */
47 ZlibCompressed(Base &b, unsigned level = 9): ZlibCompressed(b, b.get_mode()&M_RDWR, level) { }
49 /** Creates a zlib de/compression object. Mode must be either read or
50 write, and compatible with the underlying object. */
51 ZlibCompressed(Base &, Mode, unsigned level = 9);
53 ~ZlibCompressed() override;
55 void set_block(bool) override;
56 void set_inherit(bool) override;
61 std::size_t do_write(const char *, std::size_t) override;
64 /** Compresses data and writes it to the underlying object. Returns true if
65 progress was made, false otherwise. flush_mode can be any of zlib's flush
66 modes. If it is Z_FINISH, false is also returned when the stream has been
68 bool compress_data(int flush_mode);
71 std::size_t do_read(char *, std::size_t) override;
73 const Handle &get_handle(Mode) override;