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
9#include <utility>
10
12
99
103
108
115
119
121{
122 if (atEnd()) {
123 len = -1;
124 return nullptr;
125 }
126
127 if (maximumLength != -1)
128 len = qMin(maximumLength, size() - currentPosition);
129 else
131
132 return view.data() + currentPosition;
133}
134
141
146
148{
149 currentPosition = 0;
150 return true;
151}
152
157
162
164 : QNonContiguousByteDevice(), ringBuffer(std::move(rb))
165{
166}
167
171
173{
174 if (atEnd()) {
175 len = -1;
176 return nullptr;
177 }
178
179 const char *returnValue = ringBuffer->readPointerAtPosition(currentPosition, len);
180
181 if (maximumLength != -1)
182 len = qMin(len, maximumLength);
183
184 return returnValue;
185}
186
193
198
203
205{
206 currentPosition = 0;
207 return true;
208}
209
214
217 device(d),
218 currentReadBuffer(nullptr),
219 currentReadBufferSize(16 * 1024),
220 currentReadBufferAmount(0),
221 currentReadBufferPosition(0),
222 totalAdvancements(0),
223 eof(false),
224 initialPosition(d->pos())
225{
230}
231
236
238{
239 if (eof) {
240 len = -1;
241 return nullptr;
242 }
243
244 if (currentReadBuffer == nullptr)
245 currentReadBuffer = new QByteArray(currentReadBufferSize, '\0'); // lazy alloc
246
247 if (maximumLength == -1)
248 maximumLength = currentReadBufferSize;
249
253 }
254
255 qint64 haveRead = device->read(currentReadBuffer->data(),
256 qMin(maximumLength, currentReadBufferSize));
257
258 if ((haveRead == -1) || (haveRead == 0 && device->atEnd() && !device->isSequential())) {
259 eof = true;
260 len = -1;
261 // size was unknown before, emit a readProgress with the final size
262 if (size() == -1)
264 return nullptr;
265 }
266
267 currentReadBufferAmount = haveRead;
269
270 len = haveRead;
271 return currentReadBuffer->data();
272}
273
275{
276 totalAdvancements += amount;
277
278 // normal advancement
280
281 if (size() == -1)
283 else
285
286 // advancing over that what has actually been read before
289 while (i > 0) {
290 if (!device->getChar(nullptr)) {
292 return false; // ### FIXME handle eof
293 }
294 i--;
295 }
296
299 }
300
301 return true;
302}
303
305{
306 return eof;
307}
308
310{
312 if (reset) {
313 eof = false; // assume eof is false, it will be true after a read has been attempted
314 totalAdvancements = 0; // reset the progress counter
315 if (currentReadBuffer) {
316 delete currentReadBuffer;
317 currentReadBuffer = nullptr;
318 }
321 return true;
322 }
323
324 return false;
325}
326
328{
329 // note that this is different from the size() implementation of QIODevice!
330
331 if (device->isSequential())
332 return -1;
333
334 return device->size() - initialPosition;
335}
336
338{
339 if (device->isSequential())
340 return -1;
341
342 return device->pos();
343}
344
352
354 = default;
355
357{
358 return (byteDevice->size() == -1);
359}
360
362{
363 return byteDevice->atEnd();
364}
365
370
372{
373 if (isSequential())
374 return 0;
375
376 return byteDevice->size();
377}
378
380{
381 qint64 len;
382 const char *readPointer = byteDevice->readPointer(maxSize, len);
383 if (len == -1)
384 return -1;
385
386 memcpy(data, readPointer, len);
388 return len;
389}
390
392{
393 Q_UNUSED(data);
394 Q_UNUSED(maxSize);
395 return -1;
396}
397
420{
421 // shortcut if it is a QBuffer
422 if (QBuffer *buffer = qobject_cast<QBuffer *>(device)) {
424 }
425
426 // ### FIXME special case if device is a QFile that supports map()
427 // then we can actually deal with the file without using read/peek
428
429 // generic QIODevice
430 return new QNonContiguousByteDeviceIoDeviceImpl(device); // FIXME
431}
432
439std::shared_ptr<QNonContiguousByteDevice> QNonContiguousByteDeviceFactory::createShared(QIODevice *device)
440{
441 // shortcut if it is a QBuffer
442 if (QBuffer *buffer = qobject_cast<QBuffer*>(device))
443 return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(buffer);
444
445 // ### FIXME special case if device is a QFile that supports map()
446 // then we can actually deal with the file without using read/peek
447
448 // generic QIODevice
449 return std::make_shared<QNonContiguousByteDeviceIoDeviceImpl>(device); // FIXME
450}
451
460QNonContiguousByteDeviceFactory::create(std::shared_ptr<QRingBuffer> ringBuffer)
461{
462 return new QNonContiguousByteDeviceRingBufferImpl(std::move(ringBuffer));
463}
464
470std::shared_ptr<QNonContiguousByteDevice>
471QNonContiguousByteDeviceFactory::createShared(std::shared_ptr<QRingBuffer> ringBuffer)
472{
473 return std::make_shared<QNonContiguousByteDeviceRingBufferImpl>(std::move(ringBuffer));
474}
475
487
493std::shared_ptr<QNonContiguousByteDevice>
495{
496 return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(byteArray);
497}
498
507{
508 // ### FIXME if it already has been based on QIoDevice, we could that one out again
509 // and save some calling
510
511 // needed for FTP backend
512
513 return new QByteDeviceWrappingIoDevice(byteDevice);
514}
515
517
518#include "moc_qnoncontiguousbytedevice_p.cpp"
IOBluetoothDevice * device
\inmodule QtCore \reentrant
Definition qbuffer.h:16
constexpr qsizetype size() const noexcept
constexpr const_pointer data() const noexcept
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:612
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.
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
Combined button and popup list for selecting options.
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
QT_BEGIN_NAMESPACE constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:19
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
QQuickView * view
[0]