Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qnoncontiguousbytedevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <qbuffer.h>
6#include <qdebug.h>
7#include <qfile.h>
8
10
97
101
102// FIXME we should scrap this whole implementation and instead change the ByteArrayImpl to be able to cope with sub-arrays?
116
120
122{
123 return arrayImpl->readPointer(maximumLength, len);
124}
125
130
132{
133 return arrayImpl->atEnd();
134}
135
140
145
150
154
156{
157 if (atEnd()) {
158 len = -1;
159 return nullptr;
160 }
161
162 if (maximumLength != -1)
163 len = qMin(maximumLength, size() - currentPosition);
164 else
166
168}
169
176
181
183{
184 currentPosition = 0;
185 return true;
186}
187
192
197
199 : QNonContiguousByteDevice(), ringBuffer(std::move(rb))
200{
201}
202
206
208{
209 if (atEnd()) {
210 len = -1;
211 return nullptr;
212 }
213
214 const char *returnValue = ringBuffer->readPointerAtPosition(currentPosition, len);
215
216 if (maximumLength != -1)
217 len = qMin(len, maximumLength);
218
219 return returnValue;
220}
221
228
233
238
240{
241 currentPosition = 0;
242 return true;
243}
244
249
252 device(d),
253 currentReadBuffer(nullptr),
254 currentReadBufferSize(16 * 1024),
255 currentReadBufferAmount(0),
256 currentReadBufferPosition(0),
257 totalAdvancements(0),
258 eof(false),
259 initialPosition(d->pos())
260{
265}
266
271
273{
274 if (eof) {
275 len = -1;
276 return nullptr;
277 }
278
279 if (currentReadBuffer == nullptr)
280 currentReadBuffer = new QByteArray(currentReadBufferSize, '\0'); // lazy alloc
281
282 if (maximumLength == -1)
283 maximumLength = currentReadBufferSize;
284
288 }
289
290 qint64 haveRead = device->read(currentReadBuffer->data(),
291 qMin(maximumLength, currentReadBufferSize));
292
293 if ((haveRead == -1) || (haveRead == 0 && device->atEnd() && !device->isSequential())) {
294 eof = true;
295 len = -1;
296 // size was unknown before, emit a readProgress with the final size
297 if (size() == -1)
299 return nullptr;
300 }
301
302 currentReadBufferAmount = haveRead;
304
305 len = haveRead;
306 return currentReadBuffer->data();
307}
308
310{
311 totalAdvancements += amount;
312
313 // normal advancement
315
316 if (size() == -1)
318 else
320
321 // advancing over that what has actually been read before
324 while (i > 0) {
325 if (!device->getChar(nullptr)) {
327 return false; // ### FIXME handle eof
328 }
329 i--;
330 }
331
334 }
335
336 return true;
337}
338
340{
341 return eof;
342}
343
345{
347 if (reset) {
348 eof = false; // assume eof is false, it will be true after a read has been attempted
349 totalAdvancements = 0; // reset the progress counter
350 if (currentReadBuffer) {
351 delete currentReadBuffer;
352 currentReadBuffer = nullptr;
353 }
356 return true;
357 }
358
359 return false;
360}
361
363{
364 // note that this is different from the size() implementation of QIODevice!
365
366 if (device->isSequential())
367 return -1;
368
369 return device->size() - initialPosition;
370}
371
373{
374 if (device->isSequential())
375 return -1;
376
377 return device->pos();
378}
379
387
389 = default;
390
392{
393 return (byteDevice->size() == -1);
394}
395
397{
398 return byteDevice->atEnd();
399}
400
405
407{
408 if (isSequential())
409 return 0;
410
411 return byteDevice->size();
412}
413
415{
416 qint64 len;
417 const char *readPointer = byteDevice->readPointer(maxSize, len);
418 if (len == -1)
419 return -1;
420
421 memcpy(data, readPointer, len);
423 return len;
424}
425
427{
428 Q_UNUSED(data);
429 Q_UNUSED(maxSize);
430 return -1;
431}
432
455{
456 // shortcut if it is a QBuffer
457 if (QBuffer *buffer = qobject_cast<QBuffer *>(device)) {
459 }
460
461 // ### FIXME special case if device is a QFile that supports map()
462 // then we can actually deal with the file without using read/peek
463
464 // generic QIODevice
465 return new QNonContiguousByteDeviceIoDeviceImpl(device); // FIXME
466}
467
474std::shared_ptr<QNonContiguousByteDevice> QNonContiguousByteDeviceFactory::createShared(QIODevice *device)
475{
476 // shortcut if it is a QBuffer
477 if (QBuffer *buffer = qobject_cast<QBuffer*>(device))
478 return std::make_shared<QNonContiguousByteDeviceBufferImpl>(buffer);
479
480 // ### FIXME special case if device is a QFile that supports map()
481 // then we can actually deal with the file without using read/peek
482
483 // generic QIODevice
484 return std::make_shared<QNonContiguousByteDeviceIoDeviceImpl>(device); // FIXME
485}
486
495QNonContiguousByteDeviceFactory::create(std::shared_ptr<QRingBuffer> ringBuffer)
496{
497 return new QNonContiguousByteDeviceRingBufferImpl(std::move(ringBuffer));
498}
499
505std::shared_ptr<QNonContiguousByteDevice>
506QNonContiguousByteDeviceFactory::createShared(std::shared_ptr<QRingBuffer> ringBuffer)
507{
508 return std::make_shared<QNonContiguousByteDeviceRingBufferImpl>(std::move(ringBuffer));
509}
510
522
528std::shared_ptr<QNonContiguousByteDevice>
530{
531 return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(byteArray);
532}
533
542{
543 // ### FIXME if it already has been based on QIoDevice, we could that one out again
544 // and save some calling
545
546 // needed for FTP backend
547
548 return new QByteDeviceWrappingIoDevice(byteDevice);
549}
550
552
553#include "moc_qnoncontiguousbytedevice_p.cpp"
IOBluetoothDevice * device
\inmodule QtCore \reentrant
Definition qbuffer.h:16
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
QNonContiguousByteDevice * byteDevice
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.
QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd)
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.
\inmodule QtCore \reentrant
Definition qiodevice.h:34
virtual bool open(QIODeviceBase::OpenMode mode)
Opens the device and sets its OpenMode to mode.
void readyRead()
This signal is emitted once every time new data is available for reading from the device's current re...
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
bool getChar(char *c)
Reads one character from the device and stores it in c.
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success,...
virtual bool reset()
Seeks to the start of input for random-access devices.
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i....
void readChannelFinished()
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
QNonContiguousByteDeviceByteArrayImpl * arrayImpl
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.
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 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.
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.
static QNonContiguousByteDevice * create(QIODevice *device)
Create a QNonContiguousByteDevice out of a QIODevice.
static std::shared_ptr< QNonContiguousByteDevice > createShared(QIODevice *device)
Create a QNonContiguousByteDevice out of a QIODevice, return it in a std::shared_ptr.
static QIODevice * wrap(QNonContiguousByteDevice *byteDevice)
Wrap the byteDevice (possibly again) into a QIODevice.
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.
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(std::shared_ptr< QRingBuffer > rb)
virtual const char * readPointer(qint64 maximumLength, qint64 &len)=0
Return a byte pointer for at most maximumLength bytes of that device.
virtual qint64 size() const =0
Returns the size of the complete device or -1 if unknown.
virtual bool advanceReadPointer(qint64 amount)=0
will advance the internal read pointer by amount bytes.
virtual bool reset()=0
Moves the internal read pointer back to the beginning.
void readyRead()
Emitted when there is data available.
void readProgress(qint64 current, qint64 total)
Emitted when data has been "read" by advancing the read pointer.
virtual bool atEnd() const =0
Returns true if everything has been read and the read pointer cannot be advanced anymore.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
Combined button and popup list for selecting options.
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLboolean GLboolean GLboolean b
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLboolean reset
GLenum GLsizei len
#define emit
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:60
QByteArray ba
[0]
QObject::connect nullptr