1 | // |
---|
2 | // InflatingStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/InflatingStream.h#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: ZLibStream |
---|
9 | // |
---|
10 | // Definition of the InflatingInputStream and InflatingOutputStream classes. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_InflatingStream_INCLUDED |
---|
40 | #define Foundation_InflatingStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/BufferedStreamBuf.h" |
---|
45 | #include <istream> |
---|
46 | #include <ostream> |
---|
47 | #if defined(POCO_UNBUNDLED) |
---|
48 | #include <zlib.h> |
---|
49 | #else |
---|
50 | #include "Poco/zlib.h" |
---|
51 | #endif |
---|
52 | |
---|
53 | |
---|
54 | namespace Poco { |
---|
55 | |
---|
56 | |
---|
57 | class Foundation_API InflatingStreamBuf: public BufferedStreamBuf |
---|
58 | /// This is the streambuf class used by InflatingInputStream and InflatingOutputStream. |
---|
59 | /// The actual work is delegated to zlib 1.2.1 (see http://www.gzip.org). |
---|
60 | /// Both zlib (deflate) streams and gzip streams are supported. |
---|
61 | /// Output streams should always call close() to ensure |
---|
62 | /// proper completion of decompression. |
---|
63 | { |
---|
64 | public: |
---|
65 | enum StreamType |
---|
66 | { |
---|
67 | STREAM_ZLIB, |
---|
68 | STREAM_GZIP, |
---|
69 | STREAM_ZIP // ZIP is handled as STREAM_ZLIB, except that we do not check the ADLER32 value (must be checked by an outside class!) |
---|
70 | }; |
---|
71 | |
---|
72 | InflatingStreamBuf(std::istream& istr, StreamType type); |
---|
73 | InflatingStreamBuf(std::ostream& ostr, StreamType type); |
---|
74 | ~InflatingStreamBuf(); |
---|
75 | int close(); |
---|
76 | void reset(); |
---|
77 | |
---|
78 | protected: |
---|
79 | int readFromDevice(char* buffer, std::streamsize length); |
---|
80 | int writeToDevice(const char* buffer, std::streamsize length); |
---|
81 | |
---|
82 | private: |
---|
83 | enum |
---|
84 | { |
---|
85 | STREAM_BUFFER_SIZE = 1024, |
---|
86 | INFLATE_BUFFER_SIZE = 32768 |
---|
87 | }; |
---|
88 | |
---|
89 | std::istream* _pIstr; |
---|
90 | std::ostream* _pOstr; |
---|
91 | char* _buffer; |
---|
92 | z_stream _zstr; |
---|
93 | bool _eof; |
---|
94 | bool _check; |
---|
95 | }; |
---|
96 | |
---|
97 | |
---|
98 | class Foundation_API InflatingIOS: public virtual std::ios |
---|
99 | /// The base class for InflatingOutputStream and InflatingInputStream. |
---|
100 | /// |
---|
101 | /// This class is needed to ensure the correct initialization |
---|
102 | /// order of the stream buffer and base classes. |
---|
103 | { |
---|
104 | public: |
---|
105 | InflatingIOS(std::ostream& ostr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB); |
---|
106 | InflatingIOS(std::istream& istr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB); |
---|
107 | ~InflatingIOS(); |
---|
108 | InflatingStreamBuf* rdbuf(); |
---|
109 | |
---|
110 | protected: |
---|
111 | InflatingStreamBuf _buf; |
---|
112 | }; |
---|
113 | |
---|
114 | |
---|
115 | class Foundation_API InflatingOutputStream: public InflatingIOS, public std::ostream |
---|
116 | /// This stream decompresses all data passing through it |
---|
117 | /// using zlib's inflate algorithm. |
---|
118 | /// |
---|
119 | /// After all data has been written to the stream, close() |
---|
120 | /// must be called to ensure completion of decompression. |
---|
121 | { |
---|
122 | public: |
---|
123 | InflatingOutputStream(std::ostream& ostr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB); |
---|
124 | ~InflatingOutputStream(); |
---|
125 | int close(); |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | class Foundation_API InflatingInputStream: public InflatingIOS, public std::istream |
---|
130 | /// This stream decompresses all data passing through it |
---|
131 | /// using zlib's inflate algorithm. |
---|
132 | /// Example: |
---|
133 | /// std::ifstream istr("data.gz", std::ios::binary); |
---|
134 | /// InflatingInputStream inflater(istr, InflatingStreamBuf::STREAM_GZIP); |
---|
135 | /// std::string data; |
---|
136 | /// istr >> data; |
---|
137 | /// |
---|
138 | /// The underlying input stream can contain more than one gzip/deflate stream. |
---|
139 | /// After a gzip/deflate stream has been processed, reset() can be called |
---|
140 | /// to inflate the next stream. |
---|
141 | { |
---|
142 | public: |
---|
143 | InflatingInputStream(std::istream& istr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB); |
---|
144 | ~InflatingInputStream(); |
---|
145 | void reset(); |
---|
146 | }; |
---|
147 | |
---|
148 | |
---|
149 | } // namespace Poco |
---|
150 | |
---|
151 | |
---|
152 | #endif // Foundation_InflatingStream_INCLUDED |
---|