| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QNONCONTIGUOUSBYTEDEVICE_P_H |
| 41 | #define QNONCONTIGUOUSBYTEDEVICE_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists for the convenience |
| 48 | // of a number of Qt sources files. This header file may change from |
| 49 | // version to version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtCore/qobject.h> |
| 55 | #include <QtCore/qbytearray.h> |
| 56 | #include <QtCore/qbuffer.h> |
| 57 | #include <QtCore/qiodevice.h> |
| 58 | #include <QtCore/QSharedPointer> |
| 59 | #include "private/qringbuffer_p.h" |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | class Q_CORE_EXPORT QNonContiguousByteDevice : public QObject |
| 64 | { |
| 65 | Q_OBJECT |
| 66 | public: |
| 67 | virtual const char *readPointer(qint64 maximumLength, qint64 &len) = 0; |
| 68 | virtual bool advanceReadPointer(qint64 amount) = 0; |
| 69 | virtual bool atEnd() const = 0; |
| 70 | virtual qint64 pos() const { return -1; } |
| 71 | virtual bool reset() = 0; |
| 72 | virtual qint64 size() const = 0; |
| 73 | |
| 74 | virtual ~QNonContiguousByteDevice(); |
| 75 | |
| 76 | protected: |
| 77 | QNonContiguousByteDevice(); |
| 78 | |
| 79 | Q_SIGNALS: |
| 80 | void readyRead(); |
| 81 | void readProgress(qint64 current, qint64 total); |
| 82 | }; |
| 83 | |
| 84 | class Q_CORE_EXPORT QNonContiguousByteDeviceFactory |
| 85 | { |
| 86 | public: |
| 87 | static QNonContiguousByteDevice *create(QIODevice *device); |
| 88 | static QSharedPointer<QNonContiguousByteDevice> createShared(QIODevice *device); |
| 89 | |
| 90 | static QNonContiguousByteDevice *create(QByteArray *byteArray); |
| 91 | static QSharedPointer<QNonContiguousByteDevice> createShared(QByteArray *byteArray); |
| 92 | |
| 93 | static QNonContiguousByteDevice *create(QSharedPointer<QRingBuffer> ringBuffer); |
| 94 | static QSharedPointer<QNonContiguousByteDevice> createShared(QSharedPointer<QRingBuffer> ringBuffer); |
| 95 | |
| 96 | static QIODevice *wrap(QNonContiguousByteDevice *byteDevice); |
| 97 | }; |
| 98 | |
| 99 | // the actual implementations |
| 100 | // |
| 101 | |
| 102 | class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice |
| 103 | { |
| 104 | public: |
| 105 | QNonContiguousByteDeviceByteArrayImpl(QByteArray *ba); |
| 106 | ~QNonContiguousByteDeviceByteArrayImpl(); |
| 107 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 108 | bool advanceReadPointer(qint64 amount) override; |
| 109 | bool atEnd() const override; |
| 110 | bool reset() override; |
| 111 | qint64 size() const override; |
| 112 | qint64 pos() const override; |
| 113 | |
| 114 | protected: |
| 115 | QByteArray *byteArray; |
| 116 | qint64 currentPosition; |
| 117 | }; |
| 118 | |
| 119 | class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice |
| 120 | { |
| 121 | public: |
| 122 | QNonContiguousByteDeviceRingBufferImpl(QSharedPointer<QRingBuffer> rb); |
| 123 | ~QNonContiguousByteDeviceRingBufferImpl(); |
| 124 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 125 | bool advanceReadPointer(qint64 amount) override; |
| 126 | bool atEnd() const override; |
| 127 | bool reset() override; |
| 128 | qint64 size() const override; |
| 129 | qint64 pos() const override; |
| 130 | |
| 131 | protected: |
| 132 | QSharedPointer<QRingBuffer> ringBuffer; |
| 133 | qint64 currentPosition; |
| 134 | }; |
| 135 | |
| 136 | class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice |
| 137 | { |
| 138 | Q_OBJECT |
| 139 | public: |
| 140 | QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d); |
| 141 | ~QNonContiguousByteDeviceIoDeviceImpl(); |
| 142 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 143 | bool advanceReadPointer(qint64 amount) override; |
| 144 | bool atEnd() const override; |
| 145 | bool reset() override; |
| 146 | qint64 size() const override; |
| 147 | qint64 pos() const override; |
| 148 | |
| 149 | protected: |
| 150 | QIODevice *device; |
| 151 | QByteArray *currentReadBuffer; |
| 152 | qint64 currentReadBufferSize; |
| 153 | qint64 currentReadBufferAmount; |
| 154 | qint64 currentReadBufferPosition; |
| 155 | qint64 totalAdvancements; |
| 156 | bool eof; |
| 157 | qint64 initialPosition; |
| 158 | }; |
| 159 | |
| 160 | class QNonContiguousByteDeviceBufferImpl : public QNonContiguousByteDevice |
| 161 | { |
| 162 | Q_OBJECT |
| 163 | public: |
| 164 | QNonContiguousByteDeviceBufferImpl(QBuffer *b); |
| 165 | ~QNonContiguousByteDeviceBufferImpl(); |
| 166 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 167 | bool advanceReadPointer(qint64 amount) override; |
| 168 | bool atEnd() const override; |
| 169 | bool reset() override; |
| 170 | qint64 size() const override; |
| 171 | |
| 172 | protected: |
| 173 | QBuffer *buffer; |
| 174 | QByteArray byteArray; |
| 175 | QNonContiguousByteDeviceByteArrayImpl *arrayImpl; |
| 176 | }; |
| 177 | |
| 178 | // ... and the reverse thing |
| 179 | class QByteDeviceWrappingIoDevice : public QIODevice |
| 180 | { |
| 181 | public: |
| 182 | QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd); |
| 183 | ~QByteDeviceWrappingIoDevice(); |
| 184 | bool isSequential() const override; |
| 185 | bool atEnd() const override; |
| 186 | bool reset() override; |
| 187 | qint64 size() const override; |
| 188 | |
| 189 | protected: |
| 190 | qint64 readData(char *data, qint64 maxSize) override; |
| 191 | qint64 writeData(const char *data, qint64 maxSize) override; |
| 192 | |
| 193 | QNonContiguousByteDevice *byteDevice; |
| 194 | }; |
| 195 | |
| 196 | QT_END_NAMESPACE |
| 197 | |
| 198 | #endif |
| 199 | |