diff options
author | Tim Schumacher <timschumi@gmx.de> | 2022-12-26 15:58:42 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2023-01-03 10:01:09 +0000 |
commit | f0b035b66c5ae0f5990a58186598aa9f604dda35 (patch) | |
tree | 75c4b6919dc0a545bed7d609bf2f7780dd7ae4e1 /Userland/Libraries/LibCompress | |
parent | 23a9d62f39677bd5118e1ab2b14bd0e281ca1c60 (diff) | |
download | serenity-f0b035b66c5ae0f5990a58186598aa9f604dda35.zip |
LibCompress: Switch `ZlibCompressor` to a constructor pattern
We don't have anything fallible in the constructor yet, but what's there
should be fallible and once we switch to Core::Stream it will be
fallible.
Diffstat (limited to 'Userland/Libraries/LibCompress')
-rw-r--r-- | Userland/Libraries/LibCompress/Zlib.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibCompress/Zlib.h | 7 |
2 files changed, 20 insertions, 13 deletions
diff --git a/Userland/Libraries/LibCompress/Zlib.cpp b/Userland/Libraries/LibCompress/Zlib.cpp index d39bdcbb37..8c9ea5e4ef 100644 --- a/Userland/Libraries/LibCompress/Zlib.cpp +++ b/Userland/Libraries/LibCompress/Zlib.cpp @@ -68,16 +68,22 @@ u32 ZlibDecompressor::checksum() return m_checksum; } -ZlibCompressor::ZlibCompressor(OutputStream& stream, ZlibCompressionLevel compression_level) - : m_output_stream(stream) +ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(OutputStream& stream, ZlibCompressionLevel compression_level) { // Zlib only defines Deflate as a compression method. auto compression_method = ZlibCompressionMethod::Deflate; - write_header(compression_method, compression_level); + auto zlib_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ZlibCompressor(stream, compression_level))); + zlib_compressor->write_header(compression_method, compression_level); + + return zlib_compressor; +} +ZlibCompressor::ZlibCompressor(OutputStream& stream, ZlibCompressionLevel compression_level) + : m_output_stream(stream) // FIXME: Find a way to compress with Deflate's "Best" compression level. - m_compressor = make<DeflateCompressor>(stream, static_cast<DeflateCompressor::CompressionLevel>(compression_level)); + , m_compressor(make<DeflateCompressor>(stream, static_cast<DeflateCompressor::CompressionLevel>(compression_level))) +{ } ZlibCompressor::~ZlibCompressor() @@ -139,17 +145,17 @@ void ZlibCompressor::finish() m_finished = true; } -Optional<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, ZlibCompressionLevel compression_level) +ErrorOr<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, ZlibCompressionLevel compression_level) { DuplexMemoryStream output_stream; - ZlibCompressor zlib_stream { output_stream, compression_level }; + auto zlib_stream = TRY(ZlibCompressor::construct(output_stream, compression_level)); - zlib_stream.write_or_error(bytes); + zlib_stream->write_or_error(bytes); - zlib_stream.finish(); + zlib_stream->finish(); - if (zlib_stream.handle_any_error()) - return {}; + if (zlib_stream->handle_any_error()) + return Error::from_string_literal("Underlying ZlibStream indicated an error"); return output_stream.copy_into_contiguous_buffer(); } diff --git a/Userland/Libraries/LibCompress/Zlib.h b/Userland/Libraries/LibCompress/Zlib.h index a69d91d6ce..1f6222e2c5 100644 --- a/Userland/Libraries/LibCompress/Zlib.h +++ b/Userland/Libraries/LibCompress/Zlib.h @@ -62,21 +62,22 @@ private: class ZlibCompressor : public OutputStream { public: - ZlibCompressor(OutputStream&, ZlibCompressionLevel = ZlibCompressionLevel::Default); + static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(OutputStream&, ZlibCompressionLevel = ZlibCompressionLevel::Default); ~ZlibCompressor(); size_t write(ReadonlyBytes) override; bool write_or_error(ReadonlyBytes) override; void finish(); - static Optional<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default); + static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default); private: + ZlibCompressor(OutputStream&, ZlibCompressionLevel); void write_header(ZlibCompressionMethod, ZlibCompressionLevel); bool m_finished { false }; OutputBitStream m_output_stream; - OwnPtr<OutputStream> m_compressor; + NonnullOwnPtr<OutputStream> m_compressor; Crypto::Checksum::Adler32 m_adler32_checksum; }; |