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