]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "zstream.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17 #include <wx/stream.h>
18 #include <wx/zstream.h>
21 #include "../zlib/zlib.h" // don't change this, Robert
27 #define ZSTREAM_BUFFER_SIZE 1024
29 //////////////////////
31 //////////////////////
33 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
34 : wxFilterInputStream(stream
)
38 // I need a private stream buffer.
39 m_i_streambuf
= new wxStreamBuffer(*this);
40 m_i_destroybuf
= TRUE
;
42 m_inflate
.zalloc
= (alloc_func
)0;
43 m_inflate
.zfree
= (free_func
)0;
44 m_inflate
.opaque
= (voidpf
)0;
46 err
= inflateInit(&m_inflate
);
48 inflateEnd(&m_inflate
);
52 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
53 m_z_size
= ZSTREAM_BUFFER_SIZE
;
55 m_inflate
.avail_in
= 0;
56 m_inflate
.next_in
= NULL
;
59 wxZlibInputStream::~wxZlibInputStream()
61 inflateEnd(&m_inflate
);
64 size_t wxZlibInputStream::DoRead(void *buffer
, size_t size
)
68 m_inflate
.next_out
= (unsigned char *)buffer
;
69 m_inflate
.avail_out
= size
;
71 while (m_inflate
.avail_out
> 0) {
72 if (m_inflate
.avail_in
== 0) {
74 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
75 m_inflate
.next_in
= m_z_buffer
;
76 m_inflate
.avail_in
= m_parent_i_stream
->LastRead();
78 if (m_parent_i_stream
->Eof())
79 return (size
- m_inflate
.avail_in
);
81 err
= inflate(&m_inflate
, Z_FINISH
);
82 if (err
== Z_STREAM_END
)
83 return (size
- m_inflate
.avail_in
);
86 return size
-m_inflate
.avail_in
;
89 bool wxZlibInputStream::Eof() const
92 return m_parent_i_stream
->Eof();
96 //////////////////////
98 //////////////////////
100 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
101 : wxFilterOutputStream(stream
)
105 m_o_streambuf
= new wxStreamBuffer(*this);
106 m_o_destroybuf
= TRUE
;
108 m_deflate
.zalloc
= (alloc_func
)0;
109 m_deflate
.zfree
= (free_func
)0;
110 m_deflate
.opaque
= (voidpf
)0;
112 err
= deflateInit(&m_deflate
, Z_DEFAULT_COMPRESSION
);
114 deflateEnd(&m_deflate
);
118 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
119 m_z_size
= ZSTREAM_BUFFER_SIZE
;
121 m_deflate
.avail_in
= 0;
122 m_deflate
.next_out
= m_z_buffer
;
123 m_deflate
.avail_out
= m_z_size
;
126 wxZlibOutputStream::~wxZlibOutputStream()
132 err
= deflate(&m_deflate
, Z_FINISH
);
133 if (err
!= Z_STREAM_END
) {
134 wxDebugMsg(_("wxZlibOutputStream: an error occured while we was closing "
139 deflateEnd(&m_deflate
);
144 void wxZlibOutputStream::Sync()
148 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
149 m_deflate
.next_out
= m_z_buffer
;
150 m_deflate
.avail_out
= m_z_size
;
152 err
= deflate(&m_deflate
, Z_FULL_FLUSH
);
158 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
159 m_deflate
.next_out
= m_z_buffer
;
160 m_deflate
.avail_out
= m_z_size
;
163 size_t wxZlibOutputStream::DoWrite(const void *buffer
, size_t size
)
167 m_deflate
.next_in
= (unsigned char *)buffer
;
168 m_deflate
.avail_in
= size
;
170 while (m_deflate
.avail_in
> 0) {
172 if (m_deflate
.avail_out
== 0) {
173 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
174 if (m_parent_o_stream
->Bad())
175 return (size
- m_deflate
.avail_in
);
177 m_deflate
.next_out
= m_z_buffer
;
178 m_deflate
.avail_out
= m_z_size
;
181 err
= deflate(&m_deflate
, Z_NO_FLUSH
);
183 return (size
- m_deflate
.avail_in
);
188 bool wxZlibOutputStream::Bad() const
191 return m_parent_o_stream
->Bad();