]>
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"
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_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
48 m_i_destroybuf
= TRUE
;
49 m_inflate
= new z_stream_s
;
51 m_inflate
->zalloc
= (alloc_func
)0;
52 m_inflate
->zfree
= (free_func
)0;
53 m_inflate
->opaque
= (voidpf
)0;
55 err
= inflateInit(m_inflate
);
57 inflateEnd(m_inflate
);
62 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
63 m_z_size
= ZSTREAM_BUFFER_SIZE
;
65 m_inflate
->avail_in
= 0;
66 m_inflate
->next_in
= NULL
;
69 wxZlibInputStream::~wxZlibInputStream()
71 inflateEnd(m_inflate
);
75 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
79 m_inflate
->next_out
= (unsigned char *)buffer
;
80 m_inflate
->avail_out
= size
;
82 while (m_inflate
->avail_out
> 0) {
83 if (m_inflate
->avail_in
== 0) {
85 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
86 m_inflate
->next_in
= m_z_buffer
;
87 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
89 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
90 return (size
- m_inflate
->avail_in
);
92 err
= inflate(m_inflate
, Z_FINISH
);
93 if (err
== Z_STREAM_END
)
94 return (size
- m_inflate
->avail_in
);
97 return size
-m_inflate
->avail_in
;
100 //////////////////////
101 // wxZlibOutputStream
102 //////////////////////
104 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
105 : wxFilterOutputStream(stream
)
109 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
110 m_o_destroybuf
= TRUE
;
111 m_deflate
= new z_stream_s
;
113 m_deflate
->zalloc
= (alloc_func
)0;
114 m_deflate
->zfree
= (free_func
)0;
115 m_deflate
->opaque
= (voidpf
)0;
117 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
119 deflateEnd(m_deflate
);
123 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
124 m_z_size
= ZSTREAM_BUFFER_SIZE
;
126 m_deflate
->avail_in
= 0;
127 m_deflate
->next_out
= m_z_buffer
;
128 m_deflate
->avail_out
= m_z_size
;
131 wxZlibOutputStream::~wxZlibOutputStream()
137 err
= deflate(m_deflate
, Z_FINISH
);
138 if (err
!= Z_STREAM_END
)
140 wxLogDebug( _T("wxZlibOutputStream: an error occured while closing the stream.\n") );
144 deflateEnd(m_deflate
);
149 void wxZlibOutputStream::Sync()
153 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
154 m_deflate
->next_out
= m_z_buffer
;
155 m_deflate
->avail_out
= m_z_size
;
157 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::OnSysWrite(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
->LastError() != wxStream_NOERROR
)
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
);
193 // wxUSE_ZLIB && wxUSE_STREAMS