A compress() method is added to the JavaScript ArrayBuffer object.
Showing the changes to libraries/script-engine/src/ArrayBufferPrototype.cpp (1 of 3 files). Show all
Unified · Split
| @@ -14,6 +14,9 @@ · 13 unmodified lines | ||
| 14 | 14 | #include "ArrayBufferClass.h" |
| 15 | 15 | #include "ArrayBufferPrototype.h" |
| 16 | 16 | |
| 17 | +static const int QCOMPRESS_HEADER_POSITION = 0; | |
| 18 | +static const int QCOMPRESS_HEADER_SIZE = 4; | |
| 19 | + | |
| 17 | 20 | Q_DECLARE_METATYPE(QByteArray*) |
| 18 | 21 | |
| 19 | 22 | ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) { |
| @@ -43,6 +46,18 @@ QByteArray ArrayBufferPrototype::slice(qint32 begin) const { · 23 unmodified lines | ||
| 43 | 46 | return ba->mid(begin, -1); |
| 44 | 47 | } |
| 45 | 48 | |
| 49 | +QByteArray ArrayBufferPrototype::compress() const { | |
| 50 | + QByteArray* ba = thisArrayBuffer(); | |
| 51 | + | |
| 52 | + QByteArray buffer = qCompress(*ba); | |
| 53 | + | |
| 54 | + // Qt's qCompress() default compression level (-1) is the standard zLib compression. | |
| 55 | + // Here remove Qt's custom header that prevents the data server from uncompressing the files with zLib. | |
| 56 | + buffer.remove(QCOMPRESS_HEADER_POSITION, QCOMPRESS_HEADER_SIZE); | |
| 57 | + | |
| 58 | + return buffer; | |
| 59 | +} | |
| 60 | + | |
| 46 | 61 | QByteArray* ArrayBufferPrototype::thisArrayBuffer() const { |
| 47 | 62 | return qscriptvalue_cast<QByteArray*>(thisObject().data()); |
| 48 | 63 | } |