30 : compLevel ((compressionLevel < 0 || compressionLevel > 9) ? -1 : compressionLevel)
32 using namespace zlibNamespace;
35 streamIsValid = (deflateInit2 (&stream, compLevel, Z_DEFLATED,
36 windowBits != 0 ? windowBits : MAX_WBITS,
37 8, strategy) == Z_OK);
43 zlibNamespace::deflateEnd (&stream);
46 bool write (
const uint8* data,
size_t dataSize,
OutputStream& out)
53 if (! doNextBlock (data, dataSize, out, Z_NO_FLUSH))
61 const uint8* data =
nullptr;
65 doNextBlock (data, dataSize, out, Z_FINISH);
69 enum { strategy = 0 };
71 zlibNamespace::z_stream stream;
73 bool isFirstDeflate =
true, streamIsValid =
false, finished =
false;
74 zlibNamespace::Bytef buffer[32768];
76 bool doNextBlock (
const uint8*& data,
size_t& dataSize,
OutputStream& out,
const int flushMode)
78 using namespace zlibNamespace;
82 stream.next_in =
const_cast<uint8*
> (data);
83 stream.next_out = buffer;
84 stream.avail_in = (uInt) dataSize;
85 stream.avail_out = (uInt)
sizeof (buffer);
87 auto result = isFirstDeflate ? deflateParams (&stream, compLevel, strategy)
88 : deflate (&stream, flushMode);
89 isFirstDeflate =
false;
98 data += dataSize - stream.avail_in;
99 dataSize = stream.avail_in;
100 auto bytesDone = (ssize_t)
sizeof (buffer) - (ssize_t) stream.avail_out;
101 return bytesDone <= 0 || out.
write (buffer, (
size_t) bytesDone);
122 : destStream (out, deleteDestStream),
125 jassert (out !=
nullptr);
135 helper->finish (*destStream);
141 jassert (destBuffer !=
nullptr && (ssize_t) howMany >= 0);
143 return helper->write (static_cast<const uint8*> (destBuffer), howMany, *destStream);
148 return destStream->getPosition();
162 GZIPTests() :
UnitTest (
"GZIP",
"Compression") {}
164 void runTest()
override 169 for (
int i = 100; --i >= 0;)
176 for (
int j = rng.
nextInt (100); --j >= 0;)
180 for (
int k = (
int) data.
getSize(); --k >= 0;)
181 data[k] = (
char) rng.
nextInt (255);
192 uncompressed << unzipper;
199 expect (memcmp (uncompressed.
getData(),
206 static GZIPTests gzipTests;
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
void flush() override
Flushes and closes the stream.
virtual bool write(const void *dataToWrite, size_t numberOfBytes)=0
Writes a block of data to the stream.
int nextInt() noexcept
Returns the next random 32 bit integer.
~GZIPCompressorOutputStream() override
Destructor.
GZIPCompressorOutputStream(OutputStream &destStream, int compressionLevel=-1, int windowBits=0)
Creates a compression stream.
A stream which uses zlib to compress the data written into it.
This is a base class for classes that perform a unit test.
bool write(const void *, size_t) override
Writes a block of data to the stream.
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
The base class for streams that write data to some kind of destination.
bool setPosition(int64) override
Tries to move the stream's output position.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
A random number generator.
Writes data to an internal memory buffer, which grows as required.
A class to hold a resizable block of raw data.
int64 getPosition() override
Returns the stream's current position.