ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QuaZIODevice Class Reference

A class to compress/decompress QIODevice. More...

#include <src/ckb/quazip/quaziodevice.h>

+ Inheritance diagram for QuaZIODevice:
+ Collaboration diagram for QuaZIODevice:

Public Member Functions

 QuaZIODevice (QIODevice *io, QObject *parent=NULL)
 Constructor. More...
 
 ~QuaZIODevice ()
 Destructor. More...
 
virtual bool flush ()
 Flushes data waiting to be written. More...
 
virtual bool open (QIODevice::OpenMode mode)
 Opens the device. More...
 
virtual void close ()
 Closes this device, but not the underlying one. More...
 
QIODevicegetIoDevice () const
 Returns the underlying device. More...
 
virtual bool isSequential () const
 Returns true. More...
 

Protected Member Functions

virtual qint64 readData (char *data, qint64 maxSize)
 Implementation of QIODevice::readData(). More...
 
virtual qint64 writeData (const char *data, qint64 maxSize)
 Implementation of QIODevice::writeData(). More...
 

Private Attributes

QuaZIODevicePrivate * d
 

Detailed Description

This class can be used to compress any data written to QIODevice or decompress it back. Compressing data sent over a QTcpSocket is a good example.

Definition at line 41 of file quaziodevice.h.

Constructor & Destructor Documentation

QuaZIODevice::QuaZIODevice ( QIODevice io,
QObject parent = NULL 
)
Parameters
ioThe QIODevice to read/write.
parentThe parent object, as per QObject logic.

Definition at line 123 of file quaziodevice.cpp.

123  :
124  QIODevice(parent),
125  d(new QuaZIODevicePrivate(io))
126 {
127  connect(io, SIGNAL(readyRead()), SIGNAL(readyRead()));
128 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
QuaZIODevice::~QuaZIODevice ( )

Definition at line 130 of file quaziodevice.cpp.

References close(), and d.

131 {
132  if (isOpen())
133  close();
134  delete d;
135 }
virtual void close()
Closes this device, but not the underlying one.
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96

+ Here is the call graph for this function:

Member Function Documentation

void QuaZIODevice::close ( )
virtual

The underlying QIODevice is not closed in case you want to write something else to it.

Definition at line 169 of file quaziodevice.cpp.

References d, and flush().

Referenced by ~QuaZIODevice().

170 {
171  if ((openMode() & QIODevice::ReadOnly) != 0) {
172  if (inflateEnd(&d->zins) != Z_OK) {
173  setErrorString(d->zins.msg);
174  }
175  }
176  if ((openMode() & QIODevice::WriteOnly) != 0) {
177  flush();
178  if (deflateEnd(&d->zouts) != Z_OK) {
179  setErrorString(d->zouts.msg);
180  }
181  }
182  QIODevice::close();
183 }
virtual bool flush()
Flushes data waiting to be written.
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool QuaZIODevice::flush ( )
virtual

Unfortunately, as QIODevice doesn't support flush() by itself, the only thing this method does is write the compressed data into the device using Z_SYNC_FLUSH mode. If you need the compressed data to actually be flushed from the buffer of the underlying QIODevice, you need to call its flush() method as well, providing it supports it (like QTcpSocket does). Example:

QuaZIODevice dev(&sock);
dev.open(QIODevice::Write);
dev.write(yourDataGoesHere);
dev.flush();
sock->flush(); // this actually sends data to network

This may change in the future versions of QuaZIP by implementing an ugly hack: trying to cast the QIODevice using qobject_cast to known flush()-supporting subclasses, and calling flush if the resulting pointer is not zero.

Definition at line 281 of file quaziodevice.cpp.

References d, and QUAZIO_OUTBUFSIZE.

Referenced by close().

282 {
283  QString error;
284  if (d->doFlush(error) < 0) {
285  setErrorString(error);
286  return false;
287  }
288  // can't flush buffer, some data is still waiting
289  if (d->outBufPos < d->outBufSize)
290  return true;
291  Bytef c = 0;
292  d->zouts.next_in = &c; // fake input buffer
293  d->zouts.avail_in = 0; // of zero size
294  do {
295  d->zouts.next_out = (Bytef *) d->outBuf;
296  d->zouts.avail_out = QUAZIO_OUTBUFSIZE;
297  switch (deflate(&d->zouts, Z_SYNC_FLUSH)) {
298  case Z_OK:
299  d->outBufSize = (char *) d->zouts.next_out - d->outBuf;
300  if (d->doFlush(error) < 0) {
301  setErrorString(error);
302  return false;
303  }
304  if (d->outBufPos < d->outBufSize)
305  return true;
306  break;
307  case Z_BUF_ERROR: // nothing to write?
308  return true;
309  default:
310  setErrorString(QString::fromLocal8Bit(d->zouts.msg));
311  return false;
312  }
313  } while (d->zouts.avail_out == 0);
314  return true;
315 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
#define QUAZIO_OUTBUFSIZE

+ Here is the caller graph for this function:

QIODevice * QuaZIODevice::getIoDevice ( ) const

Definition at line 137 of file quaziodevice.cpp.

References d.

138 {
139  return d->io;
140 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
bool QuaZIODevice::isSequential ( ) const
virtual

Definition at line 317 of file quaziodevice.cpp.

318 {
319  return true;
320 }
bool QuaZIODevice::open ( QIODevice::OpenMode  mode)
virtual
Parameters
modeNeither QIODevice::ReadWrite nor QIODevice::Append are not supported.

Definition at line 142 of file quaziodevice.cpp.

References d.

143 {
144  if ((mode & QIODevice::Append) != 0) {
145  setErrorString(trUtf8("QIODevice::Append is not supported for"
146  " QuaZIODevice"));
147  return false;
148  }
149  if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
150  setErrorString(trUtf8("QIODevice::ReadWrite is not supported for"
151  " QuaZIODevice"));
152  return false;
153  }
154  if ((mode & QIODevice::ReadOnly) != 0) {
155  if (inflateInit(&d->zins) != Z_OK) {
156  setErrorString(d->zins.msg);
157  return false;
158  }
159  }
160  if ((mode & QIODevice::WriteOnly) != 0) {
161  if (deflateInit(&d->zouts, Z_DEFAULT_COMPRESSION) != Z_OK) {
162  setErrorString(d->zouts.msg);
163  return false;
164  }
165  }
166  return QIODevice::open(mode);
167 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
qint64 QuaZIODevice::readData ( char *  data,
qint64  maxSize 
)
protectedvirtual

Definition at line 185 of file quaziodevice.cpp.

References d, and QUAZIO_INBUFSIZE.

186 {
187  int read = 0;
188  while (read < maxSize) {
189  if (d->inBufPos == d->inBufSize) {
190  d->inBufPos = 0;
191  d->inBufSize = d->io->read(d->inBuf, QUAZIO_INBUFSIZE);
192  if (d->inBufSize == -1) {
193  d->inBufSize = 0;
194  setErrorString(d->io->errorString());
195  return -1;
196  }
197  if (d->inBufSize == 0)
198  break;
199  }
200  while (read < maxSize && d->inBufPos < d->inBufSize) {
201  d->zins.next_in = (Bytef *) (d->inBuf + d->inBufPos);
202  d->zins.avail_in = d->inBufSize - d->inBufPos;
203  d->zins.next_out = (Bytef *) (data + read);
204  d->zins.avail_out = (uInt) (maxSize - read); // hope it's less than 2GB
205  int more = 0;
206  switch (inflate(&d->zins, Z_SYNC_FLUSH)) {
207  case Z_OK:
208  read = (char *) d->zins.next_out - data;
209  d->inBufPos = (char *) d->zins.next_in - d->inBuf;
210  break;
211  case Z_STREAM_END:
212  read = (char *) d->zins.next_out - data;
213  d->inBufPos = (char *) d->zins.next_in - d->inBuf;
214  return read;
215  case Z_BUF_ERROR: // this should never happen, but just in case
216  if (!d->zBufError) {
217  qWarning("Z_BUF_ERROR detected with %d/%d in/out, weird",
218  d->zins.avail_in, d->zins.avail_out);
219  d->zBufError = true;
220  }
221  memmove(d->inBuf, d->inBuf + d->inBufPos, d->inBufSize - d->inBufPos);
222  d->inBufSize -= d->inBufPos;
223  d->inBufPos = 0;
224  more = d->io->read(d->inBuf + d->inBufSize, QUAZIO_INBUFSIZE - d->inBufSize);
225  if (more == -1) {
226  setErrorString(d->io->errorString());
227  return -1;
228  }
229  if (more == 0)
230  return read;
231  d->inBufSize += more;
232  break;
233  default:
234  setErrorString(QString::fromLocal8Bit(d->zins.msg));
235  return -1;
236  }
237  }
238  }
239 #ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
240  indebug.write(data, read);
241 #endif
242  return read;
243 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
#define QUAZIO_INBUFSIZE
qint64 QuaZIODevice::writeData ( const char *  data,
qint64  maxSize 
)
protectedvirtual

Definition at line 245 of file quaziodevice.cpp.

References d, and QUAZIO_OUTBUFSIZE.

246 {
247  int written = 0;
248  QString error;
249  if (d->doFlush(error) == -1) {
250  setErrorString(error);
251  return -1;
252  }
253  while (written < maxSize) {
254  // there is some data waiting in the output buffer
255  if (d->outBufPos < d->outBufSize)
256  return written;
257  d->zouts.next_in = (Bytef *) (data + written);
258  d->zouts.avail_in = (uInt) (maxSize - written); // hope it's less than 2GB
259  d->zouts.next_out = (Bytef *) d->outBuf;
260  d->zouts.avail_out = QUAZIO_OUTBUFSIZE;
261  switch (deflate(&d->zouts, Z_NO_FLUSH)) {
262  case Z_OK:
263  written = (char *) d->zouts.next_in - data;
264  d->outBufSize = (char *) d->zouts.next_out - d->outBuf;
265  break;
266  default:
267  setErrorString(QString::fromLocal8Bit(d->zouts.msg));
268  return -1;
269  }
270  if (d->doFlush(error) == -1) {
271  setErrorString(error);
272  return -1;
273  }
274  }
275 #ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
276  debug.write(data, written);
277 #endif
278  return written;
279 }
QuaZIODevicePrivate * d
Definition: quaziodevice.h:96
#define QUAZIO_OUTBUFSIZE

Field Documentation

QuaZIODevicePrivate* QuaZIODevice::d
private

Definition at line 96 of file quaziodevice.h.

Referenced by close(), flush(), getIoDevice(), open(), readData(), writeData(), and ~QuaZIODevice().


The documentation for this class was generated from the following files: