]>
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, wxStreamBuffer::read
);
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::OnSysRead(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
->LastError() != wxStream_NOERROR
)
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 //////////////////////
94 //////////////////////
96 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
97 : wxFilterOutputStream(stream
)
101 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
102 m_o_destroybuf
= TRUE
;
103 m_deflate
= new z_stream_s
;
105 m_deflate
->zalloc
= (alloc_func
)0;
106 m_deflate
->zfree
= (free_func
)0;
107 m_deflate
->opaque
= (voidpf
)0;
109 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
111 deflateEnd(m_deflate
);
115 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
116 m_z_size
= ZSTREAM_BUFFER_SIZE
;
118 m_deflate
->avail_in
= 0;
119 m_deflate
->next_out
= m_z_buffer
;
120 m_deflate
->avail_out
= m_z_size
;
123 wxZlibOutputStream::~wxZlibOutputStream()
129 err
= deflate(m_deflate
, Z_FINISH
);
130 if (err
!= Z_STREAM_END
) {
131 wxDebugMsg(_("wxZlibOutputStream: an error occured while we was closing "
136 deflateEnd(m_deflate
);
141 void wxZlibOutputStream::Sync()
145 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
146 m_deflate
->next_out
= m_z_buffer
;
147 m_deflate
->avail_out
= m_z_size
;
149 err
= deflate(m_deflate
, Z_FULL_FLUSH
);
154 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
155 m_deflate
->next_out
= m_z_buffer
;
156 m_deflate
->avail_out
= m_z_size
;
159 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
163 m_deflate
->next_in
= (unsigned char *)buffer
;
164 m_deflate
->avail_in
= size
;
166 while (m_deflate
->avail_in
> 0) {
168 if (m_deflate
->avail_out
== 0) {
169 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
170 if (m_parent_o_stream
->LastError() != wxStream_NOERROR
)
171 return (size
- m_deflate
->avail_in
);
173 m_deflate
->next_out
= m_z_buffer
;
174 m_deflate
->avail_out
= m_z_size
;
177 err
= deflate(m_deflate
, Z_NO_FLUSH
);
179 return (size
- m_deflate
->avail_in
);