~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/include/Poco/DeflatingStream.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DeflatingStream.h
 
3
//
 
4
// $Id: //poco/1.2/Foundation/include/Poco/DeflatingStream.h#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Streams
 
8
// Module:  ZLibStream
 
9
//
 
10
// Definition of the DeflatingStream class.
 
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_DeflatingStream_INCLUDED
 
40
#define Foundation_DeflatingStream_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Foundation.h"
 
44
#include "Poco/BufferedStreamBuf.h"
 
45
#include <istream>
 
46
#include <ostream>
 
47
#include "Poco/zlib.h"
 
48
 
 
49
 
 
50
namespace Poco {
 
51
 
 
52
 
 
53
class Foundation_API DeflatingStreamBuf: public BufferedStreamBuf
 
54
        /// This is the streambuf class used by DeflatingInputStream and DeflatingOutputStream.
 
55
        /// The actual work is delegated to zlib 1.2.1 (see http://www.gzip.org).
 
56
        /// Both zlib (deflate) streams and gzip streams are supported.
 
57
        /// Output streams should always call close() to ensure
 
58
        /// proper completion of compression.
 
59
        /// A compression level (0 to 9) can be specified in the constructor.
 
60
{
 
61
public:
 
62
        enum StreamType
 
63
        {
 
64
                STREAM_ZLIB,
 
65
                STREAM_GZIP
 
66
        };
 
67
 
 
68
        DeflatingStreamBuf(std::istream& istr, StreamType type, int level);
 
69
        DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level);
 
70
        ~DeflatingStreamBuf();
 
71
        int close();
 
72
 
 
73
protected:
 
74
        int readFromDevice(char* buffer, std::streamsize length);
 
75
        int writeToDevice(const char* buffer, std::streamsize length);
 
76
 
 
77
private:
 
78
        enum 
 
79
        {
 
80
                STREAM_BUFFER_SIZE  = 1024,
 
81
                DEFLATE_BUFFER_SIZE = 32768
 
82
        };
 
83
 
 
84
        std::istream* _pIstr;
 
85
        std::ostream* _pOstr;
 
86
        char*    _buffer;
 
87
        z_stream _zstr;
 
88
        bool     _eof;
 
89
};
 
90
 
 
91
 
 
92
class Foundation_API DeflatingIOS: public virtual std::ios
 
93
        /// The base class for DeflatingOutputStream and DeflatingInputStream.
 
94
        ///
 
95
        /// This class is needed to ensure the correct initialization
 
96
        /// order of the stream buffer and base classes.
 
97
{
 
98
public:
 
99
        DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
 
100
        DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
 
101
        ~DeflatingIOS();
 
102
        DeflatingStreamBuf* rdbuf();
 
103
 
 
104
protected:
 
105
        DeflatingStreamBuf _buf;
 
106
};
 
107
 
 
108
 
 
109
class Foundation_API DeflatingOutputStream: public DeflatingIOS, public std::ostream
 
110
        /// This stream compresses all data passing through it
 
111
        /// using zlib's deflate algorithm.
 
112
        /// After all data has been written to the stream, close()
 
113
        /// must be called to ensure completion of compression.
 
114
        /// Example:
 
115
        ///     std::ofstream ostr("data.gz", std::ios::binary);
 
116
        ///     DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
 
117
        ///     deflater << "Hello, world!" << std::endl;
 
118
        ///     deflater.close();
 
119
        ///     ostr.close();
 
120
{
 
121
public:
 
122
        DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
 
123
        ~DeflatingOutputStream();
 
124
        int close();
 
125
};
 
126
 
 
127
 
 
128
class Foundation_API DeflatingInputStream: public DeflatingIOS, public std::istream
 
129
        /// This stream compresses all data passing through it
 
130
        /// using zlib's deflate algorithm.
 
131
{
 
132
public:
 
133
        DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
 
134
        ~DeflatingInputStream();
 
135
};
 
136
 
 
137
 
 
138
} // namespace Poco
 
139
 
 
140
 
 
141
#endif // Foundation_DeflatingStream_INCLUDED