]>
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"
23 #if wxUSE_ZLIB && wxUSE_STREAMS
25 #include "wx/zstream.h"
30 // When using configure, the path must be "zlib.h" I don't know
31 // what other ports (wxMac, wxMotif without configure) need here.
34 #include "..\zlib\zlib.h"
39 #define ZSTREAM_BUFFER_SIZE 1024
41 //////////////////////
43 //////////////////////
45 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
46 : wxFilterInputStream(stream
)
50 // I need a private stream buffer.
51 m_inflate
= new z_stream_s
;
53 m_inflate
->zalloc
= (alloc_func
)0;
54 m_inflate
->zfree
= (free_func
)0;
55 m_inflate
->opaque
= (voidpf
)0;
57 err
= inflateInit(m_inflate
);
59 inflateEnd(m_inflate
);
64 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
65 m_z_size
= ZSTREAM_BUFFER_SIZE
;
67 m_inflate
->avail_in
= 0;
68 m_inflate
->next_in
= NULL
;
71 wxZlibInputStream::~wxZlibInputStream()
73 inflateEnd(m_inflate
);
77 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
81 m_inflate
->next_out
= (unsigned char *)buffer
;
82 m_inflate
->avail_out
= size
;
84 while (m_inflate
->avail_out
> 0) {
85 if (m_inflate
->avail_in
== 0) {
87 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
88 m_inflate
->next_in
= m_z_buffer
;
89 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
91 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
92 return (size
- m_inflate
->avail_in
);
94 err
= inflate(m_inflate
, Z_FINISH
);
95 if (err
== Z_STREAM_END
)
96 return (size
- m_inflate
->avail_in
);
99 return size
-m_inflate
->avail_in
;
102 //////////////////////
103 // wxZlibOutputStream
104 //////////////////////
106 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
107 : wxFilterOutputStream(stream
)
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