1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/* vim:set expandtab ts=4 sw=4 sts=4 cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsIOutputStream.h"
#include "nsString.h"
#include "nsConverterOutputStream.h"
#include "nsIUnicodeEncoder.h"
#include "mozilla/dom/EncodingUtils.h"
using mozilla::dom::EncodingUtils;
NS_IMPL_ISUPPORTS(nsConverterOutputStream,
nsIUnicharOutputStream,
nsIConverterOutputStream)
nsConverterOutputStream::~nsConverterOutputStream()
{
Close();
}
NS_IMETHODIMP
nsConverterOutputStream::Init(nsIOutputStream* aOutStream,
const char* aCharset,
uint32_t aBufferSize /* ignored */,
char16_t aReplacementChar)
{
NS_PRECONDITION(aOutStream, "Null output stream!");
nsAutoCString label;
if (!aCharset) {
label.AssignLiteral("UTF-8");
} else {
label = aCharset;
}
nsAutoCString encoding;
if (label.EqualsLiteral("UTF-16")) {
// Make sure to output a BOM when UTF-16 requested
encoding.Assign(label);
} else if (!EncodingUtils::FindEncodingForLabelNoReplacement(label,
encoding)) {
return NS_ERROR_UCONV_NOCONV;
}
mConverter = EncodingUtils::EncoderForEncoding(encoding);
mOutStream = aOutStream;
int32_t behaviour = aReplacementChar ? nsIUnicodeEncoder::kOnError_Replace
: nsIUnicodeEncoder::kOnError_Signal;
return mConverter->
SetOutputErrorBehavior(behaviour,
nullptr,
aReplacementChar);
}
NS_IMETHODIMP
nsConverterOutputStream::Write(uint32_t aCount, const char16_t* aChars,
bool* aSuccess)
{
if (!mOutStream) {
NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters");
return NS_BASE_STREAM_CLOSED;
}
NS_ASSERTION(mConverter, "Must have a converter when not closed");
int32_t inLen = aCount;
int32_t maxLen;
nsresult rv = mConverter->GetMaxLength(aChars, inLen, &maxLen);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString buf;
buf.SetLength(maxLen);
if (buf.Length() != (uint32_t) maxLen)
return NS_ERROR_OUT_OF_MEMORY;
int32_t outLen = maxLen;
rv = mConverter->Convert(aChars, &inLen, buf.BeginWriting(), &outLen);
if (NS_FAILED(rv))
return rv;
if (rv == NS_ERROR_UENC_NOMAPPING) {
// Yes, NS_ERROR_UENC_NOMAPPING is a success code
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
}
NS_ASSERTION((uint32_t) inLen == aCount,
"Converter didn't consume all the data!");
uint32_t written;
rv = mOutStream->Write(buf.get(), outLen, &written);
*aSuccess = NS_SUCCEEDED(rv) && written == uint32_t(outLen);
return rv;
}
NS_IMETHODIMP
nsConverterOutputStream::WriteString(const nsAString& aString, bool* aSuccess)
{
int32_t inLen = aString.Length();
nsAString::const_iterator i;
aString.BeginReading(i);
return Write(inLen, i.get(), aSuccess);
}
NS_IMETHODIMP
nsConverterOutputStream::Flush()
{
if (!mOutStream)
return NS_OK; // Already closed.
char buf[1024];
int32_t size = sizeof(buf);
nsresult rv = mConverter->Finish(buf, &size);
NS_ASSERTION(rv != NS_OK_UENC_MOREOUTPUT,
"1024 bytes ought to be enough for everyone");
if (NS_FAILED(rv))
return rv;
if (size == 0)
return NS_OK;
uint32_t written;
rv = mOutStream->Write(buf, size, &written);
if (NS_FAILED(rv)) {
NS_WARNING("Flush() lost data!");
return rv;
}
if (written != uint32_t(size)) {
NS_WARNING("Flush() lost data!");
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
}
return rv;
}
NS_IMETHODIMP
nsConverterOutputStream::Close()
{
if (!mOutStream)
return NS_OK; // Already closed.
nsresult rv1 = Flush();
nsresult rv2 = mOutStream->Close();
mOutStream = nullptr;
mConverter = nullptr;
return NS_FAILED(rv1) ? rv1 : rv2;
}
|