]>
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"
18 #include "wx/zstream.h"
25 #include "../zlib/zlib.h" // don't change this, Robert
31 #define ZSTREAM_BUFFER_SIZE 1024
33 //////////////////////
35 //////////////////////
37 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
38 : wxFilterInputStream(stream
)
42 // I need a private stream buffer.
43 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
44 m_i_destroybuf
= TRUE
;
45 m_inflate
= new z_stream_s
;
47 m_inflate
->zalloc
= (alloc_func
)0;
48 m_inflate
->zfree
= (free_func
)0;
49 m_inflate
->opaque
= (voidpf
)0;
51 err
= inflateInit(m_inflate
);
53 inflateEnd(m_inflate
);
58 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
59 m_z_size
= ZSTREAM_BUFFER_SIZE
;
61 m_inflate
->avail_in
= 0;
62 m_inflate
->next_in
= NULL
;
65 wxZlibInputStream::~wxZlibInputStream()
67 inflateEnd(m_inflate
);
71 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
75 m_inflate
->next_out
= (unsigned char *)buffer
;
76 m_inflate
->avail_out
= size
;
78 while (m_inflate
->avail_out
> 0) {
79 if (m_inflate
->avail_in
== 0) {
81 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
82 m_inflate
->next_in
= m_z_buffer
;
83 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
85 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
86 return (size
- m_inflate
->avail_in
);
88 err
= inflate(m_inflate
, Z_FINISH
);
89 if (err
== Z_STREAM_END
)
90 return (size
- m_inflate
->avail_in
);
93 return size
-m_inflate
->avail_in
;
96 //////////////////////
98 //////////////////////
100 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
101 : wxFilterOutputStream(stream
)
105 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
106 m_o_destroybuf
= TRUE
;
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
);