michael@0: //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsCheckSummedOutputStream_h__ michael@0: #define nsCheckSummedOutputStream_h__ michael@0: michael@0: #include "nsILocalFile.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsICryptoHash.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsString.h" michael@0: #include "../../../netwerk/base/src/nsFileStreams.h" michael@0: #include "nsToolkitCompsCID.h" michael@0: michael@0: class nsCheckSummedOutputStream : public nsSafeFileOutputStream michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: michael@0: // Size of MD5 hash in bytes michael@0: static const uint32_t CHECKSUM_SIZE = 16; michael@0: michael@0: nsCheckSummedOutputStream() {} michael@0: virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); } michael@0: michael@0: NS_IMETHOD Finish(); michael@0: NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result); michael@0: NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags); michael@0: michael@0: protected: michael@0: nsCOMPtr mHash; michael@0: nsAutoCString mCheckSum; michael@0: }; michael@0: michael@0: // returns a file output stream which can be QI'ed to nsIFileOutputStream. michael@0: inline nsresult michael@0: NS_NewCheckSummedOutputStream(nsIOutputStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsCOMPtr out = new nsCheckSummedOutputStream(); michael@0: nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap michael@0: return rv; michael@0: } michael@0: michael@0: #endif