This sample reads and writes streams of data compressed with nn::nlib::ZlibOutputStream
and nn::nlib::ZlibInputStream
.
DATA_SIZE
bytes of data are compressed and then expanded. Even if DATA_SIZE
is large, this process can compress and expand the data using a relatively small amount of memory.
const int DATA_SIZE = 10000000;
bool Compress(ReallocOutputStream::UniquePtrType* vec, size_t* n) {
ReallocOutputStream base_out;
{
ZlibOutputStream ostr;
if (0 != ostr.SetStream(&base_out)) return false;
nlib_printf(
"compress %d bytes: [0, ... 255, 0, ... 255, ....]\n", DATA_SIZE);
for (int i = 0; i < DATA_SIZE; ++i) {
return false;
}
}
}
*n = base_out.Release(vec);
return true;
}
bool UnCompress(const uint8_t* vec, size_t n) {
MemoryInputStream base_in(&vec[0], n);
{
ZlibInputStream istr;
for (int i = 0; i < DATA_SIZE; ++i) {
int c = istr.Read();
if (c != i % 256) {
return false;
}
}
if (!istr.IsEos()) return false;
}
if (!base_in.Close()) return false;
return true;
}
bool SampleMain(int, char**) {
ReallocOutputStream::UniquePtrType vec;
size_t n;
return Compress(&vec, &n) && UnCompress(vec.get(), n);
}
NLIB_MAINFUNC