1 | // |
2 | // DeflatingStream.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: ZLibStream |
7 | // |
8 | // Definition of the DeflatingStream class. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_DeflatingStream_INCLUDED |
18 | #define Foundation_DeflatingStream_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/BufferedStreamBuf.h" |
23 | #include <istream> |
24 | #include <ostream> |
25 | #if defined(POCO_UNBUNDLED) |
26 | #include <zlib.h> |
27 | #else |
28 | #include "Poco/zlib.h" |
29 | #endif |
30 | |
31 | |
32 | namespace Poco { |
33 | |
34 | |
35 | class Foundation_API DeflatingStreamBuf: public BufferedStreamBuf |
36 | /// This is the streambuf class used by DeflatingInputStream and DeflatingOutputStream. |
37 | /// The actual work is delegated to zlib (see http://zlib.net). |
38 | /// Both zlib (deflate) streams and gzip streams are supported. |
39 | /// Output streams should always call close() to ensure |
40 | /// proper completion of compression. |
41 | /// A compression level (0 to 9) can be specified in the constructor. |
42 | { |
43 | public: |
44 | enum StreamType |
45 | { |
46 | STREAM_ZLIB, /// Create a zlib header, use Adler-32 checksum. |
47 | STREAM_GZIP /// Create a gzip header, use CRC-32 checksum. |
48 | }; |
49 | |
50 | DeflatingStreamBuf(std::istream& istr, StreamType type, int level); |
51 | /// Creates a DeflatingStreamBuf for compressing data read |
52 | /// from the given input stream. |
53 | |
54 | DeflatingStreamBuf(std::istream& istr, int windowBits, int level); |
55 | /// Creates a DeflatingStreamBuf for compressing data read |
56 | /// from the given input stream. |
57 | /// |
58 | /// Please refer to the zlib documentation of deflateInit2() for a description |
59 | /// of the windowBits parameter. |
60 | |
61 | DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level); |
62 | /// Creates a DeflatingStreamBuf for compressing data passed |
63 | /// through and forwarding it to the given output stream. |
64 | |
65 | DeflatingStreamBuf(std::ostream& ostr, int windowBits, int level); |
66 | /// Creates a DeflatingStreamBuf for compressing data passed |
67 | /// through and forwarding it to the given output stream. |
68 | /// |
69 | /// Please refer to the zlib documentation of deflateInit2() for a description |
70 | /// of the windowBits parameter. |
71 | |
72 | ~DeflatingStreamBuf(); |
73 | /// Destroys the DeflatingStreamBuf. |
74 | |
75 | int close(); |
76 | /// Finishes up the stream. |
77 | /// |
78 | /// Must be called when deflating to an output stream. |
79 | |
80 | protected: |
81 | int readFromDevice(char* buffer, std::streamsize length); |
82 | int writeToDevice(const char* buffer, std::streamsize length); |
83 | virtual int sync(); |
84 | |
85 | private: |
86 | enum |
87 | { |
88 | STREAM_BUFFER_SIZE = 1024, |
89 | DEFLATE_BUFFER_SIZE = 32768 |
90 | }; |
91 | |
92 | std::istream* _pIstr; |
93 | std::ostream* _pOstr; |
94 | char* _buffer; |
95 | z_stream _zstr; |
96 | bool _eof; |
97 | }; |
98 | |
99 | |
100 | class Foundation_API DeflatingIOS: public virtual std::ios |
101 | /// The base class for DeflatingOutputStream and DeflatingInputStream. |
102 | /// |
103 | /// This class is needed to ensure the correct initialization |
104 | /// order of the stream buffer and base classes. |
105 | { |
106 | public: |
107 | DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION); |
108 | /// Creates a DeflatingIOS for compressing data passed |
109 | /// through and forwarding it to the given output stream. |
110 | |
111 | DeflatingIOS(std::ostream& ostr, int windowBits, int level); |
112 | /// Creates a DeflatingIOS for compressing data passed |
113 | /// through and forwarding it to the given output stream. |
114 | /// |
115 | /// Please refer to the zlib documentation of deflateInit2() for a description |
116 | /// of the windowBits parameter. |
117 | |
118 | DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION); |
119 | /// Creates a DeflatingIOS for compressing data read |
120 | /// from the given input stream. |
121 | |
122 | DeflatingIOS(std::istream& istr, int windowBits, int level); |
123 | /// Creates a DeflatingIOS for compressing data read |
124 | /// from the given input stream. |
125 | /// |
126 | /// Please refer to the zlib documentation of deflateInit2() for a description |
127 | /// of the windowBits parameter. |
128 | |
129 | ~DeflatingIOS(); |
130 | /// Destroys the DeflatingIOS. |
131 | |
132 | DeflatingStreamBuf* rdbuf(); |
133 | /// Returns a pointer to the underlying stream buffer. |
134 | |
135 | protected: |
136 | DeflatingStreamBuf _buf; |
137 | }; |
138 | |
139 | |
140 | class Foundation_API DeflatingOutputStream: public std::ostream, public DeflatingIOS |
141 | /// This stream compresses all data passing through it |
142 | /// using zlib's deflate algorithm. |
143 | /// After all data has been written to the stream, close() |
144 | /// must be called to ensure completion of compression. |
145 | /// Example: |
146 | /// std::ofstream ostr("data.gz", std::ios::binary); |
147 | /// DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP); |
148 | /// deflater << "Hello, world!" << std::endl; |
149 | /// deflater.close(); |
150 | /// ostr.close(); |
151 | { |
152 | public: |
153 | DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION); |
154 | /// Creates a DeflatingOutputStream for compressing data passed |
155 | /// through and forwarding it to the given output stream. |
156 | |
157 | DeflatingOutputStream(std::ostream& ostr, int windowBits, int level); |
158 | /// Creates a DeflatingOutputStream for compressing data passed |
159 | /// through and forwarding it to the given output stream. |
160 | /// |
161 | /// Please refer to the zlib documentation of deflateInit2() for a description |
162 | /// of the windowBits parameter. |
163 | |
164 | ~DeflatingOutputStream(); |
165 | /// Destroys the DeflatingOutputStream. |
166 | |
167 | int close(); |
168 | /// Finishes up the stream. |
169 | /// |
170 | /// Must be called when deflating to an output stream. |
171 | |
172 | protected: |
173 | virtual int sync(); |
174 | }; |
175 | |
176 | |
177 | class Foundation_API DeflatingInputStream: public std::istream, public DeflatingIOS |
178 | /// This stream compresses all data passing through it |
179 | /// using zlib's deflate algorithm. |
180 | { |
181 | public: |
182 | DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION); |
183 | /// Creates a DeflatingIOS for compressing data read |
184 | /// from the given input stream. |
185 | |
186 | DeflatingInputStream(std::istream& istr, int windowBits, int level); |
187 | /// Creates a DeflatingIOS for compressing data read |
188 | /// from the given input stream. |
189 | /// |
190 | /// Please refer to the zlib documentation of deflateInit2() for a description |
191 | /// of the windowBits parameter. |
192 | |
193 | ~DeflatingInputStream(); |
194 | /// Destroys the DeflatingInputStream. |
195 | }; |
196 | |
197 | |
198 | } // namespace Poco |
199 | |
200 | |
201 | #endif // Foundation_DeflatingStream_INCLUDED |
202 | |