15
16
17
18
19
20
21
22
23
24
25
26
27
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
45
46
47
48
49
50
51
52
53
55
56
57
58
59
60
61
62
63
65
66
67
68
69
70
71
72
73
75
76
77
78
79
80
81
83
84
85
86
87
88
90
91
92
93
94
95
97QNonContiguousByteDevice::QNonContiguousByteDevice() : QObject((QObject*)
nullptr)
101QNonContiguousByteDevice::~QNonContiguousByteDevice()
106 : QNonContiguousByteDevice(), byteArray(std::move(ba)), view(byteArray)
128 if (maximumLength != -1)
129 len = qMin(maximumLength, size() - currentPosition);
131 len = size() - currentPosition;
133 return view.data() + currentPosition;
138 currentPosition += amount;
139 emit readProgress(currentPosition, size());
145 return currentPosition >= size();
161 return currentPosition;
165 : QNonContiguousByteDevice(), ringBuffer(std::move(rb))
180 const char *returnValue = ringBuffer->readPointerAtPosition(currentPosition, len);
182 if (maximumLength != -1)
183 len = qMin(len, maximumLength);
190 currentPosition += amount;
191 emit readProgress(currentPosition, size());
197 return currentPosition >= size();
202 return currentPosition;
213 return ringBuffer->size();
217 : QNonContiguousByteDevice(),
219 currentReadBuffer(
nullptr),
220 currentReadBufferSize(16 * 1024),
221 currentReadBufferAmount(0),
222 currentReadBufferPosition(0),
223 totalAdvancements(0),
225 initialPosition(d->pos())
227 connect(device, &QIODevice::readyRead,
this,
228 &QNonContiguousByteDevice::readyRead);
229 connect(device, &QIODevice::readChannelFinished,
this,
230 &QNonContiguousByteDevice::readyRead);
235 delete currentReadBuffer;
245 if (currentReadBuffer ==
nullptr)
246 currentReadBuffer =
new QByteArray(currentReadBufferSize,
'\0');
248 if (maximumLength == -1)
249 maximumLength = currentReadBufferSize;
251 if (currentReadBufferAmount - currentReadBufferPosition > 0) {
252 len = currentReadBufferAmount - currentReadBufferPosition;
253 return currentReadBuffer->data() + currentReadBufferPosition;
256 qint64 haveRead = device->read(currentReadBuffer->data(),
257 qMin(maximumLength, currentReadBufferSize));
259 if ((haveRead == -1) || (haveRead == 0 && device->atEnd() && !device->isSequential())) {
264 emit readProgress(totalAdvancements, totalAdvancements);
268 currentReadBufferAmount = haveRead;
269 currentReadBufferPosition = 0;
272 return currentReadBuffer->data();
277 totalAdvancements += amount;
280 currentReadBufferPosition += amount;
283 emit readProgress(totalAdvancements, totalAdvancements);
285 emit readProgress(totalAdvancements, size());
288 if (currentReadBufferPosition > currentReadBufferAmount) {
289 qint64 i = currentReadBufferPosition - currentReadBufferAmount;
291 if (!device->getChar(
nullptr)) {
292 emit readProgress(totalAdvancements - i, size());
298 currentReadBufferPosition = 0;
299 currentReadBufferAmount = 0;
312 bool reset = (initialPosition == 0) ? device->reset() : device->seek(initialPosition);
315 totalAdvancements = 0;
316 if (currentReadBuffer) {
317 delete currentReadBuffer;
318 currentReadBuffer =
nullptr;
320 currentReadBufferAmount = 0;
321 currentReadBufferPosition = 0;
332 if (device->isSequential())
335 return device->size() - initialPosition;
340 if (device->isSequential())
343 return device->pos();
347 : QIODevice(
nullptr), byteDevice(bd)
349 connect(bd, &QNonContiguousByteDevice::readyRead,
this, &QIODevice::readyRead);
359 return (byteDevice->size() == -1);
364 return byteDevice->atEnd();
369 return byteDevice->reset();
377 return byteDevice->size();
383 const char *readPointer = byteDevice->readPointer(maxSize, len);
387 memcpy(data, readPointer, len);
388 byteDevice->advanceReadPointer(len);
400
401
402
403
404
405
406
407
408
409
410
413
414
415
416
417
418
419
420QNonContiguousByteDevice *QNonContiguousByteDeviceFactory::create(QIODevice *device)
423 if (QBuffer *buffer = qobject_cast<QBuffer *>(device)) {
424 return new QNonContiguousByteDeviceByteArrayImpl(buffer);
431 return new QNonContiguousByteDeviceIoDeviceImpl(device);
435
436
437
438
439
440std::shared_ptr<QNonContiguousByteDevice> QNonContiguousByteDeviceFactory::createShared(QIODevice *device)
443 if (QBuffer *buffer = qobject_cast<QBuffer*>(device))
444 return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(buffer);
450 return std::make_shared<QNonContiguousByteDeviceIoDeviceImpl>(device);
454
455
456
457
458
459
460QNonContiguousByteDevice *
461QNonContiguousByteDeviceFactory::create(std::shared_ptr<QRingBuffer> ringBuffer)
463 return new QNonContiguousByteDeviceRingBufferImpl(std::move(ringBuffer));
467
468
469
470
471std::shared_ptr<QNonContiguousByteDevice>
472QNonContiguousByteDeviceFactory::createShared(std::shared_ptr<QRingBuffer> ringBuffer)
474 return std::make_shared<QNonContiguousByteDeviceRingBufferImpl>(std::move(ringBuffer));
478
479
480
481
482
483
484QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(
const QByteArray &byteArray)
486 return new QNonContiguousByteDeviceByteArrayImpl(byteArray);
490
491
492
493
494std::shared_ptr<QNonContiguousByteDevice>
495QNonContiguousByteDeviceFactory::createShared(
const QByteArray &byteArray)
497 return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(byteArray);
501
502
503
504
505
506
507QIODevice *QNonContiguousByteDeviceFactory::wrap(QNonContiguousByteDevice *byteDevice)
514 return new QByteDeviceWrappingIoDevice(byteDevice);
519#include "moc_qnoncontiguousbytedevice_p.cpp"
~QByteDeviceWrappingIoDevice()
qint64 readData(char *data, qint64 maxSize) override
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
qint64 size() const override
For open random-access devices, this function returns the size of the device.
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
bool atEnd() const override
Returns true if the current read and write position is at the end of the device (i....
bool reset() override
Seeks to the start of input for random-access devices.
qint64 writeData(const char *data, qint64 maxSize) override
Writes up to maxSize bytes from data to the device.
~QNonContiguousByteDeviceByteArrayImpl()
bool reset() override
Moves the internal read pointer back to the beginning.
qint64 size() const override
Returns the size of the complete device or -1 if unknown.
qint64 pos() const override
bool advanceReadPointer(qint64 amount) override
will advance the internal read pointer by amount bytes.
const char * readPointer(qint64 maximumLength, qint64 &len) override
Return a byte pointer for at most maximumLength bytes of that device.
bool atEnd() const override
Returns true if everything has been read and the read pointer cannot be advanced anymore.
QNonContiguousByteDeviceByteArrayImpl(QBuffer *buffer)
~QNonContiguousByteDeviceIoDeviceImpl()
qint64 pos() const override
bool atEnd() const override
Returns true if everything has been read and the read pointer cannot be advanced anymore.
qint64 size() const override
Returns the size of the complete device or -1 if unknown.
bool advanceReadPointer(qint64 amount) override
will advance the internal read pointer by amount bytes.
bool reset() override
Moves the internal read pointer back to the beginning.
const char * readPointer(qint64 maximumLength, qint64 &len) override
Return a byte pointer for at most maximumLength bytes of that device.
qint64 size() const override
Returns the size of the complete device or -1 if unknown.
qint64 pos() const override
bool atEnd() const override
Returns true if everything has been read and the read pointer cannot be advanced anymore.
bool advanceReadPointer(qint64 amount) override
will advance the internal read pointer by amount bytes.
bool reset() override
Moves the internal read pointer back to the beginning.
const char * readPointer(qint64 maximumLength, qint64 &len) override
Return a byte pointer for at most maximumLength bytes of that device.
~QNonContiguousByteDeviceRingBufferImpl()