libutil: add makeDecompressionSource
Change-Id: Iac7f24d79e24417436b9b5cbefd6af051aeea0a6
This commit is contained in:
parent
3425e90d76
commit
67f778670c
|
@ -192,11 +192,9 @@ struct BrotliDecompressionSource : Source
|
|||
|
||||
std::string decompress(const std::string & method, std::string_view in)
|
||||
{
|
||||
StringSink ssink;
|
||||
auto sink = makeDecompressionSink(method, ssink);
|
||||
(*sink)(in);
|
||||
sink->finish();
|
||||
return std::move(ssink.s);
|
||||
StringSource src{in};
|
||||
auto filter = makeDecompressionSource(method, src);
|
||||
return filter->drain();
|
||||
}
|
||||
|
||||
std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink)
|
||||
|
@ -224,6 +222,19 @@ std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Si
|
|||
});
|
||||
}
|
||||
|
||||
std::unique_ptr<Source> makeDecompressionSource(const std::string & method, Source & inner)
|
||||
{
|
||||
if (method == "none" || method == "") {
|
||||
return std::make_unique<LambdaSource>([&](char * data, size_t len) {
|
||||
return inner.read(data, len);
|
||||
});
|
||||
} else if (method == "br") {
|
||||
return std::make_unique<BrotliDecompressionSource>(inner);
|
||||
} else {
|
||||
return std::make_unique<ArchiveDecompressionSource>(inner);
|
||||
}
|
||||
}
|
||||
|
||||
struct BrotliCompressionSink : ChunkedCompressionSink
|
||||
{
|
||||
Sink & nextSink;
|
||||
|
|
|
@ -19,6 +19,7 @@ struct CompressionSink : BufferedSink, FinishSink
|
|||
std::string decompress(const std::string & method, std::string_view in);
|
||||
|
||||
std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink);
|
||||
std::unique_ptr<Source> makeDecompressionSource(const std::string & method, Source & inner);
|
||||
|
||||
std::string compress(const std::string & method, std::string_view in, const bool parallel = false, int level = -1);
|
||||
|
||||
|
|
|
@ -81,16 +81,17 @@ namespace nix {
|
|||
}
|
||||
|
||||
TEST(makeCompressionSink, compressAndDecompress) {
|
||||
StringSink strSink;
|
||||
auto inputString = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
|
||||
auto decompressionSink = makeDecompressionSink("bzip2", strSink);
|
||||
auto sink = makeCompressionSink("bzip2", *decompressionSink);
|
||||
|
||||
StringSink strSink;
|
||||
auto sink = makeCompressionSink("bzip2", strSink);
|
||||
(*sink)(inputString);
|
||||
sink->finish();
|
||||
decompressionSink->finish();
|
||||
|
||||
ASSERT_STREQ(strSink.s.c_str(), inputString);
|
||||
StringSource strSource{strSink.s};
|
||||
auto decompressionSource = makeDecompressionSource("bzip2", strSource);
|
||||
|
||||
ASSERT_STREQ(decompressionSource->drain().c_str(), inputString);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue