]>
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>
22 #include "../zlib/zlib.h" // don't change this, Robert
28 #define ZSTREAM_BUFFER_SIZE 1024
30 //////////////////////
32 //////////////////////
34 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
35 : wxFilterInputStream(stream
)
39 // I need a private stream buffer.
40 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
41 m_i_destroybuf
= TRUE
;
42 m_inflate
= new z_stream_s
;
44 m_inflate
->zalloc
= (alloc_func
)0;
45 m_inflate
->zfree
= (free_func
)0;
46 m_inflate
->opaque
= (voidpf
)0;
48 err
= inflateInit(m_inflate
);
50 inflateEnd(m_inflate
);
55 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
56 m_z_size
= ZSTREAM_BUFFER_SIZE
;
58 m_inflate
->avail_in
= 0;
59 m_inflate
->next_in
= NULL
;
62 wxZlibInputStream::~wxZlibInputStream()
64 inflateEnd(m_inflate
);
68 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
72 m_inflate
->next_out
= (unsigned char *)buffer
;
73 m_inflate
->avail_out
= size
;
75 while (m_inflate
->avail_out
> 0) {
76 if (m_inflate
->avail_in
== 0) {
78 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
79 m_inflate
->next_in
= m_z_buffer
;
80 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
82 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
83 return (size
- m_inflate
->avail_in
);
85 err
= inflate(m_inflate
, Z_FINISH
);
86 if (err
== Z_STREAM_END
)
87 return (size
- m_inflate
->avail_in
);
90 return size
-m_inflate
->avail_in
;
93 //////////////////////
95 //////////////////////
97 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
98 : wxFilterOutputStream(stream
)
102 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
103 m_o_destroybuf
= TRUE
;
104 m_deflate
= new z_stream_s
;
106 m_deflate
->zalloc
= (alloc_func
)0;
107 m_deflate
->zfree
= (free_func
)0;
108 m_deflate
->opaque
= (voidpf
)0;
110 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
112 deflateEnd(m_deflate
);
116 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
117 m_z_size
= ZSTREAM_BUFFER_SIZE
;
119 m_deflate
->avail_in
= 0;
120 m_deflate
->next_out
= m_z_buffer
;
121 m_deflate
->avail_out
= m_z_size
;
124 wxZlibOutputStream::~wxZlibOutputStream()
130 err
= deflate(m_deflate
, Z_FINISH
);
131 if (err
!= Z_STREAM_END
)
133 wxLogDebug( "wxZlibOutputStream: an error occured while closing the stream.\n" );
137 deflateEnd(m_deflate
);
142 void wxZlibOutputStream::Sync()
146 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
147 m_deflate
->next_out
= m_z_buffer
;
148 m_deflate
->avail_out
= m_z_size
;
150 err
= deflate(m_deflate
, Z_FULL_FLUSH
);
155 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
156 m_deflate
->next_out
= m_z_buffer
;
157 m_deflate
->avail_out
= m_z_size
;
160 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
164 m_deflate
->next_in
= (unsigned char *)buffer
;
165 m_deflate
->avail_in
= size
;
167 while (m_deflate
->avail_in
> 0) {
169 if (m_deflate
->avail_out
== 0) {
170 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
171 if (m_parent_o_stream
->LastError() != wxStream_NOERROR
)
172 return (size
- m_deflate
->avail_in
);
174 m_deflate
->next_out
= m_z_buffer
;
175 m_deflate
->avail_out
= m_z_size
;
178 err
= deflate(m_deflate
, Z_NO_FLUSH
);
180 return (size
- m_deflate
->avail_in
);