1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <qbytearray.h>
60 // this class handles a list of QByteArrays. It is a variant of QRingBuffer
61 // that avoid malloc/realloc/memcpy.
65 QList<QByteArray> buffers;
66 qint64 bufferCompleteSize;
68 QByteDataBuffer() : bufferCompleteSize(0)
77 inline void append(QByteDataBuffer& other)
82 buffers.append(other.buffers);
83 bufferCompleteSize += other.byteAmount();
87 inline void append(const QByteArray& bd)
93 bufferCompleteSize += bd.size();
96 inline void prepend(QByteArray& bd)
102 bufferCompleteSize += bd.size();
105 // return the first QByteData. User of this function has to free() its .data!
106 // preferably use this function to read data.
107 inline QByteArray read()
109 bufferCompleteSize -= buffers.first().size();
110 return buffers.takeFirst();
113 // return everything. User of this function has to free() its .data!
114 // avoid to use this, it might malloc and memcpy.
115 inline QByteArray readAll()
117 return read(byteAmount());
120 // return amount. User of this function has to free() its .data!
121 // avoid to use this, it might malloc and memcpy.
122 inline QByteArray read(qint64 amount)
124 amount = qMin(byteAmount(), amount);
126 byteData.resize(amount);
127 read(byteData.data(), byteData.size());
131 // return amount bytes. User of this function has to free() its .data!
132 // avoid to use this, it will memcpy.
133 qint64 read(char* dst, qint64 amount)
135 amount = qMin(amount, byteAmount());
136 qint64 originalAmount = amount;
137 char *writeDst = dst;
140 QByteArray first = buffers.takeFirst();
141 if (amount >= first.size()) {
142 // take it completely
143 bufferCompleteSize -= first.size();
144 amount -= first.size();
145 memcpy(writeDst, first.constData(), first.size());
146 writeDst += first.size();
149 // take a part of it & it is the last one to take
150 bufferCompleteSize -= amount;
151 memcpy(writeDst, first.constData(), amount);
153 qint64 newFirstSize = first.size() - amount;
154 QByteArray newFirstData;
155 newFirstData.resize(newFirstSize);
156 memcpy(newFirstData.data(), first.constData() + amount, newFirstSize);
157 buffers.prepend(newFirstData);
164 return originalAmount;
167 inline char getChar()
177 bufferCompleteSize = 0;
180 // The byte count of all QByteArrays
181 inline qint64 byteAmount() const
183 return bufferCompleteSize;
186 // the number of QByteArrays
187 inline qint64 bufferCount() const
189 return buffers.length();
192 inline bool isEmpty() const
194 return byteAmount() == 0;
197 inline qint64 sizeNextBlock() const
199 if(buffers.isEmpty())
202 return buffers.first().size();
205 inline QByteArray& operator[](int i)
210 inline bool canReadLine() const {
211 for (int i = 0; i < buffers.length(); i++)
212 if (buffers.at(i).contains('\n'))
220 #endif // QBYTEDATA_H