1 /**
2 * Zlib-enabled input and output streams
3 *
4 * This is a thin wrapper of libz calls, in order
5 * to provide a simple interface to our developers
6 * for gzip input and output.
7 *
8 * Authors:
9 * Bob Jamison
10 *
11 * Copyright (C) 2006-2007 Bob Jamison
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
30 #include "gzipstream.h"
32 #include "dom/util/ziptool.h"
35 namespace org
36 {
37 namespace w3c
38 {
39 namespace dom
40 {
41 namespace io
42 {
45 //#########################################################################
46 //# G Z I P I N P U T S T R E A M
47 //#########################################################################
49 /**
50 *
51 */
52 GzipInputStream::GzipInputStream(InputStream &sourceStream)
53 : BasicInputStream(sourceStream)
54 {
55 loaded = false;
56 bufPos = 0;
57 }
59 /**
60 *
61 */
62 GzipInputStream::~GzipInputStream()
63 {
64 close();
65 }
67 /**
68 * Returns the number of bytes that can be read (or skipped over) from
69 * this input stream without blocking by the next caller of a method for
70 * this input stream.
71 */
72 int GzipInputStream::available()
73 {
74 if (closed)
75 return 0;
76 if (!loaded)
77 if (!load())
78 return 0;
79 return (int) buffer.size();
80 }
83 /**
84 * Closes this input stream and releases any system resources
85 * associated with the stream.
86 */
87 void GzipInputStream::close()
88 {
89 if (closed)
90 return;
92 closed = true;
93 }
95 /**
96 * Reads the next byte of data from the input stream. -1 if EOF
97 */
98 int GzipInputStream::get()
99 {
100 if (closed)
101 return -1;
103 if (!loaded)
104 if (!load())
105 return -1;
107 if (bufPos >= buffer.size())
108 return -1;
109 int ch = (int) buffer[bufPos++];
111 return ch;
112 }
114 /**
115 * Processes input. Fills read buffer.
116 */
117 bool GzipInputStream::load()
118 {
119 if (closed)
120 return false;
122 if (loaded)
123 return true;
125 std::vector<unsigned char> compBuf;
126 while (true)
127 {
128 int ch = source.get();
129 if (ch < 0)
130 break;
131 compBuf.push_back(ch);
132 }
133 GzipFile gz;
134 if (!gz.readBuffer(compBuf))
135 {
136 return -1;
137 }
138 buffer = gz.getData();
139 bufPos = 0;
140 loaded = true;
141 return true;
142 }
149 //#########################################################################
150 //# G Z I P O U T P U T S T R E A M
151 //#########################################################################
153 /**
154 *
155 */
156 GzipOutputStream::GzipOutputStream(OutputStream &destinationStream)
157 : BasicOutputStream(destinationStream)
158 {
160 closed = false;
161 }
163 /**
164 *
165 */
166 GzipOutputStream::~GzipOutputStream()
167 {
168 close();
169 }
171 /**
172 * Closes this output stream and releases any system resources
173 * associated with this stream.
174 */
175 void GzipOutputStream::close()
176 {
177 if (closed)
178 return;
180 flush();
182 closed = true;
183 }
185 /**
186 * Flushes this output stream and forces any buffered output
187 * bytes to be written out.
188 */
189 void GzipOutputStream::flush()
190 {
191 if (closed || buffer.size()<1)
192 return;
194 std::vector<unsigned char> compBuf;
195 GzipFile gz;
197 gz.writeBuffer(buffer);
199 std::vector<unsigned char>::iterator iter;
200 for (iter=compBuf.begin() ; iter!=compBuf.end() ; iter++)
201 {
202 int ch = (int) *iter;
203 destination.put(ch);
204 }
206 buffer.clear();
208 //printf("done\n");
210 }
214 /**
215 * Writes the specified byte to this output stream.
216 */
217 int GzipOutputStream::put(XMLCh ch)
218 {
219 if (closed)
220 {
221 //probably throw an exception here
222 return -1;
223 }
225 //Add char to buffer
226 buffer.push_back(ch);
227 return 1;
228 }
232 } // namespace io
233 } // namespace dom
234 } // namespace w3c
235 } // namespace org
240 //#########################################################################
241 //# E N D O F F I L E
242 //#########################################################################