]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "zstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_ZLIB && wxUSE_STREAMS
29 #include "wx/zstream.h"
33 #include "../zlib/zlib.h"
35 #define ZSTREAM_BUFFER_SIZE 1024
37 //////////////////////
39 //////////////////////
41 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
42 : wxFilterInputStream(stream
)
46 // I need a private stream buffer.
47 m_inflate
= new z_stream_s
;
49 m_inflate
->zalloc
= (alloc_func
)0;
50 m_inflate
->zfree
= (free_func
)0;
51 m_inflate
->opaque
= (voidpf
)0;
53 err
= inflateInit(m_inflate
);
55 inflateEnd(m_inflate
);
60 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
61 m_z_size
= ZSTREAM_BUFFER_SIZE
;
63 m_inflate
->avail_in
= 0;
64 m_inflate
->next_in
= NULL
;
67 wxZlibInputStream::~wxZlibInputStream()
69 inflateEnd(m_inflate
);
73 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
77 m_inflate
->next_out
= (unsigned char *)buffer
;
78 m_inflate
->avail_out
= size
;
80 while (m_inflate
->avail_out
> 0) {
81 if (m_inflate
->avail_in
== 0) {
83 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
84 m_inflate
->next_in
= m_z_buffer
;
85 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
87 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
88 return (size
- m_inflate
->avail_in
);
90 err
= inflate(m_inflate
, Z_FINISH
);
91 if (err
== Z_STREAM_END
)
92 return (size
- m_inflate
->avail_in
);
95 return size
-m_inflate
->avail_in
;
98 //////////////////////
100 //////////////////////
102 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
103 : wxFilterOutputStream(stream
)
107 m_deflate
= new z_stream_s
;
109 m_deflate
->zalloc
= (alloc_func
)0;
110 m_deflate
->zfree
= (free_func
)0;
111 m_deflate
->opaque
= (voidpf
)0;
113 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
115 deflateEnd(m_deflate
);
119 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
120 m_z_size
= ZSTREAM_BUFFER_SIZE
;
122 m_deflate
->avail_in
= 0;
123 m_deflate
->next_out
= m_z_buffer
;
124 m_deflate
->avail_out
= m_z_size
;
127 wxZlibOutputStream::~wxZlibOutputStream()
133 err
= deflate(m_deflate
, Z_FINISH
);
134 if (err
!= Z_STREAM_END
)
136 wxLogDebug( _T("wxZlibOutputStream: an error occured while closing the stream.\n") );
140 deflateEnd(m_deflate
);
145 void wxZlibOutputStream::Sync()
149 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
150 m_deflate
->next_out
= m_z_buffer
;
151 m_deflate
->avail_out
= m_z_size
;
153 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::OnSysWrite(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
->LastError() != wxStream_NOERROR
)
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
);
189 // wxUSE_ZLIB && wxUSE_STREAMS