Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsCheckSummedOutputStream_h__ |
michael@0 | 7 | #define nsCheckSummedOutputStream_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsILocalFile.h" |
michael@0 | 10 | #include "nsIFile.h" |
michael@0 | 11 | #include "nsIOutputStream.h" |
michael@0 | 12 | #include "nsICryptoHash.h" |
michael@0 | 13 | #include "nsNetCID.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | #include "../../../netwerk/base/src/nsFileStreams.h" |
michael@0 | 16 | #include "nsToolkitCompsCID.h" |
michael@0 | 17 | |
michael@0 | 18 | class nsCheckSummedOutputStream : public nsSafeFileOutputStream |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 22 | |
michael@0 | 23 | // Size of MD5 hash in bytes |
michael@0 | 24 | static const uint32_t CHECKSUM_SIZE = 16; |
michael@0 | 25 | |
michael@0 | 26 | nsCheckSummedOutputStream() {} |
michael@0 | 27 | virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); } |
michael@0 | 28 | |
michael@0 | 29 | NS_IMETHOD Finish(); |
michael@0 | 30 | NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result); |
michael@0 | 31 | NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags); |
michael@0 | 32 | |
michael@0 | 33 | protected: |
michael@0 | 34 | nsCOMPtr<nsICryptoHash> mHash; |
michael@0 | 35 | nsAutoCString mCheckSum; |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | // returns a file output stream which can be QI'ed to nsIFileOutputStream. |
michael@0 | 39 | inline nsresult |
michael@0 | 40 | NS_NewCheckSummedOutputStream(nsIOutputStream **result, |
michael@0 | 41 | nsIFile *file, |
michael@0 | 42 | int32_t ioFlags = -1, |
michael@0 | 43 | int32_t perm = -1, |
michael@0 | 44 | int32_t behaviorFlags = 0) |
michael@0 | 45 | { |
michael@0 | 46 | nsCOMPtr<nsIFileOutputStream> out = new nsCheckSummedOutputStream(); |
michael@0 | 47 | nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags); |
michael@0 | 48 | if (NS_SUCCEEDED(rv)) |
michael@0 | 49 | NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap |
michael@0 | 50 | return rv; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | #endif |