]>
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
;
41 m_inflate
= new z_stream_s
;
43 m_inflate
->zalloc
= (alloc_func
)0;
44 m_inflate
->zfree
= (free_func
)0;
45 m_inflate
->opaque
= (voidpf
)0;
47 err
= inflateInit(m_inflate
);
49 inflateEnd(m_inflate
);
54 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
55 m_z_size
= ZSTREAM_BUFFER_SIZE
;
57 m_inflate
->avail_in
= 0;
58 m_inflate
->next_in
= NULL
;
61 wxZlibInputStream::~wxZlibInputStream()
63 inflateEnd(m_inflate
);
67 size_t wxZlibInputStream::DoRead(void *buffer
, size_t size
)
71 m_inflate
->next_out
= (unsigned char *)buffer
;
72 m_inflate
->avail_out
= size
;
74 while (m_inflate
->avail_out
> 0) {
75 if (m_inflate
->avail_in
== 0) {
77 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
78 m_inflate
->next_in
= m_z_buffer
;
79 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
81 if (m_parent_i_stream
->Eof())
82 return (size
- m_inflate
->avail_in
);
84 err
= inflate(m_inflate
, Z_FINISH
);
85 if (err
== Z_STREAM_END
)
86 return (size
- m_inflate
->avail_in
);
89 return size
-m_inflate
->avail_in
;
92 bool wxZlibInputStream::Eof() const
95 return m_parent_i_stream
->Eof();
99 //////////////////////
100 // wxZlibOutputStream
101 //////////////////////
103 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
104 : wxFilterOutputStream(stream
)
108 m_o_streambuf
= new wxStreamBuffer(*this);
109 m_o_destroybuf
= TRUE
;
110 m_deflate
= new z_stream_s
;
112 m_deflate
->zalloc
= (alloc_func
)0;
113 m_deflate
->zfree
= (free_func
)0;
114 m_deflate
->opaque
= (voidpf
)0;
116 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
118 deflateEnd(m_deflate
);
122 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
123 m_z_size
= ZSTREAM_BUFFER_SIZE
;
125 m_deflate
->avail_in
= 0;
126 m_deflate
->next_out
= m_z_buffer
;
127 m_deflate
->avail_out
= m_z_size
;
130 wxZlibOutputStream::~wxZlibOutputStream()
136 err
= deflate(m_deflate
, Z_FINISH
);
137 if (err
!= Z_STREAM_END
) {
138 wxDebugMsg(_("wxZlibOutputStream: an error occured while we was closing "
143 deflateEnd(m_deflate
);
148 void wxZlibOutputStream::Sync()
152 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
153 m_deflate
->next_out
= m_z_buffer
;
154 m_deflate
->avail_out
= m_z_size
;
156 err
= deflate(m_deflate
, Z_FULL_FLUSH
);
162 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
163 m_deflate
->next_out
= m_z_buffer
;
164 m_deflate
->avail_out
= m_z_size
;
167 size_t wxZlibOutputStream::DoWrite(const void *buffer
, size_t size
)
171 m_deflate
->next_in
= (unsigned char *)buffer
;
172 m_deflate
->avail_in
= size
;
174 while (m_deflate
->avail_in
> 0) {
176 if (m_deflate
->avail_out
== 0) {
177 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
178 if (m_parent_o_stream
->Bad())
179 return (size
- m_deflate
->avail_in
);
181 m_deflate
->next_out
= m_z_buffer
;
182 m_deflate
->avail_out
= m_z_size
;
185 err
= deflate(m_deflate
, Z_NO_FLUSH
);
187 return (size
- m_deflate
->avail_in
);
192 bool wxZlibOutputStream::Bad() const
195 return m_parent_o_stream
->Bad();