]>
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>
20 #include "../zlib/zlib.h" // don't change this, Robert
26 #define ZSTREAM_BUFFER_SIZE 1024
28 //////////////////////
30 //////////////////////
32 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
33 : wxFilterInputStream(stream
)
37 // I need a private stream buffer.
38 m_i_streambuf
= new wxStreamBuffer(*this);
39 m_i_destroybuf
= TRUE
;
41 m_inflate
.zalloc
= (alloc_func
)0;
42 m_inflate
.zfree
= (free_func
)0;
43 m_inflate
.opaque
= (voidpf
)0;
45 err
= inflateInit(&m_inflate
);
47 inflateEnd(&m_inflate
);
51 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
52 m_z_size
= ZSTREAM_BUFFER_SIZE
;
54 m_inflate
.avail_in
= 0;
55 m_inflate
.next_in
= NULL
;
58 wxZlibInputStream::~wxZlibInputStream()
60 inflateEnd(&m_inflate
);
63 size_t wxZlibInputStream::DoRead(void *buffer
, size_t size
)
67 m_inflate
.next_out
= (unsigned char *)buffer
;
68 m_inflate
.avail_out
= size
;
70 while (m_inflate
.avail_out
> 0) {
71 if (m_inflate
.avail_in
== 0) {
73 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
74 m_inflate
.next_in
= m_z_buffer
;
75 m_inflate
.avail_in
= m_parent_i_stream
->LastRead();
77 if (m_parent_i_stream
->Eof())
78 return (size
- m_inflate
.avail_in
);
80 err
= inflate(&m_inflate
, Z_FINISH
);
81 if (err
== Z_STREAM_END
)
82 return (size
- m_inflate
.avail_in
);
85 return size
-m_inflate
.avail_in
;
88 bool wxZlibInputStream::Eof() const
91 return m_parent_i_stream
->Eof();
95 //////////////////////
97 //////////////////////
99 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
100 : wxFilterOutputStream(stream
)
104 m_o_streambuf
= new wxStreamBuffer(*this);
105 m_o_destroybuf
= TRUE
;
107 m_deflate
.zalloc
= (alloc_func
)0;
108 m_deflate
.zfree
= (free_func
)0;
109 m_deflate
.opaque
= (voidpf
)0;
111 err
= deflateInit(&m_deflate
, Z_DEFAULT_COMPRESSION
);
113 deflateEnd(&m_deflate
);
117 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
118 m_z_size
= ZSTREAM_BUFFER_SIZE
;
120 m_deflate
.avail_in
= 0;
121 m_deflate
.next_out
= m_z_buffer
;
122 m_deflate
.avail_out
= m_z_size
;
125 wxZlibOutputStream::~wxZlibOutputStream()
131 err
= deflate(&m_deflate
, Z_FINISH
);
132 if (err
!= Z_STREAM_END
) {
133 wxDebugMsg("wxZlibOutputStream: an error occured while we was closing "
138 deflateEnd(&m_deflate
);
143 void wxZlibOutputStream::Sync()
147 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
148 m_deflate
.next_out
= m_z_buffer
;
149 m_deflate
.avail_out
= m_z_size
;
151 err
= deflate(&m_deflate
, Z_FULL_FLUSH
);
157 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
158 m_deflate
.next_out
= m_z_buffer
;
159 m_deflate
.avail_out
= m_z_size
;
162 size_t wxZlibOutputStream::DoWrite(const void *buffer
, size_t size
)
166 m_deflate
.next_in
= (unsigned char *)buffer
;
167 m_deflate
.avail_in
= size
;
169 while (m_deflate
.avail_in
> 0) {
171 if (m_deflate
.avail_out
== 0) {
172 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
173 if (m_parent_o_stream
->Bad())
174 return (size
- m_deflate
.avail_in
);
176 m_deflate
.next_out
= m_z_buffer
;
177 m_deflate
.avail_out
= m_z_size
;
180 err
= deflate(&m_deflate
, Z_NO_FLUSH
);
182 return (size
- m_deflate
.avail_in
);
187 bool wxZlibOutputStream::Bad() const
190 return m_parent_o_stream
->Bad();