Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "nsBase64Encoder.h"
6
7
#include "mozilla/Base64.h"
8
9
NS_IMPL_ISUPPORTS(nsBase64Encoder, nsIOutputStream)
10
11
NS_IMETHODIMP
12
nsBase64Encoder::Close() { return NS_OK; }
13
14
NS_IMETHODIMP
15
nsBase64Encoder::Flush() { return NS_OK; }
16
17
NS_IMETHODIMP
18
nsBase64Encoder::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval) {
19
mData.Append(aBuf, aCount);
20
*_retval = aCount;
21
return NS_OK;
22
}
23
24
NS_IMETHODIMP
25
nsBase64Encoder::WriteFrom(nsIInputStream* aStream, uint32_t aCount,
26
uint32_t* _retval) {
27
return NS_ERROR_NOT_IMPLEMENTED;
28
}
29
30
NS_IMETHODIMP
31
nsBase64Encoder::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
32
uint32_t aCount, uint32_t* _retval) {
33
return NS_ERROR_NOT_IMPLEMENTED;
34
}
35
36
NS_IMETHODIMP
37
nsBase64Encoder::IsNonBlocking(bool* aNonBlocking) {
38
*aNonBlocking = false;
39
return NS_OK;
40
}
41
42
nsresult nsBase64Encoder::Finish(nsACString& result) {
43
nsresult rv = mozilla::Base64Encode(mData, result);
44
if (NS_FAILED(rv)) {
45
return rv;
46
}
47
48
// Free unneeded memory and allow reusing the object
49
mData.Truncate();
50
return NS_OK;
51
}