Backed out changeset bd51857879db (bug 1862957) for causing WakeLock related failures...
[gecko.git] / xpcom / base / nsGZFileWriter.h
blobdc33b4a2ded7ab9823661fc46bd6fd8e8b814feb
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 enum Operation { Append, Create };
31 explicit nsGZFileWriter(Operation aMode = Create);
33 NS_INLINE_DECL_REFCOUNTING(nsGZFileWriter)
35 /**
36 * Initialize this object. We'll write our gzip'ed data to the given file,
37 * overwriting its contents if the file exists.
39 * Init() will return an error if called twice. It's an error to call any
40 * other method on this interface without first calling Init().
42 [[nodiscard]] nsresult Init(nsIFile* aFile);
44 /**
45 * Alternate version of Init() for use when the file is already opened;
46 * e.g., with a FileDescriptor passed over IPC.
48 [[nodiscard]] nsresult InitANSIFileDesc(FILE* aFile);
50 /**
51 * Write the given string to the file.
53 [[nodiscard]] nsresult Write(const nsACString& aStr);
55 /**
56 * Write |length| bytes of |str| to the file.
58 [[nodiscard]] nsresult Write(const char* aStr, uint32_t aLen) {
59 return Write(nsDependentCSubstring(aStr, aLen));
62 /**
63 * Close this nsGZFileWriter. This method will be run when the underlying
64 * object is destroyed, so it's not strictly necessary to explicitly call it
65 * from your code.
67 * It's an error to call this method twice, and it's an error to call Write()
68 * after Finish() has been called.
70 nsresult Finish();
72 private:
73 Operation mMode;
74 bool mInitialized;
75 bool mFinished;
76 gzFile mGZFile;
79 #endif