Bug 1919083 - [ci] Enable os-integration variant for more suites, r=jmaher
[gecko.git] / xpcom / base / nsGZFileWriter.h
blobcccefd34584e225ead23168a2e120547931a7a47
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsGZFileWriter_h
8 #define nsGZFileWriter_h
10 #include "nsISupportsImpl.h"
11 #include "zlib.h"
12 #include "nsDependentString.h"
13 #include <stdio.h>
15 /**
16 * A simple class for writing to a .gz file.
18 * Note that the file that this interface produces has a different format than
19 * what you'd get if you compressed your data as a gzip stream and dumped the
20 * result to a file.
22 * The standard gunzip tool cannot decompress a raw gzip stream, but can handle
23 * the files produced by this interface.
25 class nsGZFileWriter final {
26 virtual ~nsGZFileWriter();
28 public:
29 explicit nsGZFileWriter();
31 NS_INLINE_DECL_REFCOUNTING(nsGZFileWriter)
33 /**
34 * Initialize this object. We'll write our gzip'ed data to the given file,
35 * overwriting its contents if the file exists.
37 * Init() will return an error if called twice. It's an error to call any
38 * other method on this interface without first calling Init().
40 [[nodiscard]] nsresult Init(nsIFile* aFile);
42 /**
43 * Alternate version of Init() for use when the file is already opened;
44 * e.g., with a FileDescriptor passed over IPC.
46 [[nodiscard]] nsresult InitANSIFileDesc(FILE* aFile);
48 /**
49 * Write the given string to the file.
51 [[nodiscard]] nsresult Write(const nsACString& aStr);
53 /**
54 * Write |length| bytes of |str| to the file.
56 [[nodiscard]] nsresult Write(const char* aStr, uint32_t aLen) {
57 return Write(nsDependentCSubstring(aStr, aLen));
60 /**
61 * Close this nsGZFileWriter. This method will be run when the underlying
62 * object is destroyed, so it's not strictly necessary to explicitly call it
63 * from your code.
65 * It's an error to call this method twice, and it's an error to call Write()
66 * after Finish() has been called.
68 nsresult Finish();
70 private:
71 bool mInitialized;
72 bool mFinished;
73 FILE* mGZFile;
74 z_stream mZStream = {};
75 Bytef mBuffer[8192];
78 #endif